简体   繁体   English

Ajax发布将参数添加到主页链接

[英]Ajax post adds parameters to home link

I'm going to start off that I'm not really experienced in web development, so excuse if this question sound rather dumb or the solution is obvious but I can't seem to find it on this site or google. 首先,我没有真正的Web开发经验,所以请原谅,如果这个问题听起来很愚蠢,或者解决方案很明显,但我似乎无法在本网站或Google上找到它。 Basically I have an HTML form and want to post data to a PHP script which mails the data to a specific email address. 基本上,我有一个HTML表单,想将数据发布到PHP脚本,该脚本将数据邮寄到特定的电子邮件地址。 This is the HTML code: 这是HTML代码:

<form id="contact-form" name="contactform" action="">
  <fieldset>
    <p class="contact-name">
      <input id="name" type="text" placeholder="Full Name" value="" name="name"/>
      <label class="error" for="name" id="name_error">This field is required.</label> 
    </p>
    <p class="contact-email">
      <input id="email" type="text" placeholder="Email Address" value="" name="email"/>
      <label class="error" for="email" id="email_error">This field is required.</label> 
    </p>    
    <p class="contact-message">
      <textarea id="message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea>
      <label class="error" for="message" id="message_error">This field is required.</label> 
    </p>
    <p class="contact-submit">
      <input type="submit" value="Submit" id="submit_button" class="button"> 
    </p>
  </fieldset>        
</form> 

This is the jQuery & Ajax code to validate the data: 这是用于验证数据的jQuery&Ajax代码:

$(document).ready(function() {
    $('.error').hide();
    $(".button").click(function() {
        //validate process  
        var name = $("input#name").val();
        if (name == "") {
            $("label#name_error").show();
            $("input#name").focus();
            return false;
        }
        var email = $("input#email").val();
        if (email == "") {
            $("label#email_error").show();
            $("input#email").focus();
            return false;
        }
        var message = $("textarea#message").val();
        if (message == "") {
            $("label#message_error").show();
            $("textarea#message").focus();
            return false;
        }
    });

    var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
    $.ajax({
        type: "POST",
        url: "_include/php/contact.php",
        data: dataString,
    });
    return false;

});

The problem that occurs here is not the fact that the JS code doesn't run but the post adds the parameters to the current link like: http//website.com/index.html?name=test&email=email&message=msg 这里发生的问题不是JS代码没有运行,而是帖子将参数添加到当前链接,例如:http // website.com / index.html?name = test&email = email&message = msg

But I need it to add the parameters to my PHP file which is located at _include/php/contact.php 但是我需要它来将参数添加到位于_include / php / contact.php的PHP文件中

Update: So I've tried various answers and thanks for the great replies but I'm still stuck. 更新:因此,我尝试了各种答案,并感谢您的出色答复,但我仍然感到困惑。 I can choose to set the action of the form to the PHP file but that means that my page is refreshed and that is something that I want to avoid. 我可以选择将表单的操作设置为PHP文件,但这意味着页面已刷新,这是我要避免的事情。 The other js scripts didn't seem to change the fact that the parameters are added to the wrong link.. 其他js脚本似乎并没有改变将参数添加到错误链接的事实。

Your ajax request is not within the $('.button').click() event. 您的ajax请求不在$('.button').click()事件之内。 It is triggered even without clicking the button. 即使不单击按钮也将触发它。 Thus, now the button is triggering the native form submit which has the action="" and therefor will append the formdata as a GET request. 因此,现在该按钮将触发具有action=""的本机表单提交,因此将附加表单数据作为GET请求。

Change the javascript code so the formlogic is applied on $('form').submit() instead of the button click event; 更改javascript代码,以便将formlogic应用于$('form').submit()而非按钮click事件; This will increase logic in your code and you disable the full native functionality of the form by return false; 这将增加代码中的逻辑,并通过返回false禁用表单的全部本机功能;

表单的“ action”属性需要设置为要提交表单的php页面,例如:

 <form id="contact-form" name="contactform" action="/php/contact.php">

I've not tested it, but the snipplet below show the basic setup I normally use. 我没有测试过,但是下面的代码片段显示了我通常使用的基本设置。

$('#contact-form').submit(function(event){
    var form = $(this);
    if (isContactFormValid()) {
        postData(form.serialize());
    }
    event.preventDefault();



    function isContactFormValid() {
        //Some logic here
        return true;
    }
    function postData(data) {
        $.ajax({
            type: "POST",
            url: "_include/php/contact.php",
            data: data
        });
    }
});

Probably you need event.preventDefault() 可能您需要event.preventDefault()

http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM