0

Basically, I'm trying to call a Javascript function inside a PHP script when using Ajax.

JavaScript:

<script type="text/javascript">
    function Validate() {
        hr = new XMLHttpRequest();
        name = document.getElementById('name').value;

        hr.open('POST', 'validator.php', true);
        hr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        hr.onreadystatechange = function() {
            if ( hr.readyState == 4 && hr.status == 200 ) {
                document.getElementById('message').innerHTML = hr.responseText;
            }
        }
        hr.send('name=' + name);
    }

    function disable() {
        document.getElementById('message').innerHTML = '';
    }
</script>

HTML:

<div id="message"></div>
Name: <input type="text" id="name /">
<input type="button" onclick="Validate();" value="Validate" />

PHP:

<?php    
   $name = $_POST['name'];    
   if ( !empty( $name ) ) {
       if ( $name == 'Tom' ) {
           echo "<script>alert('Hello Tom, Welcome Back')</script>";
       } else {
           echo 'You are not Tom';
       }
   } else {
       echo 'Please enter a name.';
   }
?>

Everything works fine except calling the Javascript function inside PHP echo <script>alert()</script>

What I think the problem here is because I declared hr.responseText, as a result, the javascript I want to show has returned into text. But what should I do to solve this problem?

Any help would be appreciated.

3 Answers 3

0

Try changing your echo from "<script>alert('Hello Tom, Welcome Back')</script>";

to just 'Hello Tom, Welcome Back';

Then in your javascript you can call alert(hr.responseText);

You will have to change your javascript to check for what is returned so you know to call either the alert or the document.getElementById('message').innerHTML = hr.responseText;

EDIT: I will add all the changed code...

<?php

$name = $_POST['name'];

if(!empty($name)){
  if($name == 'Tom'){
   echo 'Hello Tom, Welcome Back';
}else{
    echo 'You are not Tom';
}
}else{
  echo 'Please enter a name.';
}
 ?>

Javascript

<script type="text/javascript">
function Validate(){
   hr = new XMLHttpRequest();
   name = document.getElementById('name').value;

hr.open('POST', 'validator.php', true);
hr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

hr.onreadystatechange = function(){
  if(hr.readyState == 4 && hr.status == 200){
   response = hr.responseText.replace(/^\s+|\s+$/g,'');  //remove whitespace so you can compare
   if (response == ("You are not Tom") || response == ("Please enter a name."))
     {
     document.getElementById('message').innerHTML = hr.responseText;
     }
     else {  
       alert(hr.responseText);
    }
    }
}
hr.send('name=' + name);
}

function disable(){
   document.getElementById('message').innerHTML = '';
}
</script>
1
  • Thanks! I actually figured it out myself! Instead of response == ('You are not Tom'), I simply echo 'ok' when $name=='Tom' in PHP, then in Javascript if(hr.response == 'ok') then execute alert() Commented Aug 9, 2013 at 3:02
0

Try this:

   function do_alert($msg){
            echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
   }
   if(!empty($name)){
         if($name == 'Tom'){
             do_alert("You are tom");
          }else{
             do_alert("You are not tom");
          }
  }else{
         echo 'Please enter a name.';
   }        
4
  • This don't work, only 'Please enter a name' has returned when nothing is entered. Commented Aug 9, 2013 at 0:33
  • nothing is entered ? you mean $name is empty? because if that is the case then it will probably echo that because nothing is entered right? Commented Aug 9, 2013 at 0:36
  • yes, when $name is empty. But when $name == 'Tom' the alert won't show up Commented Aug 9, 2013 at 0:38
  • It's not working for me either. you could try to validate it inside your function validate if you want to. Commented Aug 9, 2013 at 1:02
0

You didn't tag jQuery, but if you are using it, $.getScript might be what you want.

Aside from that, it might be better to build the script into your original document, and then execute this or that function based on the response text.

3
  • but I would like all validation to be done on server side, since it's more secured? Edited: I believe I didn't use any jQuery here :/? Commented Aug 9, 2013 at 0:35
  • The way I imagine it, it still would be. Your response would be different; the validation would be the same. What happens on the client would be the way you handle the response. Commented Aug 9, 2013 at 0:38
  • OK, forget jQuery. ;-) Commented Aug 9, 2013 at 0:39

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.