Ajax POST to PHP - how do I reply?


Joad 49

I have posted an email address from javascript to php via ajax. Then I searched the database in php to find out if the email exists in the database. How can I then send a message back to javascript/html saying that the value exists?

Here is what I am using to send the request:

 function postEmail(){

      var checkEmail = "[email protected]";

      var dataString = 'checkEmail1='+ checkEmail;

      // AJAX Code To Submit Form.
      $.ajax({
      type: "POST",
      url: "myfile.php",
      data: dataString,
      cache: false,
      success: function(result){

      alert("Sent successfully");
      } 
      });
 }

Then in PHP:

 $checkEmail2=$_POST['checkEmail1'];

 $results = mysql_query("select id from myTable where emailaddress='$checkEmail2' ");
 $row = mysql_num_rows($results);

 if ($row > 0 ) {
 echo "email already exists";
 } else {

 if ($row == 0 ) {
 echo "email doesnt exist";
 }

 }

Not sure if I should get the request? Or if you return a value or something. thanks.

(ps, I'm developing a hybrid app, so need to send/retrieve from PHP using JSON)

Mehani

I think you need to remove the second condition! Output json or any other format you want. I use json_encode for arrays

try:

  $checkEmail2=$_POST['checkEmail1'];

  $results = mysql_query("select id from myTable where    emailaddress='$checkEmail2' ");
 $row = mysql_num_rows($results);

 if ($row > 0 ) {
     echo "email already exists";
 } else {
    echo "email doesnt exist";
  }  

JAVASCRIPT

From your success function, print the result to the console to see the output

     console.log(result);

Related


Ajax POST to PHP - how do I reply?

Joad 49 I have posted an email address from javascript to php via ajax. Then I searched the database in php to find out if the email exists in the database. How can I then send a message back to javascript/html saying that the value exists? Here is what I am u

jQuery Ajax post html data to php and get reply

Relm I want to post span data like below, but nothing happens when clicked. I posted "4" in the following range. I don't post much about this or syntax. span. <span style="color:#B00" id="PidClick">4</span> use this. $('#PidClick').click(function() { console.

How do I get a reply from this site?

Bashir My question is simple, I would like to get an answer from: http://www.pulsant.com I just want to check if there is a redirection, for this I am using the following code: import urllib.request as Request import urllib.parse url = 'http://www.pulsant.com'

How do I send a reply by courier?

Amit Adhikari I've been trying out an express app that sends responses as a stream. var Readable = require('stream').Readable; var rs = Readable(); app.get('/report', function(req,res) { res.statusCode = 200; res.setHeader('Content-type', 'appli

How do I get a reply from this site?

Bashir My question is simple, I would like to get an answer from: http://www.pulsant.com I just want to check if there is a redirection, for this I am using the following code: import urllib.request as Request import urllib.parse url = 'http://www.pulsant.com'

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

Blog post comment reply with ajax

Oguz Demir I have a question, I need to create a comment reply action for my blog. public JsonResult ReplyComment(string replycomment, int articleid, int commentid) { var UserId = Session["UserId"]; if (replycomment == null) {

How to POST ajax to php

Include "breakDance" I can't seem to figure out how to post using ajax. I made a stupid table to try it out and even reducing it all the way down to only two values still doesn't work. My HTML is like this: <html> <head> <script type="text/javascript" src="j.j

How do you post a form using ajax in php?

maximum learning I'm trying to submit a form using ajax, but when it succeeds I set it, a jquery alert should show but it doesn't, so I think it's unsuccessful. Anyone know why? ? This is the form I am trying to submit with ajax <b>Product Title: </b><br><inpu

How do I split the success output of an Ajax post?

tacomaba I have an ajax post to a PHP file which returns a table on success. I use the entire success output to populate a div in the main HTML page. this is very simple. However, sometimes the result table is large and it takes a long time or fails to display

How do i connect my form to ajax and post through it

Omega my html: {% extends 'base.html' %} {% block content %}* <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create a Recipe</title> <form class="" action="index.html" method="post"> <br>

How do i connect my form to ajax and post through it

Omega my html: {% extends 'base.html' %} {% block content %}* <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create a Recipe</title> <form class="" action="index.html" method="post"> <br>

How do I use ajax to post XML data to the server?

juanma This is exactly the data I want to pass to the server: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.o

How do I make a $.ajax POST call to use SSL?

User 3809638 Here is my JavaScript: parameter = "name=" + name + "&email=" + email + "&phone=" + phone + "&comments=" + comments; $.ajax({ url: 'sendEmail.php?' + parameter, success: function(data) { if (data.indexOf("Message sent") > 0) { ale

How do i connect my form to ajax and post through it

Omega my html: {% extends 'base.html' %} {% block content %}* <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create a Recipe</title> <form class="" action="index.html" method="post"> <br>

How do i connect my form to ajax and post through it

Omega my html: {% extends 'base.html' %} {% block content %}* <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create a Recipe</title> <form class="" action="index.html" method="post"> <br>

How do i connect my form to ajax and post through it

Omega my html: {% extends 'base.html' %} {% block content %}* <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create a Recipe</title> <form class="" action="index.html" method="post"> <br>

How do I reply to messages other than the channel I typed in?

Phase N So I created a command that should reply to the user's message. Here is my code so far (check_perms() is a separate function that checks if the user has the proper permissions to run the command): @client.command() async def reply(ctx, message_id:disco

How do I reply to messages other than the channel I typed in?

Phase N So I created a command that should reply to the user's message. Here is my code so far (check_perms() is a separate function that checks if the user has the proper permissions to run the command): @client.command() async def reply(ctx, message_id:disco

How do I reply to messages other than the channel I typed in?

Phase N So I created a command that should reply to the user's message. Here is my code so far (check_perms() is a separate function that checks if the user has the proper permissions to run the command): @client.command() async def reply(ctx, message_id:disco

How do I reply to messages other than the channel I typed in?

Phase N So I created a command that should reply to the user's message. Here is my code so far (check_perms() is a separate function that checks if the user has the proper permissions to run the command): @client.command() async def reply(ctx, message_id:disco