0

I writing a registration/login form, I am sending user info via POST to a PHP that is looking in a DB. I would like that the PHP returns an ok or wrong value to the js and I don't now how to do it. Here my js:

    ui.onClick_regsubmit=function()
{
var fname=document.getElementById('fname').value;
var lname=document.getElementById('lname').value;
var password=document.getElementById('password').value;
var mail=document.getElementById('mail').value;
var affiliation=document.getElementById('affiliation').value;
var data = new FormData();
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
data.append("fname",fname);
data.append("lname",lname);
data.append("password",password);
data.append("mail",mail);
data.append("affiliation",affiliation);
xhr.open( 'post', 'PHP/registration.php', false );
xhr.send(data);
window.alert(affiliation);
}

And the php:

<?php
     mysql_connect('localhost','root','') or die('Cannot connect mysql server');
       mysql_select_db('ChemAlive_login')  or die('cannot connect database');

   $lname=$_POST['lname'];
   $fname=$_POST['fname'];
  $password=$_POST['password'];
  $mail=$_POST['mail'];
  $affiliation=$_POST['affiliation'];

  $q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
  $n=mysql_fetch_row($q);
  if($n>0)
{
  $q=mysql_query("select password  from login where mail='".$mail."' ");
  $pp=mysql_fetch_row($q);
  if($pp[0]=$password) echo "ok";
  else echo "wrong";
}
else
{   $insert=mysql_query("insert into login values('".$fname."','".$lname."','".$mail."','".$password."','".$affiliation."')") or die(mysql_error());}
?>

I would like to return to js this ok or wrong value. How to do it?

3 Answers 3

1
xhr.onload=function()
{
 if (xhr.status==200) 
 {
  alert(xhr.response);
 }else
 {
  alert("unknown server error");
 }
}

it will be better if the server sends a response code, and javascript will transfer this code to the text. For example:

onload=function()
{
 switch(xhr.response)
 {
  case "0":{alert("unknown error")};break;
  case "1":{alert("email is already used")};break;
  ...
 }
}

I think thought it is clear

Sign up to request clarification or add additional context in comments.

Comments

0

I do not have the rep to comment or I'd ask for details, but if you can consider using ajax, it could look something like this:

php:

$doit = //your query;
if($doit){
$youdid = 'ok';
}
else{
exit('1');
}

js:

$(document).ready(function () {

            var foo = $("#formfield")val();
            $.ajax({
                "foo":foo;
                type: 'POST',
                url: 'some.php',
                success: function(responseText) { 
if(responseText == "1") {
alert("Leave instantly");
};

}
else {
alert("One of us");
}

Comments

0

If you want to return either ok or wrong to the JavaScript to handle you could do something like this in your registration.php page:

 $q=mysql_query("select password  from login where mail='".$mail."' ");
 $pp=mysql_fetch_row($q);
   if($pp[0]=$password){ 
     header('Content-Type: application/json');
         echo json_encode(array('password' => 'ok'));
   }else{ 
     header('Content-Type: application/json');
         echo json_encode(array('password' => 'wrong'));
   }

I have not fully testing this, but the idea is to set the header to return json and then send it a JSON string.

Does that make sense?

Like I said in my comment below I have only used jQuery for AJAX. But here is a little something of what I know about XMLHttpRequest and my undertsanding of how you would test what you get back.

You can set up a listener for when you get a response back onreadystatechange and then put the response in a variable var pass = xhr.response and then just output the text to an alert box like alert(pass.password).

if (xhr.onreadystatechange === 4 && xhr.status === 200){
    var pass = xhr.response;
    //should output either ok or wrong
    alert(pass.password);
}

Read more about XMLHttpRequest here

Let me know if that works.

2 Comments

How shoud I modify the js too test this json array password?
Well to be honest I have only ever made an AJAX request using jQuery and the $.ajax method. This method gives you the ability to run a function on success, failure, and/or completion. Using this method a way to test it would be success: function (json) { alert(json.password); }, I could put an example of a full jQuery ajax request if that would be helpful? api.jquery.com/jquery.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.