Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling PHP file using AJAX

I'm trying to call a php file using the $.ajax() function in jQuery however it is not working. The following code is run when a button on the page is clicked:

if($error === false) {
        alert($error);
        $.ajax({
            url: '/new-user.php',
            type: 'POST',
            data: {
                name: $('#name').val(),
                email: $('#name').val(),
                password: $('#name').val()
            }
        });

This is my form:

<form onClick="return false;" class="reg-form">

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="name">First Name</label>
                <input type="text" id="name" class="form-control" autofocus="autofocus">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="email">Email Address</label>
                <input type="text" id="email" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" class="form-control">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="password-confirm">Confirm Password</label>
                <input type="password" id="password-confirm" class="form-control">
            </div>
        </div>
    </div>

    <button class="btn trans reg-btn">Sign Up</button>

    <div class="reg-msg">
        <span id="password-error-msg">Passwords do not match.</span>
    </div>

</form>

I have set the form's onClick to return false so that the form does not reload the page when submitted so that the jQuery can run.

Any help is greatly appreciated.

like image 609
stu177 Avatar asked Aug 30 '13 17:08

stu177


2 Answers

The main reason why people use forms is so that you can define an

action (in your case a php script),

method (GET or POST) and a

submit button that captures all the information in the form and automatically sends it to the server.

This is nice when you have 20-30 inputs or you are handling multiple file inputs. In your case you are handling the data retrieval in your ajax function, you have no submit button, action or method defined so you could make things a lot easier by not using a form at all....

    $.ajax({
        url: '/new-user.php',
        type: 'POST',
        dataType: "json",
        data: {
            name: $('#name').val(),
            email: $('#email').val(),
            password: $('#password').val()
        }
    }).done(function(data){
            alert(JSON.stringify(data));
    });

I've left out the formatting divs for simplicity...

<input type="text" id="name" class="form-control" autofocus="autofocus">
<input type="text" id="email" class="form-control">
<input type="password" id="password" class="form-control">

The above code will grab the data in your inputs, send it to the php script and output the data that is sent back from your php script in the alert.

And most importantly your page will NOT refresh!

like image 172
A.O. Avatar answered Sep 21 '22 15:09

A.O.


I actually serialize my form data because it's less code. Plus you can use it with multiple forms. For instance you can have a hidden input which holds a task number and have one php script to delegate tasks. Or you can have another variable for the submit function to pass a your script's location. Therefore I submit my forms like the following.

HTML:

<form onsubmit="submitForm('#myForm'); return false;" id='myForm'>
     <input type='text' name='name'/>
     <input type='text' name='email'/>
     <input type='text' name='password'/>
     <input type='submit'/>
</form>

Javascript:

function submitForm(formId){
     var formData = $.(formId).serialize();

     $.ajax({
        url: '/script.php',
        type: 'POST',
        data: formData,
        success:function(response){
           alert(response);
       }
   });

}
like image 44
evan.stoddard Avatar answered Sep 21 '22 15:09

evan.stoddard