[英]“Data entered successfully” message to appear on same page of insert form?
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: 这是php上“成功”消息的片段,用于处理提交给数据库的数据:
$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. 每当我插入数据时,在insert_backend.php页面上都会收到“成功创建新记录”消息,该页面是用于处理和提交数据的php文件,但在用于插入数据的实际表单页面上却没有。 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): 我的完整插入表单代码(包含php重定向检查解决方案):
<!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: 更新了我的PHP代码以包括标头位置:
$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: 使一切正常的FINAL代码(实际上是代码段):
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();
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";
}
To show the message on form page 在表单页面上显示消息
Store the message in session after inserting the row(since you are redirecting page) 插入行后将消息存储在会话中(因为您正在重定向页面)
I think the soultion suggested by sgt BOSE will work for you. 我认为sgt BOSE所建议的灵魂将为您服务。 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.
但是,您应该使用会话变量,而不是将“成功” GET参数传递回包含表单的页面,因为如果用户重新加载窗口,$ _ GET ['success']将重新打印消息。
Once you print the result stored in session variable make sure to unset it. 一旦打印了存储在会话变量中的结果,请确保将其取消设置。
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.
它的工作方式是将消息声明在script标签内部,因此您应该执行类似的操作,以便调用消息。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.