Pass an array to a php script using AJAX

tatty27

I have a form that I would like to submit without refreshing the page, I am new to AJAX and although I have managed this task passing a single value I'm not sure how to do this with an array.

The form input (displayed using a while loop)

<input type="hidden" name="user" value="<? echo $user_id; ?>" >
<input type="text" name="reason[][<? echo $row['reasonID']; ?>]"
                   size="25" value="<? echo $row['reason_name']; ?>"/>

<input type="submit" class="submit" value="Save"/>

The script

<script type="text/javascript" src="/js/jquery.js"></script>
<script>
  $(function() {
    $(".submit").click(function() {
    var dataString = '????';
    $.ajax({
    type: "POST",
    url: "save_reasons.php",
    data: dataString,
    success: function(){
    alert('yay');
  }
});

return false;
});
});
</script>

save_reasons.php

   if(isset($_POST['save_reasons'])){
      $user_id = $_POST['user'];
       foreach($_POST['reason'] as $item=>$value)
     {
   if(is_array($value)){
       foreach($value as $ID=>$reason)
        {
        $sql = "UPDATE gradeReason SET reason_name = '$reason' WHERE reasonID = $ID";
        $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
        }   
         }
      else{
        if($value !=''){
              $sql = "INSERT INTO gradeReason (reason_userID, category, reason_name) VALUES ($user_id, 'positioning', '$value')";
              $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
           }
        }
         } 
     }

After doing some research I think using the datastring is the way forward but I am not sure how to using this with an array (reason) and the userID (user) and then use it in the PHP script.

L105

Use jQuery serialize function to pass your values.

  $.ajax({
    type: "POST",
    url: "save_reasons.php",
    data: $('#myForm').serialize(),
    success: function(){
    alert('yay');
     }
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to pass javascript array to php using ajax

From Dev

using AJAX query to pass javascript array to php

From Dev

Pass html input array values to php script via ajax

From Dev

Pass variable to php script with AJAX

From Dev

How to pass Javascript array to PHP file using AJAX?

From Dev

Pass php array to Ajax variable

From Dev

Pass array using ajax into controller

From Dev

Calling a PHP script using ajax

From Dev

Calling a PHP script using ajax

From Dev

Pass base 64 string using ajax script

From Dev

Ajax - Manipulate a correct array to pass to PHP

From Dev

Pass PHP array to external jQuery $.ajax

From Dev

How to pass array from ajax to php?

From Dev

jQuery - How to pass an array with ajax to PHP?

From Dev

How to pass a PHP array to a BASH script?

From Dev

Pass string array to PHP script as POST

From Dev

Pass an array from a php script to another

From Dev

Pass an array over AJAX using JavaScript/JQuery

From Dev

Pass checkbox values into an array using ajax to jsp

From Dev

How to pass string array to controller using ajax?

From Dev

Unable to pass an array and a variable to server using $.ajax()

From Dev

How to pass the select box value as an array using AJAX from one page to another in PHP?

From Dev

Simple call to php script using AJAX not working

From Dev

Sending Information to a PHP Script using Ajax

From Dev

Calling a php script using ajax not working

From Dev

Using Javascript / Ajax to run a php script

From Dev

Simple call to php script using AJAX not working

From Dev

Sending FileList using Ajax to PHP script

From Dev

Using Ajax and JQuery to call PHP Script

Related Related

HotTag

Archive