好吧,我有一對單選按鈕將它們的值存儲到數據庫中,以便在用戶未來返回到網站時保留其狀態。問題是,無論用戶選擇按鈕1(like)還是按鈕2(不喜歡),該值總是返回。任何人都可以幫我弄清楚爲什麼討厭不被退回?AJAX單選按鈕不起作用
這裏是我的form.php的:
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var entered = document.getElementById('entered').value;
var queryString = "?entered=" + entered;
ajaxRequest.open("GET", "check.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name="myform" action="check.php" method="post">
<fieldset>
<legend>Posts</legend>
<div id="post_1" class="post">
<b>Post #1</b><br>
Content of post #1<br>
<p><input type="radio" id="entered" name="like_1" value="like" onclick="ajaxFunction();" onchange="ajaxFunction();" /><label for="like1a">Like</label></p> <p><input type="radio" id="entered" name="like_1" value="dislike" onclick="ajaxFunction();" onchange="ajaxFunction();" /><label for="like1b"> Dislike</label></p>
</div>
</fieldset>
</form>
<div id='ajaxDiv'>Your result will display here</div>
,這是check.php:
<?php
// Retrieve data from Query String
$entered = $_GET['entered'];
// Escape User Input to help prevent SQL Injection
$entered = mysql_real_escape_string($entered);
echo $entered;
?>
所以進入只存儲 「喜歡」 沒有哪個單選按鈕被選中,此事基本上$改變選擇應該改變存儲的值,但這也不會發生。我錯過了什麼嗎?
這是一般不宜有一個單選按鈕提交的東西,你應該讓一個普通按鈕。 – Maz
此外,您應該爲您的站點添加CSRF保護,如果它將被部署。 – Maz
只允許用戶選擇喜歡或不喜歡,而不是兩者。據我所知,只有單選按鈕可以做到這一點。 – Sweepster