最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - After submit form using post method form data appends to url in php why? - Stack Overflow

programmeradmin4浏览0评论

I am developing an application in which I send form data using ajax. After success function data add into the database but it does not display any messages and plete form data appends to url. In this I am using "POST" method. Why this data appends to form and not showing any messages to result field.

This is my html code

<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="[email protected]">
        </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.

ajax function

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 plete data appends to the url. As shown in fig. Why this happens? and why it does not display proper message to result field.

Thanks in advance.

I am developing an application in which I send form data using ajax. After success function data add into the database but it does not display any messages and plete form data appends to url. In this I am using "POST" method. Why this data appends to form and not showing any messages to result field.

This is my html code

<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="[email protected]">
        </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.

ajax function

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 plete data appends to the url. As shown in fig. Why this happens? and why it does not display proper message to result field.

Thanks in advance.

Share Improve this question edited Jun 7, 2016 at 7:21 rrk 15.9k4 gold badges30 silver badges47 bronze badges asked Jun 7, 2016 at 7:15 chetanchetan 2493 silver badges6 bronze badges 4
  • Could you share your PHP data? At least the response? – Jurik Commented Jun 7, 2016 at 7:27
  • 1 And you should prevent the default use of form in your processForm function e.preventDefault() - otherwise your form is submitted too. – Jurik Commented Jun 7, 2016 at 7:43
  • Your form is being submitted and your ajax function was never called, or form is submitted after your ajax function is called. – Pardeep Poria Commented Jun 7, 2016 at 7:50
  • What do you mean by After submit I call js file in which I am using ajax.? Do you mean you want to do first an HTML submit and after that call another script via AJaX? Or did you actually mean Instead of submit I want to call a a script via AJaX? – Marc Compte Commented Jun 7, 2016 at 8:05
Add a ment  | 

2 Answers 2

Reset to default 5

In short because there is no real error. PHP is not processing your request and so goes about its normal business. The URL has the POST data stuck in there as it normally would, only it isnt being flushed out since it isnt being redirected.

This is because you haven't actually specified a method for the form.

You need to use

   <form method="post" ....

use method="post" in html form .

发布评论

评论列表(0)

  1. 暂无评论