dongyi0114 2018-11-02 14:00
浏览 57
已采纳

ajax请求不向php页面发送请求

Hello guys I have this code that displays mysql data in a table,which it does, I want to take the data from the cells,which it does, and use an ajax request to post the data to a php file,which it doesn't, and the data retrieved to be displayed in a paragraph tag,just for testing. When I take the cell data and post it to an alert in works.

What am I doing wrong
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

  • 写回答

1条回答 默认 最新

  • doushi1996 2018-11-02 17:15
    关注

    Uncaught ReferenceError: $ is not defined this error caused if you didn't include the jquery. In the code you showed, i didn't see the reference for jquery.
    If you don't want to use jQuery then you have to do with the pure Javascirpt for your ajax request.
    Refer https://www.w3schools.com/xml/xml_http.asp, https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题