user5005768Himadree
user5005768Himadree

Reputation: 1439

How avoid offest problems by correctly sending multiple variables from ajax

I am new in ajax.I want to send html form values along with some other variables to php using ajax,so i serialize all input values and send to php.But the problem offset occurred, when i try to send multiple variables.

enter image description here

var_dump($_POST);
array (size=2)
  'values' => string 'loanid=123&loan_name=Bank+Loan&description=FRB+Bank+Loan&amount=25000&textdatetimepicker1=27%2F01%2F2016&rate=12.5' (length=114)
  'flag' => string '2' (length=1)

Here is my Ajax Code:

<script type="text/javascript">                                             
                $("form").on("submit", function( event ) {              
                    event.preventDefault();                 
                    var flag = 2;   //want to send this 
                    var values = $( this ).serialize();  //want to send this                        
                    $('#serialize').append(values);                  
                    $.ajax({
                        url: "loan_type_info.php",
                        type: "post",
                        async:true,
                        data:  {values:values , flag:flag},
                        dataType: 'html',
                        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                        success: function(data) {                           
                            $('#result').append('Response Data:');
                            $('#result').append(data);                          
                        },                  
                        error: function(jqXHR, textStatus, errorThrown) {
                           console.log(textStatus, errorThrown);
                        }
                    });
                });         
</script>

I think php is not getting all the variables.I also follow this link AJAX - multiple data and try with data: {values,flag} and data: {values:values , flag:flag} or values={values:values , flag:flag}. But still same offset problem.

here is my php code:

<?php
// they all came from serialize data: values
$loanid      = (isset($_POST['loanid'])) ? $_POST['loanid'] : "not";
$loan_name   = (isset($_POST['loan_name'])) ? $_POST['loan_name'] : "not";
$description = (isset($_POST['description'])) ? $_POST['description'] : "not";
$amount      = (isset($_POST['amount'])) ? $_POST['amount'] : "not";
$datestring  = (isset($_POST['textdatetimepicker1'])) ? $_POST['textdatetimepicker1'] : "not";
$rate        = (isset($_POST['rate'])) ? $_POST['rate'] : "not";
$date_arr = explode('/', $datestring);
$date     = date("Y/m/d", strtotime($date_arr[2] . $date_arr[1] . $date_arr[0]));

//  comes from  data: flag
$flag  = (isset($_POST['flag'])) ? $_POST['flag'] : 0;
?>

Please help me and let me know for any further information.Thanks.

Upvotes: 1

Views: 69

Answers (1)

hherger
hherger

Reputation: 1680

Corresponding to your Ajax code, the PHP script gets 2 $_POST arguments: values and flag.

The values argument thus has to be split up and interpreted by the PHP script. The snipplet below shows how this can be done.

Notes:

  • I have simulated the $_POST array at the beginning.
  • The values variable is split explode('&',$_POST['values']).
  • In a loop the key=value pairs are separated explode('=', $value), the value part is url_decoded, since it comes url_encoded from the Ajax call.
  • Finally, the value part is assigned to a variable with the name of the key part $$keyvar[0] = $keyvar[1].

Code

    <?php

    $_POST = array(
        'values' => 'loanid=123&loan_name=Bank+Loan&description=FRB+Bank+Loan&amount=25000&textdatetimepicker1=27%2F01%2F2016&rate=12.5',
        'flag' => '2'
    );

    // Code BEGIN ==============================================================

    $values = (isset($_POST['values']) && !empty($_POST['values'])) ? explode('&',$_POST['values']) : false;
    $flag =   (isset($_POST['flag']) && !empty($_POST['flag'])) ? $_POST['flag'] : '0';

    if ($values!==false) foreach ($values as $key => $value) {
        $keyvar = explode('=', $value);
        $keyvar[1] = (isset($keyvar[1])) ? urldecode($keyvar[1]) : '';
        $$keyvar[0] = $keyvar[1];
    }

    // Code END ================================================================

    echo '$loanid=' . ((isset($loanid)) ? $loanid : 'n/a') . '<br />';
    echo '$loan_name=' . ((isset($loan_name)) ? $loan_name : 'n/a') . '<br />';
    echo '$description=' . ((isset($description)) ? $description : 'n/a') . '<br />';
    echo '$amount=' . ((isset($amount)) ? $amount : 'n/a') . '<br />';
    echo '$datestring=' . ((isset($datestring)) ? $datestring : 'n/a') . '<br />';
    echo '$rate=' . ((isset($rate)) ? $rate : 'n/a') . '<br />';
    echo '$textdatetimepicker1=' . ((isset($textdatetimepicker1)) ? $textdatetimepicker1 : 'n/a') . '<br />';

    ?>

Output

    $loanid=123
    $loan_name=Bank Loan
    $description=FRB Bank Loan
    $amount=25000
    $datestring=n/a
    $rate=12.5
    $textdatetimepicker1=27/01/2016

Upvotes: 1

Related Questions