本文介绍了通过AJAX将可变长度的数组传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难通过此表格/

<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>

我将如何使用Jquery/AJax发送此表单.我不想更新整个页面,所以到目前为止已经使用了此AJAX.

How would 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"

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

我将如何删除%5B和%5D或对其进行解码,以便重新获得方括号并将其变成要提交给SQL字符串的数组?

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?

推荐答案

您可以使用 json_decode()以在php中对其进行解码尽管我很好奇,为什么在发送整个表单并且已经将它们作为数组发送时为什么要序列化呢?

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

这篇关于通过AJAX将可变长度的数组传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 19:58