-1

My problem is very similar to javascript inside php code

I am trying to embed Javascript code inside PHP. But Javascript code doesn't seem to work. My sample code is as below. I am trying to run is on my local XAMPP server. If i remove Java script code, rest of PHP code works perfectly fine.

    <?php
    session_start();
    include('connection.php');
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $emailID=$_POST['emailID'];
    $pwd=$_POST['pwd'];
    $phone=$_POST['phone'];

$query = mysql_query("SELECT * FROM user_info WHERE uname = '$emailID' AND pwd = '$pwd'") or die(mysql_error());

if(!$row = mysql_fetch_array($query) or die(mysql_error()))
{
        mysql_query("INSERT INTO user_info(fname, lname, uname, pwd, phone)VALUES('$fname', '$lname', '$emailID', '$pwd', '$phone')") or die(mysql_error());
?>
        <script type="text/javascript" >
            window.parent.modalWin.CallCallingWindowFunction(0, 'User registration successful');
        </script>   
<?php
        mysql_close($con);
}
else
{

?>
    <script type="text/javascript">
            alert("E-mail is already registered");
    </script>
<?php   
    header("location: registration.html");
}
?>
3
  • "doesn't seem to work" - What does it do? Do you get a server-side error in the PHP? Does it serve the page to the browser and then you get a JS error in the browser console? Or...? Commented Dec 1, 2013 at 10:06
  • try replacing the script blocks with "User registration successful" and "E-mail is already registered". Do they appear on the page? Commented Dec 1, 2013 at 10:08
  • After some more debugging, i realized after the script, if i try to re-direct, the alert message doesn't come up. echo '<script type="text/javascript">alert("Alredy registered");</script>'; header("location: registration.html"); Commented Dec 1, 2013 at 11:44

1 Answer 1

0

Try directly in you php like this

<?php 


echo '<script type="text/javascript">alert("Data alert");</script>';
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the Answer, it works fine, but after the script, if i try to re-direct, the alert message doesn't come up. echo '<script type="text/javascript">alert("Alredy registered");</script>'; header("location: registration.html");

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.