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条)

报告相同问题?

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题