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

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题