AlexJaa
AlexJaa

Reputation: 389

check database connection after button is click

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.

EDITED:

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:

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

Upvotes: 0

Views: 1748

Answers (2)

Matt
Matt

Reputation: 1590

You could try something like this.

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
}

In your PHP file could be changed to what is below.

$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

Upvotes: 0

Sandeep Pal
Sandeep Pal

Reputation: 2185

You should do something like this.

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

Upvotes: 1

Related Questions