Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a simple form with two fields.i am checking the name already exists in database or not. if exits show message on span element name already exists.i used ajax call for this
in my php file i am returning name exits echo "yes" and name not exits echo "no".what i want is in success response i am using "yes" and "no" coming from php file file and show message on span element.But it is not working.Please help me anybody.This is my first example using ajax call jquery.Thanks in Advance.

What I have tried:

JavaScript
 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript">

function checkfirstname(element)
{
  var name=element.value;
  name=name.replace(/\s\s+/g, ' ');
  element.value =name.replace(/\w\S*/g, function(txt){return
  txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});

if(name)
{
   $.ajax({
   method: "POST",
   datatype:"text",
   url: "checkname.php",
   data: {
    firstname:name,
  },
   success: function (response) {

   if(response=="yes")
    {
     $( '#name_status' ).innerHTML("Name already exits");
    }
      else if(response=="no")
      {
       $( '#name_status' ).innerHTML("");
      }

      else
      {
       $('#name_status').html('Error');
      }
   }

   });

  }

}






HTML
<html>
<head>
</head>
<body>
  <?php error_reporting(0); ?>

  <?php include('connection2.php');?>
  <?php

  if(isset($_POST['firstname']))
  {
     $name=$_POST['firstname'];

     $checkdata="SELECT firstname FROM customer WHERE firstname='$name'";

     $query=mysqli_query($con,$checkdata);
      if(mysqli_errno($con)==0){
     if(mysqli_num_rows($query)>0)
     {
      echo "yes";
     }
     else
     {
      echo "no";
     }
    }
      else{
        echo mysqli_errno($con)."".mysqli_error($con);

      }
  }
  mysqli_close($con);

 ?>
</body>
</html>
Posted
Updated 5-Sep-17 7:10am
v2
Comments
Kornfeld Eliyahu Peter 5-Sep-17 7:44am    
go and debug your code - you will see what 'response' is and how to handle it... (reading jQuery documentation may help too)

1 solution

Look at the response returned from your AJAX request. It is not simply the string "yes" or "no"; it includes the <html>, <head>, and <body> tags that you've declared on that page.

The string "<html><head></head><body>yes</body></html>" is clearly not equal to the string "yes"!

Remove those tags from your checkname page. You'll also want to remove any white-space from outside of the <?php ... ?> markup.

You'll also want to fix the SQL Injection[^] vulnerability in your code.

The page should look something like this:
PHP
<?php
error_reporting(0);
include('connection2.php');

if(isset($_POST['firstname']))
{
    $name = $_POST['firstname'];
    $checkdata = "SELECT firstname FROM customer WHERE firstname = ?";
    
    $stmt = mysqli_prepare($con, $checkdata);
    mysqli_stmt_bind_param($stmt, 's', $name);
    
    mysqli_stmt_execute($stmt);
    $rowCount = mysqli_stmt_affected_rows($stmt);
    mysqli_stmt_close($stmt);
    
    if ($rowCount > 0) {
        echo "yes";
    }
    else {
        echo "no";
    }
}

mysqli_close($con);
?>


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
 
Share this answer
 
Comments
Member 13153537 27-Sep-17 3:08am    
Thanks for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900