简体   繁体   中英

AJAX Passing Form Data

I am trying to figure out why this is not working. Here is the sub-set of my form that looks like this:

   <input type="hidden" id="gift_id3" name="giver3gift_id" value="925"></td>
   <tr id="tr3">
       <td>
            <select class="type" id="type3" name="giver3paymenttype_val">
             <option value="1" SELECTED>Check</option>
             <option value="2">Cash</option>
             <option value="3">ACH</option>
             <option value="4">In Kind Donation</option>
          </select>
       </td>
       <td><input type="text" class="refnum" id="refnum3" name="giver3ref_num" value="2147483647"></td>
       <td><input type="text" class="amount" id="amount3" name="giver3amount" value="25.00" onBlur="this.value=formatCurrency(this.value)"></td>
       <td>
            <select class="type" id="type3" name="giver3taracct_val">
             <option value="1" SELECTED>General Fund</option>
             <option value="2">Building Fund</option>
             <option value="3">Missions Fund</option>
          </select>
       </td>
       <td><input type="checkbox" id="void3" name="giver3void">
   </tr>

Here is my AJAX:

var $inputs = $("#dialog-editgiving :input");
var parameters = [];                  

$inputs.each(function() {
   parameters[this.name] = $(this).val();
});

$.ajax({
   url : "./scripts/form_process/update_giving.php",
   type: "POST",
   data : {parameters:parameters},
   success: function(data, textStatus, jqXHR)
         {
           alert(data);
         },
   error: function(jqXHR, textStatus, errorThrown)
         {
           alert(errorThrown);
          }
});

Here is my update_giving.php file

<?php

foreach ($_POST as $key => $value) {
 echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}

?>

When I look through the $inputs array, I get the the values that are submitted in the form. Then it is handed off to the "./scripts/form_process/update_giving.php" script, I get nothing in the POST object.

Do I have something incorrect in my AJAX request?

instead of this code

var $inputs = $("#dialog-editgiving :input");
var parameters = [];                  

$inputs.each(function() {
   parameters[this.name] = $(this).val();
});

use

var paramerter = $( '#dialog-editgiving' ).serialize();

for fetching all input from the form..

and fetch data in update_giving.php file as yoy fetch in normal php function for example

$_POST['giver3taracct_val'];

i hope this code will help you.

Try to execute the code at form submit and serialize all the data using serialize()

try something like this:

$('form').submit(function(e){
e.preventDefault();

var data = $(this).serialize();

$.ajax({
   url : "./scripts/form_process/update_giving.php",
   type: "POST",
   data : data,
   success: function(data, textStatus, jqXHR)
         {
           alert(data);
         },
   error: function(jqXHR, textStatus, errorThrown)
         {
           alert(errorThrown);
          }
});

for use the register page

<script>
    function my_user(user_name){
          var dataString = 'user_name='+ user_name +'&tbl_name='+ tbl_name+'&valid_user='+ valid_user;
            $.ajax({
                type: "POST",
                url: "user_ajax.php",
                data: dataString,
                cache: false,
                success: function(data) {
                 if(data == 1){
                       $("#user_exits").html("User Name Already Exist!");
                       $('#btn_submit').addClass("disabled");
                       //$("#user").focus();
                  }
                  else if(data == 0){
                    $("#user_exits").addClass("hide");
                    $('#btn_submit').removeClass("disabled");
                  }
                }
            });
          }
</script>
<input required type="text" class="form-control" placeholder="User Name *" id="user" name="v_user_name" onblur="my_user(this.value)" value="<?php echo $user_name; ?>" autofocus >
//user_ajax.php

    <?php
      $page_no=$_POST['page_no'];
      $tbl_name=$_POST['tbl_name'];


      if($tbl_name=='tbl_user' && $_POST['user_name']!='' && $_POST['valid_user']=='user_exit'){

          $sql_user = "SELECT * FROM tbl_user WHERE v_user_name='".$_POST['user_name']."' ";
          $rsd_user = mysql_query($sql_user);
          $sql_user_exist = "SELECT * FROM tbl_user WHERE id ='".$_SESSION['usr_id']."' ";
          $rsd_exist_user = mysql_query($sql_user_exist);
          $row_user=mysql_fetch_array($rsd_exist_user);
          if($row_user['v_user_name']==$_POST['user_name']){
            echo $msg_user=0;
          }
          else{
            $msg_user = mysql_num_rows($rsd_user); //returns 0 if not already exist
            echo $msg_user;
          }
      }
    ?>

With the assistance of Rubby Smith's comment. I was able to make this code work. Thanks Everyone.

    var parameters = $("#dialog-editgiving :input").serialize();

    $.ajax({
         url : "./scripts/form_process/update_giving.php",
         type: "POST",
         data : {parameters:parameters},
         success: function(data, textStatus, jqXHR)
             {
                alert(data);
             },
         error: function(jqXHR, textStatus, errorThrown)
             {
                alert(errorThrown);
             }
     });

Using the following within the PHP script.

$params = array();
$form_fields = array_keys($HTTP_POST_VARS);
for ($i = 0; $i < sizeof($form_fields); $i++) {
    parse_str($HTTP_POST_VARS[$form_fields[$i]], $params);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM