检查数据库连接后,按钮是点击数据库连接、按钮

2023-09-10 22:16:28 作者:那一年、迩笑靥如花

我建立一个网站,有一个预约。我用ajax从形式到另一个网页,其中插入信息到我的数据库发送信息。为了更加有用,我希望做一个测试连接到我的数据库。当用户填写了所有的领域需要的,他会点击提交按钮和一个测试连接必须在发送数据之前先检查。而当连接失败,它会告诉未设置连接的用户(也许他的网络连接丢失,或服务器本身是不及格)。这样一来,该网站prevents提示用户,他们的预约数据发送,但实际上并非如此。

I am building a website that has a reservation. I use ajax to send information from forms into the another page which insert the information into my database. To make it even useful, I want to make a test connection into my database. After a user fills up all the fields required he will click the submit button and a test connection must check first before sending the data. And when the connection fails, it will tell the user that the connection is not set(maybe his internet connection is lost, or the server itself is failing). In that way, the website prevents prompting the user that their reservation data is sent, but actually NOT.

编辑:

下面是我的跑步和固定的code:

Here's my running and fixed code:

$("#esubmit2").click(function(){
    var event2 = document.getElementsByName("eevent2")[0].value;
    var engager2 = document.getElementsByName("eengager2")[0].value;
    var contact2 = document.getElementsByName("econtact2")[0].value;
    var eadd2 = document.getElementsByName("eeadd2")[0].value;
    var venue2 = document.getElementsByName("evenue2")[0].value;
    var datein2 = document.getElementsByName("edatein2")[0].value;
    var message2 = document.getElementsByName("emessage2")[0].value;
    var reg2 = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    if(event2 == "" || event2 == " "){
        $("#eevent2").focus();
        return false;
    }
    else if(engager2 == "" || engager2 == " "){
        $("#eengager2").focus();
        return false;
    }
    else if(contact2 == "" || contact2 == " "){
        $("#econtact2").focus();
        return false;
    }
    else if(venue2 == "Venue:"){
        $("#evenue2").focus();
        return false;
    }
    else if(datein2 == "" || datein2 == " "){
        $("#edatein2").focus();
        return false;
    }
    else if(message2 == "" || datein2 == " "){
        $("#emessage2").focus();
        return false;
    }
    else if(eadd2 != ""){
        if(reg2.test(eadd2) == false){
            $("#eeadd2").focus();
            $("#eeadd2").css("backgroundColor","#800517");
            $("#eeadd2").css("color","#FFFFFF");
            return false;
        }
        else{
            sendreserve_event2(); // call function sendreserve()
            return false;
        }
    }
    else{
        sendreserve_event2(); // call function sendreserve()
        return false;
    }
})


function sendreserve_event2(){ // send informations to database
    var data_string = $("form#eform2").serialize(); // IMPORTANT

    $.ajax({
        type:       "POST",
        url:        "submitreserve_eventpc.php",
        data:       data_string,
        success:    function(json) {

        if(json == 1){
            $("#eevent2").val("");
            $("#eengager2").val("");
            $("#econtact2").val("");
            $("#eeadd2").val("");
            $("#evenue2").val("Venue:");
            $("#edatein2").val("");
            $("#emessage2").val("");

            $("#eeadd2").css("backgroundColor","#FFFFFF");
            $("#eeadd2").css("color","#555");

            alert("Reservation Successful!!! \n\nPlease wait for your reservation code to be send to your e-mail account or contact number.\n\nThank you!");

            return false;
        }
        else{
            alert("Sorry for the inconvenience but the connection to our database failed.\nPlease check you internet connection or refresh you page.\n\nIf one of the above failed please report to our admin.\nThank You.");
            return false;
        }


        }//end success function


    }); //end ajax call
    return false; // IMPORTANT
}

submitreserve_eventpc.php:

submitreserve_eventpc.php:

if($test){
mysql_query("INSERT INTO tblevent(eventName,engager,contactNumber,emailAdd,venue,checkinDate,message) VALUES('$event2','$engager2','$contact2','$eadd2','$venue2','$datein2','$message2')");
    $ok = 1;
}
else{
    $ok = 0;
}

echo json_encode($ok);

如果有,你看,请编辑任何改善。对于现在这个满足我的需求:)

If there's any improvement that you see please edit. For now this met my needs :)

推荐答案

您应该做这样的事情。

您php.file

 <?php
   $con=mysql_connect('localhost','root','root');
   if($con){
    // do insertion  data here into the database
    $sql = "Insert into table query";
    if($sql){
     echo "Data inserted successfully";
    }else{
     echo "Sorry! some error occured ".mysql_error(); 
    }
   }else{
    echo "Unable to connect to the server";
   }
  ?>
 
精彩推荐