草庐IT

基于嵌套 if 语句的 PHP 邮件重定向 header 位置

coder 2024-04-28 原文

您好,我正在为客户设计一个网站。 我设计了这个表格

    <form action="inc/email.php" method="POST" onSubmit="alert('Email send successfully!');">
    <div class="col-md-6">
        <input type="text" id="contact-name" name="contact-name" value="" placeholder="Your name" required/>
        <input type="email" id="contact-email" name="contact-email" value="" placeholder="Your email" required/>
        <input type="number" id="contact-phone" name="contact-phone" value="" placeholder="Your phone" required/>
    </div>
    <div class="col-md-6">
        <select name="cars">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="audi">Audi</option>
        </select>
    </div>
    <div class="col-md-12">
        <input type="submit" value="Send message"/></td>
    </div>
</form>

它有3种选择,1st Volvo,2nd Saab,3rd Audi
现在我想根据用户选择将表单提交重定向到不同的网站
例如如果用户选择 Volvo,则为 facebook.com;如果选择 Saab,则为 Twiiter.com。

我为相同的代码设计了以下 PHP 代码

<?php
/*Getting submitted values from html form*/

$name = $_POST['contact-name'];
$email = $_POST['contact-email'];
$phone = $_POST['contact-phone'];
$cars= $_POST['cars'];
/* Generating Output HTML */

$output = '<html><body><div>';
$output .= '<h2 style="color: #49A020;">Newsletter Email</h2>';
$output .= '<p style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;">Name: '.$name.'</p>';
$output .= 'Phone: <a style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="tel: '.$phone.'">'.$phone.'</a><br><br>';
$output .= 'Email: <a  style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="mailto:'.$email.'">'.$email.'</a>';
$output .= '</div><div>';

/*Signature */

$output .= '<hr style="border:1px solid #49A020;">';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">Thanks & Regards,</p>';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">South Post Fitness,</p>';
$output .= 'Phone: <a href="tel:416843 5925">(416) 843 5925</a><br>';
$output .='Email: <a href="maito: info@xx.com">info@xx.com</a>';
$output .= '</div></body></html>';

 $to = 'my email address here';  /* Receivers Email*/

$headers = "From: $email \r\n"; /*Headers*/
$headers .= 'MIME-Version: 1.0' . "\n"; /*Headers*/
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n"; /*Headers*/

$subject = 'my subject here'; /* Subject */
mail($to, $subject, $output, $headers); /*Sending Mail*/
  if(mail) {
    if($cars=="Volvo"){
          header("Location: http://facebook.com");  /*Redirect to home after sucessfull submission*/
    }
    elseif($cars=="Saab"){
      header("Location: http://twitter.com");  /*Redirect to home after sucessfull submission*/
    }
    elseif($cars=="Audi"){
        header("Location: http://pintrest.com");  /*Redirect to home after sucessfull submission*/
    }
  }
  else {
  echo 'Error sending email'; /*If failed display error message */
  }

?>

但此代码仅将页面重定向到第一个链接,即 facebook.com,无论选择哪个选项

最佳答案

mail($to, $subject, $output, $headers); /*Sending Mail*/
if(mail) {

应该是

$mail = mail($to, $subject, $output, $headers); /*Sending Mail*/
if($mail) {

请注意您的条件中缺少的变量赋值和缺少的 $ 符号。

除此之外,您检查了大写字符串,但您的实际字符串是小写。

if($cars=="Volvo")

不等于

if($cars=="volvo")

关于基于嵌套 if 语句的 PHP 邮件重定向 header 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292241/

有关基于嵌套 if 语句的 PHP 邮件重定向 header 位置的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  3. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  4. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  5. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  6. ruby - ruby 中有 each_if 吗? - 2

    假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其

  7. ruby-on-rails - 使用回形针的嵌套形式 - 2

    我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?

  8. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  9. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  10. ruby - 将 spawn() 的标准输出/标准错误重定向到 Ruby 中的字符串 - 2

    我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])

随机推荐