douji0108 2017-04-07 02:43
浏览 18
已采纳

需要PHP和我的HTML5表单的帮助

I used an anchor tag for my submit button so I think that this is probably why I'm having so much trouble trying to figure this out.

I'm not sure if it's the "href" that is messing it all up. I also would like some assistance with server side validation, not having a lot of experience in PHP, I was only able to validate using JavaScript.

<form id="myform" method="post" name="contact_form" action="process.php">

    <input id="cname" type="text" name="name" minlength="2" placeholder="Full Name" class="form-control" required>

    <input id="cemail" type="email" name="email" placeholder="Email Address" class="form-control" required>

    <textarea id="ccomment" rows="5" name="message" placeholder="Message..." class="form-control" required></textarea>

    <div id="send-btn">
        <a href="process.php" onClick="$(this).closest('form').submit();" class="btn btn-lg btn-general btn-white" role="button" name="submit">SEND</a>
        <asp:Button runat="server" Text="submit"/>
    </div>

</form>

This below is my PHP code. Please assist with validations and making it work with an anchor tag. I could have messed up the form somewhere.

<?php ob_start();


if (isset($_POST['submit'])) {

    $to = "someoneelse@someone.com";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $txt = $_POST['message'];
    $headers = "From: .$email" . "
" .

    mail($to,$email,$txt,$headers);
    header("Location: index.html");
}


?>
  • 写回答

3条回答 默认 最新

  • dongmimeng5500 2017-04-07 04:24
    关注

    Ideally, with modern Javascript you would avoid having any script in your markup and would attach the click event using jquery and prevent the default action for the anchor tag using preventDefault. (See https://api.jquery.com/click/ and https://api.jquery.com/event.preventDefault/ )

    However, if you are going to use an onclick attribute, you need to return false. If the onclick event returns false, then the browser will cancel the default action.

    <a href="#" onclick="$('#myform').submit(); return false;" class="btn btn-lg btn-general btn-white" role="button" name="submit">SEND</a>
    

    When the anchor link is clicked, the javascript to submit the form will run, but the return false will prevent the link from being followed. The return false will also prevent the browser from jumping to the top when you use a # in your href.

    As for server side validation in PHP, you will need to use conditional statements to check that the posted variables match a pattern. See How to validate an email address in PHP .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路