1

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.

2 Answers 2

1

Use jQuery serialize function to pass your values.

  $.ajax({
    type: "POST",
    url: "save_reasons.php",
    data: $('#myForm').serialize(),
    success: function(){
    alert('yay');
     }
  });
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, how would I use it on the php page? (I'm very new to AJAX)
it's ok, sussed it, thought I had to somehow unserialize it the data before I could use it but a bit of research shows not, thanks for a simple solution!
1

Send your data as json.

var myobj = { this: 'that' };
$.ajax({
  url: "my.php",
  data: JSON.stringify(myobj),
  processData: false,
  dataType: "json",
  success:function(a) { },
  error:function() {}
});

On your server side script

<?php
  $array = json_decode(file_get_contents("php://input"), true);
?>

4 Comments

Passing true as a second argument to json_decode makes it return an array, so casting it to an array is not needed.
var myobj = { this: 'that' }; is actually a json objects where you store the javascript arrays for processing. In this case it will be interpreted in php like array('this' => 'that');
so I don't have to change that with my own values?
Use this $(this).serializeArray() to serialize your array to json so you can passit as json string. var dataString = $('#yourfield').serializeArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.