doukuibi9631 2017-03-31 20:41
浏览 58

使用PHP提交html表单在同一div中显示“Thank You”消息

I am trying to create a simple forum using php to email the contents. Once the user clicks submit, I would like to have the div display a thank you message.

Currently going about this by trying to have the php write the html code using an if else statement. It seems the if statement is working, but the else statement is showing up as text.

Here is my code:

<?php
if(isset($_POST['submit'])) {
        echo "<form method="post" action="">
        <div class="field half first">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" />
        </div>  
        <div class="field half">    
        <label for="email">Email</label>        
        <input type="text" name="email" id="email" />       
        </div>
        <div class="field">
        <label for="message">Message</label>
        <textarea name="message" id="message" rows="5"></textarea>      
        </div>      
        <input class="button submit" type="submit" name="submit" value="Submit">    
        </form>";       

} else {
    $email_to = "field25farming@gmail.com";
    $email_subject = "Field 25 Contact Form";
    echo "Thanks! We'll get back to you soon."; 
} 
?>

You can see the problem in the bottom of the page here if you'd like: http://field25.com/

I'm also open to other ways of achieving this, thanks for you help.

  • 写回答

1条回答 默认 最新

  • dswqw66280 2017-03-31 20:59
    关注

    Surprised that's rendering anything at all! There are a couple of problems. First, you have nested double quotes. Second, you're checking for the existence of the submit and if it exists, rendering the form. If it has to exist first, it'll never be rendered.

    You can avoid the first problem entirely by simply dipping out of PHP. The second problem can be fixed by checking if submit does not exist via the ! operator (or flipping the if/else output).

    <?php
    // if submit is not set, we want to render the form.
    if( !isset($_POST['submit']) ) { ?>
    
    <!-- leave PHPlandia -->
    
    <form method="post" action="">
      <div class="field half first">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" />
      </div>
      <div class="field half">
        <label for="email">Email</label>
        <input type="text" name="email" id="email" />
      </div>
      <div class="field">
        <label for="message">Message</label>
        <textarea name="message" id="message" rows="5"></textarea>
      </div>
      <input class="button submit" type="submit" name="submit" value="Submit">
      </form>
    <?php
    } else {
        $email_to = "field25farming@gmail.com";
        $email_subject = "Field 25 Contact Form";
    
        // ... rest of your email sending code here ...
    
        echo "Thanks! We'll get back to you soon.";
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题