单击按钮后检查数据库连接

时间:2014-07-29 05:15:40

标签: php jquery ajax

我正在建立一个有预订的网站。我使用ajax将表单中的信息发送到另一个页面,该页面将信息插入到我的数据库中。为了使它更有用,我想在我的数据库中建立测试连接。用户填写所需的所有字段后,他将单击提交按钮,测试连接必须先检查,然后再发送数据。当连接失败时,它会告诉用户没有设置连接(可能是他的互联网连接丢失,或服务器本身失败)。这样,网站就会阻止提示用户发送他们的预订数据,但实际上并不是。

编辑:

这是我的运行和固定代码:

$("#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:

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);

如果您看到任何改进,请编辑。现在这满足了我的需求:)

2 个答案:

答案 0 :(得分:1)

你应该这样做。

你的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";
   }
  ?>

答案 1 :(得分:0)

你可以尝试这样的事情。

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

    $.ajax({
        type:       "POST",
        url:        "submitreserve_eventpc.php",
        data:       data_string
    }).done(function(data) {
        data = $.parseJSON(data);
        message = data.message;

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

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

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

        } else {
            console.log(message);
        }
    });
    return false; // IMPORTANT
}

在您的PHP文件中可以更改为以下内容。

$myDatabase = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$myDatabase) 
{
    $message = 'Could not connect: ' . mysql_error();
} else {
    $event2   = $_POST['eevent2'];
    $engager2 = $_POST['eengager2'];
    $contact2 = $_POST['econtact2'];
    $eadd2    = $_POST['eeadd2'];
    $venue2   = $_POST['evenue2'];
    $datein2  = $_POST['edatein2'];
    $message2 = $_POST['emessage2'];
    mysql_query("INSERT INTO tblevent(eventName,engager,contactNumber,emailAdd,venue,checkinDate,message) VALUES('$event2','$engager2','$contact2','$eadd2','$venue2','$datein2','$message‌​2')");
    $message = 'success';
}

$response = array('message' => $message);
echo json_encode($response); // This is the data that your AJAX function gets in .done