Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

successful/fail message pop up box after submit?

Tags:

javascript

php

Basically after clicking the submit button, I want a pop up box to pop up saying successful or fail, then clicking OK to confirm the message. At the moment i am getting a pop up box "undefined" followed by failed message pop up box. HELP PLEASE!

here is the script

<?php
include ('config.php');

if (isset($_POST['name'])) {

$name = "name";

$query = "INSERT INTO pop ('id','name') VALUES ('','$name')";
    $result = mysql_query($query,$cn);
    if ($result) {
    echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
else
{
    echo "<script type='text/javascript'>alert('failed!')</script>";
}
}       
?>

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

    <form action="" method="post">
    Name:<input type="text" id="name" name="name"/>
    <input type="submit" value="submit" name="submit" onclick="alert();"/>
    </form>
</body>

like image 457
munue Avatar asked Apr 27 '13 09:04

munue


2 Answers

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>
</html>
like image 112
DannyB Avatar answered Oct 02 '22 00:10

DannyB


Instead of using a submit button, try using a <button type="button">Submit</button>

You can then call a javascript function in the button, and after the alert popup is confirmed, you can manually submit the form with document.getElementById("form").submit(); ... so you'll need to name and id your form for that to work.

like image 31
half-fast Avatar answered Oct 02 '22 00:10

half-fast