2

I have an insert form where I can submit data into my database.

Here's my code:

<form action="insert_backend.php" method="POST" enctype="multipart/form-data">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label for="uploadedimage">Small image to upload: </label>
<input type="file" name="uploadedimage" id="uploadedimage"/><br />
<label>Date:</label>
<input class="input" name="date" type="text" value=""><br />
<label>Retrace:</label>
<input class="input" name="retrace" type="text" value=""><br />
<label>Start of Swing Trade:</label>
<input class="input" name="start_of_swing_trade" type="text" value=""><br />
<label>End of Swing Trade:</label>
<input class="input" name="end_of_swing_trade" type="text" value=""><br />
<label>Bull flag:</label>
<input class="input" name="bull_flag" type="text" value=""><br />
<label>Bear flag:</label>
<input class="input" name="bear_flag" type="text" value=""><br />
<label>EMA Crossover:</label>
<input class="input" name="ema_crossover" type="text" value=""><br />
<label>Trading Instrument:</label>
<input class="input" name="trading_instrument" type="text" value=""><br />
<input class="submit" name="submit" type="submit" value="Insert">
</form>

Here's a snippet of the "Success" message on the php that is used to process the data submitted to the database:

$stmt->bind_param('sssssssss',$target_path, $date, $retrace, $start_of_swing_trade, $end_of_swing_trade, $bull_flag, $bear_flag, $ema_crossover, $trading_instrument);
         if(!$stmt){ exit("bind failed");}
        //will return 0 if fail
        if($stmt->execute() != 0){

            echo "New record created successfully";
        }else{ echo "Failed to insert new record";}

Whenever I Insert data, I get the "New record created successfully" message on the insert_backend.php page, which is the php file used to process and submit the data, but not on the actual form page used to insert data. Usually when you use a form to send a message to someone or to send data into a database, the form resets itself and you get a "success" message that shows at the bottom of the form without getting redirected to another page. But the code I have redirects me to the page that is used to process the data. how do I get it to show the "success" message on the same page as the form itself?

My FULL insert form code (with the php redirect check solution in it):

<!DOCTYPE html>
<html>
<head>
<title>Chart Submission Form</title>
<link href="insert.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Insert Data In Database Using PHP.</h2>
</div>
<form action="insert_backend.php" method="POST" enctype="multipart/form-data">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label for="uploadedimage">Small image to upload: </label>
<input type="file" name="uploadedimage" id="uploadedimage"/><br />
<label>Date:</label>
<input class="input" name="date" type="text" value=""><br />
<label>Retrace:</label>
<input class="input" name="retrace" type="text" value=""><br />
<label>Start of Swing Trade:</label>
<input class="input" name="start_of_swing_trade" type="text" value=""><br />
<label>End of Swing Trade:</label>
<input class="input" name="end_of_swing_trade" type="text" value=""><br />
<label>Bull flag:</label>
<input class="input" name="bull_flag" type="text" value=""><br />
<label>Bear flag:</label>
<input class="input" name="bear_flag" type="text" value=""><br />
<label>EMA Crossover:</label>
<input class="input" name="ema_crossover" type="text" value=""><br />
<label>Trading Instrument:</label>
<input class="input" name="trading_instrument" type="text" value=""><br />
<input class="submit" name="submit" type="submit" value="Insert">
</form>
<?php
if (isset($_GET['success']) && $_GET['success']) {
    echo "New record created successfully";
} else if (isset($_GET['success']) && !$_GET['success']) {
    echo "Failed to insert new record";
}
?>
</div>
</div>
</body>
</html>

Updated my PHP code to include the header location:

        $sql = "INSERT into charts (charts_URL, charts_date, charts_retrace, charts_start_of_swing_trade, charts_end_of_swing_trade, charts_bullflag, charts_bearflag, charts_ema_crossover, charts_trading_instrument) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

     
        // s = string, i = integer, d = double, b = blob
        //preparing statement
        $stmt = $conn->prepare($sql);
        if(!$stmt){ exit("prepare failed");}
        //binding param
        $stmt->bind_param('sssssssss',$target_path, $date, $retrace, $start_of_swing_trade, $end_of_swing_trade, $bull_flag, $bear_flag, $ema_crossover, $trading_instrument);
         if(!$stmt){ exit("bind failed");}
        //will return 0 if fail
        if($stmt->execute() != 0){
        
        $success = true;
    }else{ 
        $success = false;
    }
    header('location:insertchart.php?success='.$success);
    exit;
}
}
}
?>

The FINAL code (actually a snippet) that made everything work:

    if($stmt->execute() != 0){
        $success = true;
        header('Location:http://mlsinc.net/chart-submission/insertchart.php?success='.$success);
    }else{ 
        $success = false;
        header('Location:http://mlsinc.net/chart-submission/insertchart.php?success='.$success);
    }
}
    //close connection
$conn->close();
Thomas
  • 69
  • 1
  • 7
  • You'll need to use an JavaScript (or jQuery) AJAX post to submit the form if you don't want to redirect. http://stackoverflow.com/questions/3438123/how-to-submit-a-form-with-ajax-json – codyogden Apr 22 '15 at 06:14

4 Answers4

1

Try with -

$stmt->bind_param('sssssssss',$target_path, $date, $retrace, $start_of_swing_trade, $end_of_swing_trade, $bull_flag, $bear_flag, $ema_crossover, $trading_instrument);
     if(!$stmt){ exit("bind failed");}
    //will return 0 if fail
    if($stmt->execute() != 0){
        $success = true;
    }else{ 
        $success = false;
    }
    header('location:yourform.php?success='.$success);
    exit;

On you form page add a check for it -

if (isset($_GET['success']) && $_GET['success']) {
    echo "New record created successfully";
} else if (isset($_GET['success']) && !$_GET['success']) {
    echo "Failed to insert new record";
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • where exactly do I put the check code at? I put it right below `` in between `` tags but when I click Insert, I get redirected to the insert_backend.php page with no messages. – Thomas Apr 22 '15 at 08:54
  • you should add this code inside php tags on the page you are redirecting after processing – Sougata Bose Apr 22 '15 at 08:56
  • I did, but the page still redirects to the page that processes the data and I don't see any success or fail messages. Check my code above....I edited my original post. – Thomas Apr 22 '15 at 09:06
  • Have you added the header? – Sougata Bose Apr 22 '15 at 09:07
  • yes, check my updated php code above. I didn't put the full path. I just put the file name since the insert form and the processing file are in the same folder/directory. But when I click insert, it still redirects to the processing file. – Thomas Apr 22 '15 at 09:14
  • it is not redirecting back to the form page? – Sougata Bose Apr 22 '15 at 09:16
  • No, which is really weird. It goes straight to the processing file. – Thomas Apr 22 '15 at 09:29
  • got it working (as far as I can see). was missing a bracket at the end which I added in, then I put in the header redirection code in twice....one under the if statement and one under the else statement. so no matter if there is success or failure, the user will be redirected to the form and see a success/failure message there. – Thomas Apr 22 '15 at 15:25
0

To show the message on form page

Store the message in session after inserting the row(since you are redirecting page)

0

I think the soultion suggested by sgt BOSE will work for you. But instead of passing a "success" GET parameter back to your page containing form, you should use a session variable because the $_GET['success'] will re-print the message if user reloads the window.

Once you print the result stored in session variable make sure to unset it.

Techroshni
  • 541
  • 2
  • 11
0

Ok I see your problem here, it's actually pretty simple. the way it works is that the message would be declared inside of the script tag so you should do something like this so it will call the message.

<html>

<head>
  <script type="text/javascript">
    //Put the javascript function you want the button to do Here
    function show_alert()

     {
      alert("Form Succesfully Submited");
    }
  </script>
</head>


<body>

  <form action="insert_backend.php" method="POST" enctype="multipart/form-data">
    <!-- Method can be set as POST for hiding values in URL-->
    <h2>Form</h2>
    <label for="uploadedimage">Small image to upload:</label>
    <input type="file" name="uploadedimage" id="uploadedimage" />
    <br />
    <label>Date:</label>
    <input class="input" name="date" type="text" value="">
    <br />
    <label>Retrace:</label>
    <input class="input" name="retrace" type="text" value="">
    <br />
    <label>Start of Swing Trade:</label>
    <input class="input" name="start_of_swing_trade" type="text" value="">
    <br />
    <label>End of Swing Trade:</label>
    <input class="input" name="end_of_swing_trade" type="text" value="">
    <br />
    <label>Bull flag:</label>
    <input class="input" name="bull_flag" type="text" value="">
    <br />
    <label>Bear flag:</label>
    <input class="input" name="bear_flag" type="text" value="">
    <br />
    <label>EMA Crossover:</label>
    <input class="input" name="ema_crossover" type="text" value="">
    <br />
    <label>Trading Instrument:</label>
    <input class="input" name="trading_instrument" type="text" value="">
    <br />






    <!--This is where you tell the script where to load the (ID) for example im using a simple message for you what you need to do is make the id im sure you know how to create a simple (ID) inside of a java ,heck im  thirteen im about an expert on it if i can do im sure you can! :) :)
anyway inside the div tag make it look like this: <div id=("PUT ID IN HERE") >
then put you function in the script for wha you want it to do hope this helped you have a nice day!-->
    <div>
      <input class="button" name="submit" type="submit"  onclick="show_alert()" value="Submit">
    </div>

  </form>







</body>

</html>
Hope that this helps if not dont hate lol.
Noah Mayo
  • 72
  • 1
  • 15