duangan6797 2014-01-22 23:49
浏览 32
已采纳

使用AJAX表单更新数据库

I have a file, form.php, that has a small form for searching the database for people by first and last name. The form uses a JavaScript function to send variables to search_name.php through AJAX and information queried from mydatabase as values in a form.

I want to be able to update the information on the form in the #result element with the results from the search.

I tried doing a small example that did no have the form returned through AJAX and it worked but for some reason I am not able to do it in my bigger project.

Can anyone please help. I have looked for examples and information but I'm new to AJAX and PHP and can't figure out why this is happening.

form.php

<script language="JavaScript" type="text/javascript">
    function ajax_post(){
        var fn = document.getElementById("first_name").value;
        var ln = document.getElementById("last_name").value;
        var errorMsg ="";
        if (fn==null || fn=="" ){
            errorMsg +="Enter First Name 
";
            document.getElementById("first_name").focus();
        }       
        if (ln==null || ln=="" ){
            errorMsg +="Enter Last Name 
";
            document.getElementById("last_name").focus();
        }       
        if(errorMsg != ""){
            alert(errorMsg);
            document.getElementById("first_name").focus();
            return false;       
        }else{ 
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "search_name.php";        
            var vars = "firstname="+fn+"&lastname="+ln;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("result").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            hr.send(vars); // Actually execute the request
            document.getElementById("result").innerHTML = "processing...";        
        }
    }
</script>
</head>
<body>
    <div class="left" id="search">
        First Name: <input id="first_name" name="first_name" type="text" /> 
        <br /><br />
        Last Name: <input id="last_name" name="last_name" type="text" />
        <br /><br />
        <input name="myBtn" type="submit" value="Search" onClick="javascript:ajax_post();return">
        <br /><br />
    </div>
    <div id="result"></div>

search_name.php

<?php $form_profile = '<form method="POST" action=""><table width="450px"><tr><td><label for="firstname" >First Name: </label></td><td><input  type="text" id="first_name" name="first_name" maxlength="50" size="30" value="'.$first_name.'"/></td></tr><tr><td><label for="lastname" >Last Name: </label></td><td><input  type="text" id="last_name" name="last_name" maxlength="50" size="30" value="'.$last_name.'"/></td></tr><tr><td><label for="headline">Headline</label></td><td><input  type="text" id= "headline" name="headline" maxlength="50" size="30" value="'.$profile_headline.'"/></td></tr></table><input type="submit" id="submit" name="submit" value="Save and Update"></form>'; ?>

<?php
//check if form has been submitted
if(isset($_POST['submit'])){

    $first_name= $_POST['first_name'];
    $last_name= $_POST['last_name'];
    $headline= $_POST['headline'];
    $summary= $_POST['summary'];


    $title_array= $_POST['title'];
    $company_array= $_POST['company'];
    $start_month_array= $_POST['start_month'];
    $start_year_array= $_POST['start_year'];
    $end_month_array= $_POST['end_month'];
    $end_year_array= $_POST['end_year'];

    if($first_name && $last_name){
        //connect to server
        $link = mysql_connect("localhost", "root", "########");

        if($link){
            mysql_select_db("mydatabase",$link);
        }


        //check if person exists
        $exists = mysql_query("SELECT * FROM profile WHERE firstname = '$first_name' AND lastname = '$last_name'") or die ("The query could not be complete.");
            if(mysql_num_rows($exists) != 0){
                //update
                mysql_query("UPDATE profile SET  headline='$headline' WHERE firstname = '$first_name' AND lastname = '$last_name'") or die("Update could not be applied");
                echo "Success!!";

}else echo "That alumni is not in the database";
}else echo "You must provide a first and last name.";


}

?>
  • 写回答

2条回答 默认 最新

  • douweng7308 2014-01-23 00:03
    关注

    Your javascript here:-

        var vars = "firstname="+fn+"&lastname="+ln;
    

    is not including "submit" which your PHP script requires to search the database, here:-

    if(isset($_POST['submit'])){
    

    So if you just add +"&submit=true" to the end of your vars variable, it should fix the given problem.

    var vars = "firstname="+fn+"&lastname="+ln+"&submit=true";
    

    Of course, you will see a lot of Undefined index warnings as your PHP script looks for lots of other variables that aren't sent initially =)

    Hope this is of some help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭