Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. After success function data add into the database but it does not display any messages and complete 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 complete data appends to the url. As shown in fig.enter image description here Why this happens? and why it does not display proper message to result field.

Thanks in advance.

like image 861
chetan Avatar asked Jun 07 '16 07:06

chetan


2 Answers

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" ....
like image 177
mrmemio29 Avatar answered Sep 26 '22 02:09

mrmemio29


use method="post" in html form .

like image 34
Abhijit Gujar Avatar answered Sep 23 '22 02:09

Abhijit Gujar