0

I don't know what i did wrong in the ajax call. I want to return the data back to my script and work with it but its not returning result. Here are some things i will like to clarify as well as i am new to ajax programming. if the ajax call is successful and it returns result to the script. Where in the script will the result be. Will i have to create an array that holds the result or ... Here is the html

   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="reserveform">
    <div id="response"></div>
        <div>
        <label>Name</label><input type="text" name="name" id="nameid">
    </div>
    <div>
        <label>Phone Number</label><input type="text" name="phone" id="phoneid">
    </div>
        <div>
        <button type="submit" class="btn" id="btnsubmit">Submit</button>
    </div>
   </form>
         <div id="success"></div>


//my script

$('#btnsubmit').click(function(e){
    e.preventDefault();
        var nameid = $('#nameid').val();
    var phoneid = $('#phoneid').val();
        var validate_msg = '';
       if (nameid == ''){
        validate_msg = '<p> Name is Required. </p>';    
    }
    if (phoneid == '' || ($.isNumeric(phoneid) == false)){
        validate_msg += '<p> Phone field is either empty or non numeric. </p>';
    }

    if (validate_msg != ''){
    $('#response').addClass('error').html('<strong>Please correct the errors below </strong>' +  validate_msg);
    } else {
        var formData = $('form #reserveForm').serialize();
        submitForm(formData);
    }
});


    function submitForm(formData){
        $.ajax({
            type: 'POST',
            url: 'reservation.php',
            data:formData,
            dataType:'json',
            success: function(data){
                $("#success").html(data);
            },
            error: function(XMLHttpResponse, textStatus, errorThrown){
                    $('#success').removeClass().addClass('error').html('<p>There was an ' + errorThrown + ' due to ' + textStatus + 'condition');
            }
        });

    }
DiD
  • 77
  • 1
  • 11
  • If the name of your PHP file (`$_SERVER['PHP_SELF']`) is `reservation.php`, then please see my answer below for a solution. – cssyphus Mar 05 '14 at 16:28
  • Is this question solved to your satisfaction? If so, please accept an answer to close the question. Otherwise, please post additional information so that we can help you further. – cssyphus Mar 03 '15 at 04:53

4 Answers4

1

If you are trying ajax form submission and need to populate the result in the success div without refresh, you need return false

Don't use

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="reserveform">

Here don't need the action in form, try like

<form id="reserveform" onsubmit="return submitForm();">

In function use form serialize so all input elements value in form will available

function submitForm(){
        var formData = $("#reserveform").serialize();
        $.ajax({
            type: 'POST',
            url: 'reservation.php',
            data:formData,
            dataType:'json',
            success: function(data){
                $("#success").html(data);
            },
            error: function(XMLHttpResponse, textStatus, errorThrown){
                    $('#success').removeClass().addClass('error').html('<p>There was an ' + errorThrown + ' due to ' + textStatus + 'condition');
            }
        });
        return false; // use this otherwise the form will be submitted and results an entire page refresh
    }
Ajith S
  • 2,907
  • 1
  • 18
  • 30
0

please try this

you have to put the btnsubmit click event inside onload or document ready

 $(document).ready(function()
    {

     $('#btnsubmit').click(function(e){
        e.preventDefault();
            var nameid = $('#nameid').val();
        var phoneid = $('#phoneid').val();
            var validate_msg = '';
           if (nameid == ''){
            validate_msg = '<p> Name is Required. </p>';    
        }
        if (phoneid == '' || ($.isNumeric(phoneid) == false)){
            validate_msg += '<p> Phone field is either empty or non numeric. </p>';
        }

        if (validate_msg != ''){
        $('#response').addClass('error').html('<strong>Please correct the errors below </strong>' +  validate_msg);
        } else {
            var formData = $('form #reserveForm').serialize();
            submitForm(formData);
        }
    });

    }

    );
java seeker
  • 1,246
  • 10
  • 13
  • I tried to hide the form and display a message if the request is successful. Each time i attempt it, it doesn't hide neither does it display the msg. I'm i doing something wrong? – DiD Mar 07 '14 at 03:27
  • I tried making necessary corrections and creating an external file but i'm getting this weird error msg. "{"error": "Shell form does not validate{'html_initial_name': u'i..." Here is the link http://jsfiddle.net/2t7GF/ – DiD Mar 07 '14 at 03:57
0

You cant use like this $("#success").html(data); because data is in json format. here, the data is an object. You need to parse it and assign to tags.

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
0

I see you configured the form to submit to the same document that you are in already. This is fine, we do this all the time.

However, you are also stopping the form from submitting and then using AJAX to submit the values. You specify reservation.php as your ajax processor. Is this the same PHP page you are on already? If so, then I know what the problem is: you can't do that.

By design, AJAX must use an external PHP file to process the AJAX. If you try to do this inside the same document, the AJAX will only echo back the full HTML of the document itself.

Short answer: use an external PHP file. This is by design.

See this other answer for more information about exactly this issue.

See these other posts for further info about AJAX and getting info into the PHP processing script, and returning info back from the PHP processing script:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Thanks, i used an external script called process.php. How do i get the data in the new script. Do i still need to get the data manually using $_POST of ajax sends the value for me to use directly. – DiD Mar 06 '14 at 17:16
  • @DiD See additional examples added to my answer, above – cssyphus Mar 06 '14 at 18:52