如何消除此脚本中 die() 的使用,以便底部的 HTML 仍然运行?

发布于 2024-11-18 12:39:16 字数 5557 浏览 2 评论 0原文

当我最初编写这个脚本时,站点设计不需要在 die() 函数之后执行 html。现在确实如此。我意识到我可以将 HTML 代码的末尾复制到每个 die() 语句中,但它包含一个 php 包含文件 (footer.inc.php),然后该文件会被忽略。 (这是我目前正在使用的解决方法,因此页面看起来不错,但不包含页脚。)

我尝试将第一个序列中的 die() 函数切换为 if() 和 elseif() 函数,但是然后它会显示 die() 消息以及电子邮件代码后面的消息,表明电子邮件功能成功。

我确信有一种方法可以让脚本只执行其中一个语句而不使用 die(),但我无法弄清楚。任何提示将不胜感激!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta name="google-site-verification" content="0QW3jKsbHBGLvnLgLIoSHRuxjHBUI_MMQ0wn9J-4eo4" />
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
 <link href="../stylesheet.css" rel="stylesheet" type="text/css" /> 
  <link rel="icon" href="../images/favicon.ico" type="image/jpg" />
<title>Center Court - Schedule a racquet pick-up</title>
</head>
<body>
<div id="container">

<div id="liquid-round">
    <div class="top"><span></span></div>
        <div class="center-content">

<div id="header">
<?php include("../header2.inc.php"); ?>
</div>
<div id="nav">
<?php include("../nav.inc.php"); ?>
</div>
    <div id="phpscreen">
<?php
    //retrieve details from POST submission
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $retrieval = $_POST['retrieval'];
    $needed = $_POST['needed'];
    $string = $_POST['string'];
    $outofstock = $_POST['outofstock'];
    $tension = $_POST['tension'];
    $scale = $_POST['scale'];
    $extras= $_POST['extras'];
    $notes = $_POST['notes'];

    //validate sumbitted data
    if (empty($name)){
    die('<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($email)){
    die('<p class="center">Please provide your email.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($phone)){
    die('<p class="center">Please provide your phone number.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($address)){
    die('<p class="center">Please provide your address.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    if (empty($city)){
    die('<p class="center">Please provide your city.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    if (empty($needed)){
    die('<p class="center">Please provide the date and time you need your racquet.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    //formulate email message
    $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';
    $body = "
    Name: $name\r\n
    Email: $email\r\n
    Phone number: $phone\r\n
    Address: $address\r\n
    City: $city\r\n
    Retrieval method: $retrieval\r\n
    Needed by: $needed\r\n
    String type: $string\r\n
    Similar or upgrade OK: $outofstock\r\n
    Tension: $tension\r\n
    Unsure scale: $scale\r\n
    Extras: $extras\r\n
    Special requests or notes: $notes\r\n";
    if(mail($to, $subject, $body, "From $from")){
        echo '<p>Your pick-up has been requested. You should receive confirmation shortly via e-mail or phone. If you do not hear from us by the end of the day, please call Center Court Tennis Shop at 203-966-2543.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>';
    }else{
        die('<p>Sorry, there was a mail delivery error. please call Center Court Tennis Shop at 203-966-2543 to schedule your pick-up.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>');
        }
        ?>
        </div>

<div id="footer">
<?php include("../footer.inc.php"); ?>
</div> <!--end footer-->

</div><!--end center content-->

    <div class="bottom"><span></span></div>
</div><!--end liquid-round-->
</div><!--end container-->

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18191457-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script></body> </html> 

When I initially wrote this script, the site design did not require the execution of the html after the die() functions. Now it does. I realize I can copy the end of the HTML code into each of the die() statements, but it contains a php include file (footer.inc.php) which is then ignored. (This is the workaround that I am going with at the moment, so the page looks OK but does not contain the footer.)

I tried switching the die() functions in the first sequence to if() and elseif() functions, but then it displays both the die() message and the message after the email code that indicates the email function was successful.

I am sure there is a way to have the script execute only one of these statements without using the die(), but I can't figure it out. Any tips would be greatly appreciated!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta name="google-site-verification" content="0QW3jKsbHBGLvnLgLIoSHRuxjHBUI_MMQ0wn9J-4eo4" />
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
 <link href="../stylesheet.css" rel="stylesheet" type="text/css" /> 
  <link rel="icon" href="../images/favicon.ico" type="image/jpg" />
<title>Center Court - Schedule a racquet pick-up</title>
</head>
<body>
<div id="container">

<div id="liquid-round">
    <div class="top"><span></span></div>
        <div class="center-content">

<div id="header">
<?php include("../header2.inc.php"); ?>
</div>
<div id="nav">
<?php include("../nav.inc.php"); ?>
</div>
    <div id="phpscreen">
<?php
    //retrieve details from POST submission
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $retrieval = $_POST['retrieval'];
    $needed = $_POST['needed'];
    $string = $_POST['string'];
    $outofstock = $_POST['outofstock'];
    $tension = $_POST['tension'];
    $scale = $_POST['scale'];
    $extras= $_POST['extras'];
    $notes = $_POST['notes'];

    //validate sumbitted data
    if (empty($name)){
    die('<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($email)){
    die('<p class="center">Please provide your email.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($phone)){
    die('<p class="center">Please provide your phone number.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }

    if (empty($address)){
    die('<p class="center">Please provide your address.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    if (empty($city)){
    die('<p class="center">Please provide your city.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    if (empty($needed)){
    die('<p class="center">Please provide the date and time you need your racquet.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
    }
    //formulate email message
    $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';
    $body = "
    Name: $name\r\n
    Email: $email\r\n
    Phone number: $phone\r\n
    Address: $address\r\n
    City: $city\r\n
    Retrieval method: $retrieval\r\n
    Needed by: $needed\r\n
    String type: $string\r\n
    Similar or upgrade OK: $outofstock\r\n
    Tension: $tension\r\n
    Unsure scale: $scale\r\n
    Extras: $extras\r\n
    Special requests or notes: $notes\r\n";
    if(mail($to, $subject, $body, "From $from")){
        echo '<p>Your pick-up has been requested. You should receive confirmation shortly via e-mail or phone. If you do not hear from us by the end of the day, please call Center Court Tennis Shop at 203-966-2543.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>';
    }else{
        die('<p>Sorry, there was a mail delivery error. please call Center Court Tennis Shop at 203-966-2543 to schedule your pick-up.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>');
        }
        ?>
        </div>

<div id="footer">
<?php include("../footer.inc.php"); ?>
</div> <!--end footer-->

</div><!--end center content-->

    <div class="bottom"><span></span></div>
</div><!--end liquid-round-->
</div><!--end container-->

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18191457-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script></body> </html> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-11-25 12:39:16

您可以构建一个 $error 变量并在尝试发送电子邮件之前检查它。大致如下:

$error = '';
//validate sumbitted data
if (empty($name)){
    $error .= '<p class="center">Please provide your name.</p>';
}

if (empty($email)){
    $error .= '<p class="center">Please provide your email.</p>';
}

// etc...

if (empty($error)) {
    //formulate email message
    $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';

    // etc...

    if (mail($to, $subject, $body, "From $from")) {
        echo '<p>Your pick-up has been requested...</p>';
    } else {
        die('<p>Sorry, there was a mail delivery error...');
    }
} else {
    echo $error;
    echo '<form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>';
}

// Rest of HTML

You can build an $error variable and check it before attempting the email. Something along the lines of:

$error = '';
//validate sumbitted data
if (empty($name)){
    $error .= '<p class="center">Please provide your name.</p>';
}

if (empty($email)){
    $error .= '<p class="center">Please provide your email.</p>';
}

// etc...

if (empty($error)) {
    //formulate email message
    $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';

    // etc...

    if (mail($to, $subject, $body, "From $from")) {
        echo '<p>Your pick-up has been requested...</p>';
    } else {
        die('<p>Sorry, there was a mail delivery error...');
    }
} else {
    echo $error;
    echo '<form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>';
}

// Rest of HTML
笔落惊风雨 2024-11-25 12:39:16

不要使用 die(),因为它被编程为阻止整个脚本运行。相反,您应该使用 echoprint 方法在屏幕上显示内容。

示例:

<div id="phpscreen">
     <?
     //validate sumbitted data
     if (empty($name)) {
         echo '<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>';
     }
     else if (empty($email)) {
          ...
     }
     ....
     else {
         if(mail($to, $subject, $body, "From $from")) {
              ...
         } 
         else {
              ...
         }
     } ?>
</div>

然后对所有其他检查使用“else if”。否则,如果您不填写 10 个字段,您将显示 10 个表格。 :)

Simply do not use die() because it is programmed to stop your entire script from running. Instead, you should use either the echo or print method to display content on your screen.

Example:

<div id="phpscreen">
     <?
     //validate sumbitted data
     if (empty($name)) {
         echo '<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>';
     }
     else if (empty($email)) {
          ...
     }
     ....
     else {
         if(mail($to, $subject, $body, "From $from")) {
              ...
         } 
         else {
              ...
         }
     } ?>
</div>

And then use "else if" for all the other checks. Else you will show 10 forms if you do not fill in 10 fields. :)

染火枫林 2024-11-25 12:39:16

将您的 if邮件发送 语句更改为 if-elseif-else

//validate sumbitted data
if (empty($name)){
    echo('<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
}
elseif (empty($email)){
    echo('<p class="center">Please provide your email.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
}
//... other validation conditionals...

//None of the above conditionals are true, send the message.
else
{

   $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';
    $body = "
    Name: $name\r\n
    Email: $email\r\n
    Phone number: $phone\r\n
    Address: $address\r\n
    City: $city\r\n
    Retrieval method: $retrieval\r\n
    Needed by: $needed\r\n
    String type: $string\r\n
    Similar or upgrade OK: $outofstock\r\n
    Tension: $tension\r\n
    Unsure scale: $scale\r\n
    Extras: $extras\r\n
    Special requests or notes: $notes\r\n";
    if(mail($to, $subject, $body, "From $from")){
        echo '<p>Your pick-up has been requested. You should receive confirmation shortly via e-mail or phone. If you do not hear from us by the end of the day, please call Center Court Tennis Shop at 203-966-2543.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>';
    }else{
        die('<p>Sorry, there was a mail delivery error. please call Center Court Tennis Shop at 203-966-2543 to schedule your pick-up.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>');
        }
}

Change your if and mail sending statements into an if-elseif-else block

//validate sumbitted data
if (empty($name)){
    echo('<p class="center">Please provide your name.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
}
elseif (empty($email)){
    echo('<p class="center">Please provide your email.</p> <form><p class="center"><input type="button" value="Go back" onClick="history.go(-1);return true;"> </p></form>');
}
//... other validation conditionals...

//None of the above conditionals are true, send the message.
else
{

   $to = '[email protected]';
    $from = 'centercourtstringing.com';
    $subject = 'Contact from centercourtstringing.com';
    $body = "
    Name: $name\r\n
    Email: $email\r\n
    Phone number: $phone\r\n
    Address: $address\r\n
    City: $city\r\n
    Retrieval method: $retrieval\r\n
    Needed by: $needed\r\n
    String type: $string\r\n
    Similar or upgrade OK: $outofstock\r\n
    Tension: $tension\r\n
    Unsure scale: $scale\r\n
    Extras: $extras\r\n
    Special requests or notes: $notes\r\n";
    if(mail($to, $subject, $body, "From $from")){
        echo '<p>Your pick-up has been requested. You should receive confirmation shortly via e-mail or phone. If you do not hear from us by the end of the day, please call Center Court Tennis Shop at 203-966-2543.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>';
    }else{
        die('<p>Sorry, there was a mail delivery error. please call Center Court Tennis Shop at 203-966-2543 to schedule your pick-up.</p><p class="center"><a href="http://centercourtstringing.com">Return to home page</a></p>');
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文