我有一个包含 ajax代码的html文件,使用POST方法将数据发送到我的php文件,但它似乎不起作用,因为php文件没有值。
我在这里缺少什么?
我的代码:
<!DOCTYPE html>
<html>
<body>
First Name: <img src="" name="fname" id="T1" type="text" value="Michael"/><br>
First Name: <img src="" name="fname" id="T2" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T3" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T4" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T5" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T6" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T7" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T8" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T9" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T10" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T11" type="text" value="Doug"/>
First Name: <img src="" name="fname" id="T12" type="text" value="Doug"/>
<p>Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".</p>
<button id="b1" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b2" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b3" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b4" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b5" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b6" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b7" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b8" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b9" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b10" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b11" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<button id="b12" name="button" onclick="xmlhttpsPost(this.id)">Try it</button>
<p id="demo"></p>
<script>
function xmlhttpsPost(id){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "code.php", true);
xhttp.send(myFunction(id));
}
function myFunction(id) {
var str = id;
var len = str.length;
var tempid = str.slice(1,len);
newid = tempid-1;
alert(str);
alert(newid);
var x = document.getElementsByName("fname")[newid].id;
return x;
}
</script>
</body>
</html>
我的php代码如下:
<?php
$servername = "localhost";
$username = "admin";
$password = "pwd";
$dbname = "testdb";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_error($conn));
}
$num= $_POST[x];
echo $num;
if ($num == "")
$num=2;
echo $num;
$sql = "INSERT INTO `test` (`disliked`) VALUES ('$num')";
if (mysqli_query($conn, $sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>