【问题标题】:Php form submission - "thank you" message on same page instead of another page?Php 表单提交 - 在同一页面而不是另一个页面上的“谢谢”消息?
【发布时间】:2012-08-09 21:01:36
【问题描述】:

我有一个带有 php 表单提交的 html 页面,这是非常基本的.. 我现在要做的是提交,然后转到一个带有“谢谢”等字样的空白页面,但是什么我想做的是当用户提交表单时,它会弹出一个窗口或灯箱,上面写着“谢谢提交”。我怎样才能做到这一点?这是表单的 html 代码,以及提交的实际 php 代码。

html代码:

        <form method="post" action="includes/buy.php">
        <h2> Sizes: </h2><select name="sizes"><option value="1">1</option></select>
        <h2> Email: </h2><input type="text" name="email" />
        <input type="hidden" value="black" name="color" />
        <input  type="image" value="submit" src="images/buynow.jpg" />
        </form>

php表单提交代码:

<?php
if(isset($_POST['email'])) {

$email_to = " ";
$email_subject = "subject line here";


function died($error) {
echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

if(!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$email_from = $_POST['email']; 
$color = $_POST['color'];
$sizes = $_POST['sizes'];


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details:\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Size: ".clean_string($sizes)."\n";
$email_message .= "Color: ".clean_string($color)."\n";



// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<?php
}
die();
?>

【问题讨论】:

  • 您可能想要考虑进行 ajax 调用。对于更简单的事情,您可以使用特殊消息重定向回此页面

标签: php html forms lightbox


【解决方案1】:

我建议您使用AJAX。这是一个例子:

HTML:

<form name="yourForm" action="">
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    <input type="submit" class="formSubmit" />
</form>

JavaScript (jQuery):

$('.formSubmit').on('click', function() {
    var field1 = $('input[name=field1]').val();
    var field2 = $('input[name=field2]').val();
    $.ajax({
        type: 'POST';
        url: 'yourPHPScript.php?f1='+ field1 +'&f2='+ field2;
        success: function(result) {
            alert(result);
        }
    });
});

PHP:

// proccess the data
die('successful');

在 PHP 文件中:如果有问题,只需 die() 那个结果,它就会显示一个 alert()

【讨论】:

    【解决方案2】:

    如果您想在不加载页面的情况下执行此操作(即使用弹出对话框或类似的方法),则需要考虑使用 AJAX。但是,如果重新加载页面没问题,您可以只使用当前页面作为页面表单的目标并在同一脚本中处理表单。

    【讨论】:

    • 谢谢!我希望我可以选择这个和另一个人的作为答案,你们俩的想法是一样的,但是谢谢!
    【解决方案3】:

    如果您对它在单独的窗口中弹出感到满意,您可以将表单的目标设置为 _new 或 _blank。更好的解决方案是使用javascript通过AJAX提交表单,然后在页面中显示结果。

    在将 JavaScript 与 AJAX 结合使用时,最好查看 jQuery,特别是 jQuery.post

    【讨论】:

      【解决方案4】:

      如果你想将一个快速简单的 PHP 链接到另一个 .php 文件

      你可以做类似的事情

      <?php
        echo $_POST['field1'];
      ?>
      

      我在上课时不得不用我的手机快速阅读这篇文章,但希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 2021-10-16
        • 2019-01-04
        • 1970-01-01
        相关资源
        最近更新 更多