mew*_*ebs 34 html php forms submit
我有一个位于contact.html上的php表单
表单从processForm.php处理
当用户填写表单并单击"提交"时,processForm.php会发送电子邮件并将用户定向到 - processForm.php,并在该页面上显示"成功!您的邮件已发送"消息.
我对php知之甚少,但我知道要求它的动作是:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何在不重定向到processForm.php页面的情况下将消息保留在表单div中?
如果需要,我可以发布整个processForm.php,但它很长.
谢谢
Tom*_*oot 33
为了在提交时保持同一页面,您可以将action empty(action=""
)留在表单标记中,或者完全不将它保留.
对于消息,创建一个变量($message = "Success! You entered: ".$input;"
),然后在页面中要显示消息的位置回显该变量<?php echo $message; ?>
.
像这样:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Cha*_*seC 13
保持同一页面的最佳方式是发布到同一页面.
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
Run Code Online (Sandbox Code Playgroud)
Ash*_*han 12
有两种方法:
将表单提交到同一页面:使用PHP脚本处理提交的表单.(这可以通过将表单action
设置为当前页面URL来完成.)
if(isset($_POST['submit'])) {
// Enter the code you want to execute after the form has been submitted
// Display Success or Failure message (if any)
} else {
// Display the Form and the Submit Button
}
Run Code Online (Sandbox Code Playgroud).2. 使用AJAX表单提交对初学者来说比方法#1稍微困难一些.
您可以#
在表单操作中使用该操作:
<?php
if(isset($_POST['SubmitButton'])){ // Check if form was submitted
$input = $_POST['inputText']; // Get input text
$message = "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="#" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
小智 5
朋友。使用这种方式,不会出现“未定义的变量消息”,并且可以正常工作。
<?php
if(isset($_POST['SubmitButton'])){
$price = $_POST["price"];
$qty = $_POST["qty"];
$message = $price*$qty;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="post">
<input type="number" name="price"> <br>
<input type="number" name="qty"><br>
<input type="submit" name="SubmitButton">
</form>
<?php echo "The Answer is" .$message; ?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
253181 次 |
最近记录: |