Php 提交复选框ID和文本字段值,而不使用Ajax刷新浏览器
我是编程新手。我需要开发一个带有复选框和文本字段的评分系统,用户在其中单击列表中的主题,并在文本字段中添加他的评分/体验,如下图所示 这是我的HTML代码Php 提交复选框ID和文本字段值,而不使用Ajax刷新浏览器,php,ajax,wordpress,Php,Ajax,Wordpress,我是编程新手。我需要开发一个带有复选框和文本字段的评分系统,用户在其中单击列表中的主题,并在文本字段中添加他的评分/体验,如下图所示 这是我的HTML代码 <form action="" method="post"> <input type="checkbox" id="1" name="cb[1]" value="" onclick="document.getElementById('t1').disabled=!this.checked;" />
<form action="" method="post">
<input type="checkbox" id="1" name="cb[1]" value="" onclick="document.getElementById('t1').disabled=!this.checked;" />
<label for="1">Checkbox No. 1</label>
<input type="number" max="5" min="1" id="t1" name="t[1]" value="" disabled="disabled" /><br /><br />
<input type="checkbox" id="2" name="cb[2]" value="" onclick="document.getElementById('t2').disabled=!this.checked;"/>
<label for="2">Checkbox No. 2</label>
<input type="number" max="5" min="1"id="t2" name="t[2]" value="" disabled="disabled" /><br /><br />
<input type="checkbox" id="3" name="cb[3]" value="" onclick="document.getElementById('t3').disabled=!this.checked;"/>
<label for="3">Checkbox No. 3</label>
<input type="number" max="5" min="1"id="t3" name="t[3]" value="" disabled="disabled" /><br /><br />
<input type="checkbox" id="4" name="cb[4]" value="" onclick="document.getElementById('t4').disabled=!this.checked;"/>
<label for="4">Checkbox No. 4</label>
<input type="number" max="5" min="1"id="t4" name="t[4]" value="" disabled="disabled" /><br /><br />
<input name="submit" type="submit" value="Submit" />
</form>
复选框1
复选框2
复选框3
复选框4
这是我的php函数
global $usedTexts;
$usedTexts = array();
function postdata(){
if ( isset($_POST['submit']) && array_key_exists("t", $_POST) && is_array($_POST["t"]) && array_key_exists("cb", $_POST) && is_array($_POST["cb"])) {
$usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
foreach($usedTexts as $subjectId=>$subjectExp){
if($subjectExp!=null){
echo "This is checkbox id = " . $subjectId . " and This is text field value = " . $subjectExp . "<br />";
}
}
}
}
global$usedTexts;
$usedTexts=array();
函数postdata(){
如果(isset($POST['submit'])和数组($POST[“t”])和is_数组($POST[“t”])和数组($POST[“cb”])和is_数组($POST[“cb”]){
$usedTexts=array_intersect_key($_POST[“t”],$_POST[“cb”]);
foreach($usedText作为$subjectId=>$subjectExp){
如果($subjectExp!=null){
echo“这是复选框id=“.$subjectId.”,这是文本字段值=“.$subjectExp.”
;
}
}
}
}
我正在使用wordpress,我想提交复选框ID和文本字段值,而不使用Ajax刷新浏览器。我还想显示复选框id和值,如图所示。如果有人能为此提供ajax代码,我将不胜感激。谢谢:)单击submit按钮时,使用JQuery可防止表单的默认提交行为。它是这样的,但它是不完整的:
$('#form').submit(function(event){
event.preventDefault(); //This line prevents the default submit action of the id #form
//Put your AJAX code here that will submit your checkbox and textbox data
})
请参见单击submit按钮时,使用JQuery防止表单的默认提交行为。它是这样的,但它是不完整的:
$('#form').submit(function(event){
event.preventDefault(); //This line prevents the default submit action of the id #form
//Put your AJAX code here that will submit your checkbox and textbox data
})
请参见您可以使用XMLHttpRequest对象对其进行编码,或者使用JQuery是一种更简单但不必要的更好方法 然后你可以做这样的事情
$('form').submit(function(event) { //make sure you give your form an ID
event.preventDefault();
$.ajax({ // Initiate an ajax call
type: "POST", // You seem to want post HTTP call
url: "URLPATH/youphpcode.php",
dataType: "json", // this is the data type to return usually JSON
data: votes,//data to send USUALLY JSON or hashmap/array
success: function(d)
{
$('#displayMSG').HTML('Your Votes have been submitted') // Maybe display a message or error.
}
});
});
为了找出他们启用和选择了哪些字段,我在这里添加了一个脚本。
完整代码*
您可以使用XMLHttpRequest对象编写代码,或者使用JQuery是一种更简单、但不是必需的更好的方法 然后你可以做这样的事情
$('form').submit(function(event) { //make sure you give your form an ID
event.preventDefault();
$.ajax({ // Initiate an ajax call
type: "POST", // You seem to want post HTTP call
url: "URLPATH/youphpcode.php",
dataType: "json", // this is the data type to return usually JSON
data: votes,//data to send USUALLY JSON or hashmap/array
success: function(d)
{
$('#displayMSG').HTML('Your Votes have been submitted') // Maybe display a message or error.
}
});
});
为了找出他们启用和选择了哪些字段,我在这里添加了一个脚本。
完整代码*
嗨,谢谢你的密码。您能告诉我如何使用ajax将数据(复选框ID和值)作为关联数组发送到php函数吗。因为复选框ID和复选框名称是在foreach循环中从数据库中获取的。数据:{“checkbox1”:4,“checkbox2”:1}如果没有这个,我想使用关联数组将数据传递到函数hi,感谢代码。您能告诉我如何使用ajax将数据(复选框ID和值)作为关联数组发送到php函数吗。因为复选框ID和复选框名称是在foreach循环中从数据库中获取的。数据:{“checkbox1”:4,“checkbox2”:1}如果没有这个,我想使用关联数组将数据传递给函数