简体   繁体   English

使用post方法提交表单后,表单数据会附加到php中的url为什么?

[英]After submit form using post method form data appends to url in php why?

I am developing an application in which I send form data using ajax. 我正在开发一个应用程序,我使用ajax发送表单数据。 After success function data add into the database but it does not display any messages and complete form data appends to url. 成功后,函数数据会添加到数据库中,但不会显示任何消息,并且完整的表单数据会附加到url。 In this I am using "POST" method. 在这我使用“POST”方法。 Why this data appends to form and not showing any messages to result field. 为什么此数据附加到表单而不向结果字段显示任何消息。

This is my html code 这是我的HTML代码

<form  class="form-horizontal form-label-left" id="myForm" novalidate>
    <span class="section">Personal Info</span>

    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fname"> First Name <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="fname" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="1" name="fname" placeholder="First Name" required="required" type="text">
        </div>
    </div>

    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="lname"> Last Name <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="lname" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="1" name="lname" placeholder="Last Name" required="required" type="text">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="username"> Username <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="username" class="form-control col-md-7 col-xs-12" data-validate-length-range="0,25" data-validate-words="1" name="username" placeholder="Username" required="required" type="text">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="email" id="email" name="email" required="required" class="form-control col-md-7 col-xs-12" placeholder="abc@gmail.com">
        </div>
    </div>
    <div class="item form-group">
        <label for="password" class="control-label col-md-3">Password <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="password" type="password" name="password" data-validate-length="6,8" class="form-control col-md-7 col-xs-12" required="required">
        </div>
    </div>
    <div class="item form-group">
        <label for="password2" class="control-label col-md-3 col-sm-3 col-xs-12">Repeat Password</label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="password2" type="password" name="password2" data-validate-linked="password" class="form-control col-md-7 col-xs-12" required="required">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="telephone">Telephone <span class="required">*</span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="tel" id="telephone" name="telephone" required="required" data-validate-length-range="8,20" class="form-control col-md-7 col-xs-12">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="status">Status</label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <select id="status" name="status" class="form-control col-md-7 col-xs-12">
                <option>Select Status</option>
                <option value="ACTIVE" >Active</option>
                <option value="INACTIVE">Inactive</option>
                <option value="VACATION">Vacation</option>
            </select>
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="role">Role</label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <select id="role" name="role" class="form-control col-md-7 col-xs-12">
                <option value="ACTIVE" >Select Role Name</option>
                <option value="Manager" >Manager</option>
                <option value="Operator">Operator</option>
                <option value="Admin">Admin</option>

            </select>
        </div>
    </div>

    <div class="ln_solid"></div>
    <div class="form-group">
        <div class="col-md-6 col-md-offset-3">
            <button type="reset" id="reset" class="btn btn-primary">Cancel</button>
            <button id="send" type="submit" class="btn btn-success">Submit</button>
        </div>
    </div>

    <div id="result"></div>
</form>

After submit I call js file in which I am using ajax. 提交后我调用js文件,其中我使用的是ajax。

ajax function ajax功能

function processForm( e ){
    alert(e);
    $.ajax({
        url: 'add_user_check.php',
        dataType: 'text',
        type: 'post',
        contentType: 'application/x-www-form-urlencoded',
        data: $(this).serialize(),
        success: function( data, textStatus, jQxhr )
        {
            alert(data);
            var split = data.split(',');
            var display = split[0];
            var msg = split[1];

            $('#result').html( msg );



        },
        error: function( jqXhr, textStatus, errorThrown ){
            $('#result').html( "Connection error :"+errorThrown);

        }
    });

    e.preventDefault();
}

$('#myForm').submit( processForm );

If data is submitted successfully it returns success message and will display to result field. 如果成功提交数据,则返回成功消息并显示到结果字段。 But message is not display and complete data appends to the url. 但是不显示消息,并且完整数据附加到URL。 As shown in fig. 如图所示。 在此输入图像描述 Why this happens? 为什么会这样? and why it does not display proper message to result field. 以及它为什么不向结果字段显示正确的消息。

Thanks in advance. 提前致谢。

In short because there is no real error. 总之,因为没有真正的错误。 PHP is not processing your request and so goes about its normal business. PHP不处理您的请求,因此正常业务。 The URL has the POST data stuck in there as it normally would, only it isnt being flushed out since it isnt being redirected. URL通常会将POST数据卡在那里,只是因为它没有被重定向而没有被刷新。

This is because you haven't actually specified a method for the form. 这是因为您实际上没有为表单指定方法。

You need to use 你需要使用

  <form method="post" .... 

以html格式使用method =“post”。

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

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