大家好,我有这段代码可以在一个表中显示mysql数据,我想要从单元格中获取数据,并使用ajax请求将数据发布到php文件中,否,检索到的数据将显示在段落标签中,仅用于测试。当我获取单元格数据并将其发布到工作中的警报时。
我在做什么错
test.php
if(isset($_POST['searchbox'])){
$bloodonation =$_POST['searchbox'];
$multiple= explode(',',$bloodonation);
$var1 = $multiple[0]; // firstname
$var2 = $multiple[1]; // fathername
$var3 = $multiple[2]; // lastname /*bloodtype.blood_type='$var4' AND bodytype.bodytype='$var5' AND */
$_SESSION["firstname"] = $var1;
$_SESSION["fathername"] = $var2;
$_SESSION["lastname"] = $var3;
if(!empty($bloodonation)){
//$myfile = fopen("file.txt", "w");
//file_put_contents('file.txt',$bloodonation);
//fclose($myfile);
$bloodquery ="SELECT d.firstname AS donnerfirstname,d.fathername AS donnerfathername,d.lastname AS donnerlastname,bloodtype.blood_type,bodytype.bodytype,MAX(d.bloodonation_date)
FROM personprofile d,personprofile r,bloodtype,bodytype
WHERE r.firstname = '$var1' AND r.fathername='$var2' AND r.lastname= '$var3' AND r.bloodtype=d.bloodtype
AND d.hascancer='No' AND d.chronicdisease='No' AND d.autoimmunedisease='No' AND d.substanceabuse=1
AND d.hospitaladmission=134 AND d.health_issues='No'";
//$sql = "SELECT `firstname`, `fathername`, `lastname` FROM `personprofile` WHERE chronicdisease=\"No\" AND hascancer=\"No\" AND autoimmunedisease=\"No\"";
$bloodqr=mysqli_query($link,$bloodquery);
echo "<table>";
echo "<tr><th>Firstname</th><th>Fathername</th><th>Lastname</th><th>Blood type</th><th>Body type</th></tr> ";
while($row=mysqli_fetch_assoc($bloodqr)){
echo"<tr><td id='dfirstname'>";
echo $row['donnerfirstname'];
echo "</td><td id='dfathername'>";
echo $row['donnerfathername'];
echo "</td><td id='dlastname'>";
echo $row['donnerlastname'];
echo "</td><td id='dbloodtype'>";
echo $row['blood_type'];
echo "</td><td id='dbodytype'>";
echo $row['bodytype'];
echo "</td><td>";?><html><button onclick="outputdata()">Send Email</button></html> <?php
echo"</td></tr>";
}
}
} ?>
<!DOCTYPE html>
<html>
<head>
<script>
var donorfirstname = document.getElementById("dfirstname");
var dfn = donorfirstname.innerHTML;
var donorfathername = document.getElementById("dfathername");
var dfan = donorfathername.innerHTML;
var donorlastname = document.getElementById("dlastname");
var dln= donorlastname.innerHTML;
var donorbloodtype = document.getElementById("dbloodtype");
var dbt=donorbloodtype.innerHTML;
var donorbodytype = document.getElementById("dbodytype");
var dbot= donorbodytype.innerHTML;
function outputdata() {
$.ajax({
type: 'POST',
url: 'mytest.php',
data: {dofirstname: dfn,dofathername:dfan,dolastname:dln,dobloodtype:dbt,dobodytype:dbot},
success: function(data) {
$("#demo").html(data);}
});
//alert(dfn+dfan);//this works when uncommented
}
</script> </head>
<body>
<p id="demo"></p>
</body>
</html>
mytest.php
答案 0 :(得分:0)
Uncaught ReferenceError: $ is not defined
如果您不包括jquery,则会导致此错误。在显示的代码中,我没有看到jquery的参考。
如果您不想使用jQuery,则必须将纯Javascirpt用于您的ajax请求。
请参阅https://www.w3schools.com/xml/xml_http.asp,https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp