0

I am trying to execute the below code which perfectly returns me an output from database.

<?php if(isset($_POST['submit']))
{
    $email = $_POST["Email_i"];
    $checkRe = mysql_query("select * from contact_form where email='$email'",$con);
   if(mysql_num_rows($checkRe)>0)
   {
      $check = 1;
   }
?>

I am trying to call a function using onSubmit event as follows;

<form action="#" method="post" name="myForm" onSubmit="return CheckForm()">
    <input type="submit" name="submit">
</form>

<script type="text/javascript">
function CheckForm()
{

    var calc =1;
    var Checkre = "<?php 
    $check="";
    echo $check; ?>";

   if(calc == Checkre)
   {
        alert('phone number exists');
        return false;
   }

}
</script>

the above function does not set the value of $check hence not resulting into its execution.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Jijo Nair
  • 828
  • 2
  • 11
  • 30
  • This code has a serious security problem. Never, ever, ever, (ever) pass unsanitized input from the browser to MySQL. You need to be using the `mysqli_*()` functions and prepared statements. – Will Dec 28 '15 at 08:39
  • 1
    PHP is hypertext pre-processor which is executed before the html page is rendered. Either use ajax or submit the form.. – Rayon Dec 28 '15 at 08:40
  • yes i will take that into note. I have been using these depreciated statements, but I will make the changes very fast. thank you! – Jijo Nair Dec 28 '15 at 08:40

1 Answers1

-1

instead of HTML FORMS, try to use and utilize jQuery AJAX.

Sample Code (where data is passed as object [or named array], dataType is declared for your api.php response, timeout is included for network error, error + success + complete functions):

$(document).ready(function(){
    $("#btnSubmit").click(function(){
        var x = $("#textbox").val();

        $.ajax({
            type: "POST",
            url: "url-to-php-api",
            data: { reference:x },
            dataType: "HTML-or-JSON-or-JSONP",
            timeout: 30000, //1 sec = 1000 ms
            error: function(x, t, m) {
                if (t === "timeout") {
                    alert("Network Connection Delayed.");
                    //this is for network error i.e.: connection delays
                }

                //and some other codes for other errors~
                //error function runs when there is some error with the jQuery AJAX syntax
            },
            success: function(retData) {
                alert("PHP RESPONSE: " + retData);
                //success function runs when the js successfully communicated, passed the values to PHP, gets result and back
            },
            complete: function() {
                alert("jQuery AJAX Function Complete.");
                //complete function runs after the process is completed, regardless of being error of successful
            }
        });
    });
});

this code is of course, as far as my personal coding is concerned

TheQuestioner
  • 702
  • 10
  • 28