Friday, 18 April 2014

echo javascript with single quotes

You are echoing outside the body tag of your HTML. Put your echos there, and you should be fine.
Also, remove the onclick="alert()" from your submit. This is the cause for your first undefined message.
<?php
  $posted = false;
  if( $_POST ) {
    $posted = true;

    // Database stuff here...
    // $result = mysql_query( ... )
    $result = $_POST['name'] == "danny"; // Dummy result
  }
?>

<html>
  <head></head>
  <body>

  <?php
    if( $posted ) {
      if( $result ) 
        echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
      else
        echo "<script type='text/javascript'>alert('failed!')</script>";
    }
  ?>
    <form action="" method="post">
      Name:<input type="text" id="name" name="name"/>
      <input type="submit" value="submit" name="submit"/>
    </form>
</body>

Web Developer interview question Krawler Information Systems Pvt. Ltd

JAVASCRIPT: Difference between UNDEFINED and NULL Many of times we programmers often get bamboozzled between NULL and UNDEFINED. UNDE...