开发者

Pass a variable length array, through AJAX to PHP

I am massive problems trying to pass this form/

<form id="form">
  <select id="<?php echo $answer['aid']; ?>" name="importance[<?php echo $answer['aid']; ?>]">                      
                                  <option value="0">NO</option>
                                  <option value="25">Unlikely</option>
                                  <option value="50" selected="selected">Neutral</option>
                                  <option value="75">Perhaps</option>
                                  <option value="100">YES</option>
                       </select>
<input 

type="submit" id="submit" value="Next"/>
</form>

How wou开发者_如何学Cld i use Jquery/AJax to send this form. I dont want to update the whole page so used this AJAX so far.

$('#form').submit(function() {
    alert('Submit button clicked with ServiceID =' + serviceID);  
    var impArray =  $('#form').serialize()
    JSONstring = JSON.stringify(impArray)
     alert(JSONstring);
     $.ajax({
           type: "POST",
           url: "update_KB.php",
           data: JSONstring, 
           success: function(msg){
             alert( "Data Saved: " + JSONstring );
           }
    });

It gives me a string a string that looks like this in the alert

"importance%5B101%5D=50&importance%5B100%5D=50&importance%5B99%5D=50&importance%5B98%5D=50"

how would i remove the %5B and %5D or decode it so that i get the square brackets back and turn it into an array to be submitted to an SQL string?


you can use json_decode() to decode it in php though I am curious why you want to serialize this when your sending your whole form and you are already sending them as arrays ?


You don't need to change %5D or %5B, you can get the params at update_KB.php from GET/POST by which you sending data. If you get any problem pls write the following code -

 print_r ($_POST); OR print_r ($_GET);

at your update_KB.php. Then you can decide what to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜