1

我正在用 JQuery 开发一个 web 表单并通过 post 发送到 PHP 页面。

它看起来像这样:

$('#form-signup').submit(function(e)
{
    $.ajax(
    {
        type: 'POST',
        data: { name: $('#name').val() },
        url: 'signup_backend.php',
        success: function(data, textStatus, jqXHR)
        {
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert('error');
        }
    });

我的网络表单如下所示:

<form id="form-signup" action="">
    <fieldset>
        <label for="name">Nome</label>
        <input type="text" id="name" name="name" value="<?=$name?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="email">Email</label>
        <input type="email" id="email" name="email" value="<?=$email?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="tel">Telefone</label><input type="tel" id="tel" name="tel" value="<?=$tel?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <div>
            <label class="blocker">Tipo de anunciante</label><span class="required">*</span>
        </div>
        <input type="radio" name="advertiser" id="advertiser_1">
        <label for="advertiser_1" class="aleft">Particular</label>
        <input type="radio" name="advertiser" id="advertiser_2">
        <label for="advertiser_2" class="aleft">Imobiliária</label>
    </fieldset>
    <fieldset>
        <label for="password">Senha</label>
        <input type="password" id="password" name="password" value="<?=$password?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="check-password">Confirmação da senha</label>
        <input type="password" id="check-password" name="check_password" value="<?=$check_password?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <input type="submit" value="Enviar" id="sub">
    </fieldset>
</form>

当我收到退货时,我的地址显示如下:

http://localhost:8888/guia/web/adm/signup.php?name=John&email=&tel=&password=&check_password=

有没有办法避免我的地址栏中的这些数据?

多谢你们

4

5 回答 5

6

防止表单提交的默认操作

$('#form-signup').submit(function(e){
    e.preventDefault(); /// <--- THIS LINE
    $.ajax({
        type: 'POST',
        data: {name: $('#name').val()},
        url: 'signup_backend.php',
        success: function(data, textStatus, jqXHR){

        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('error');
        }
    });
});
于 2012-09-27T14:57:52.183 回答
2

您是否尝试过在处理程序结束时返回 false (对于提交函数)?

于 2012-09-27T14:58:19.997 回答
1

看起来您并没有阻止submit事件的默认行为(提交表单),因此在提交表单时您会同时收到 AJAX POST 请求和正常的 GET 表单提交。

添加这个:

e.preventDefault();

在您的submit事件处理函数中,以防止默认表单提交并发送 AJAX POST 请求。

于 2012-09-27T14:58:22.573 回答
0

另一种依赖于jQuery.ajax([settings]).

$.ajax({
    processData : false
)};

网址:http ://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings

于 2018-01-19T22:18:30.057 回答
0

您可以method='POST'form标签中定义。这也将规定类型,而不将其放在 jquery 函数中。

<form id="form-signup" action="POST">

如下:

<form id="form-signup" action="POST">
    <fieldset>
        <label for="name">Nome</label>
        <input type="text" id="name" name="name" value="<?=$name?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="email">Email</label>
        <input type="email" id="email" name="email" value="<?=$email?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="tel">Telefone</label><input type="tel" id="tel" name="tel" value="<?=$tel?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <div>
            <label class="blocker">Tipo de anunciante</label><span class="required">*</span>
        </div>
        <input type="radio" name="advertiser" id="advertiser_1">
        <label for="advertiser_1" class="aleft">Particular</label>
        <input type="radio" name="advertiser" id="advertiser_2">
        <label for="advertiser_2" class="aleft">Imobiliária</label>
    </fieldset>
    <fieldset>
        <label for="password">Senha</label>
        <input type="password" id="password" name="password" value="<?=$password?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <label for="check-password">Confirmação da senha</label>
        <input type="password" id="check-password" name="check_password" value="<?=$check_password?>"><span class="required">*</span>
    </fieldset>
    <fieldset>
        <input type="submit" value="Enviar" id="sub">
    </fieldset>
</form>
于 2017-09-11T12:37:35.677 回答