Abeer Waseem
Abeer Waseem

Reputation: 72

jquery/ajax form submission not working

i got a jquery code for form submission from some tutorial websites, but the ajax was not working i guess. It happens nothing when i submit the form. I tried everything but no result.

here is my jquery code.

    $("#summary").submit(function (event) {
    event.preventDefault();
    var name = $("#name").val();
    var address = $("#address").val();
    var landline_number = $("#landline_number").val();
    var mobile_number = $("#mobile_number").val();
    var profile_email = $("#profile_email").val();
    var profile_statement = $("#profile_statement").val();
    var dataString = 'name='+name+'&address='+address+'&landline_number=' + landline_number + '&profile_email=' + profile_email + '&prof  ile_statement=' + profile_statement + '&mobile_number=' + mobile_number;
    $.ajax({
        type: 'POST',
        data: dataString,
        url: 'test2.php',
        success: function (data) {
            $('#hello').html(data);
        }
    });
});

Html Code

<form name="summary" id="summary" action="" method="POST"> 
 <div class="profile-header">

  <table  width="740">
  <tr>
  <td>
  <h2>Personal Detail:</h2>
  </td>
  <td>
  <input type="text" id="name" name="name" />
  </td>
  </tr>
  </table>
  </div>
  <div class="profile-table-hide">

  <table  width="740">
  <tr>
    <td width="297"><p>Address Location</p></td>
    <td width="10" class="divider"></td>
    <td width="428"><input id="address" type="text" name="address" /></td>
  </tr>
  <tr>
    <td><p>Landline Number</p></td>
    <td class="divider">&nbsp;</td>
    <td><input type="text" id="landline_number" name="landline_number" /></td>
  </tr>
  <tr>
    <td><p>Mobile Number</p></td>
    <td class="divider"></td>
    <td><input type="text" id="mobile_number" name="mobile_number" /></td>
  </tr>
  <tr>
    <td><p>Email Address</p></td>
    <td class="divider">&nbsp;</td>
    <td><input type="text" id="profile_email"  name="profile_email" /></td>
  </tr>
  <tr>
    <td><p>Personal Statement</p></td>
    <td class="divider">&nbsp;</td>
    <td><textarea id="profile_statement" name="profile_statement"></textarea></td>
  </tr>
  <tr>
    <td></td>
    <td class="divider">&nbsp;</td>
    <td><input type="button" id="save1" value="Save" name="save1" /></td>
  </tr>
</table>
</div>
</form>

Php File

$sql = "INSERT INTO `profile_detail` (`ref`,`p_name`,`p_mob`,`landline_number`,`p_email`,`p_add`,`p_statement`) VALUES 
    ('$user_app_id','".$_POST['name']."','".$_POST['mobile_number']."','".$_POST['landline_number']."',
     '".$_POST['profile_email']."','".$_POST['address']."','".$_POST['profile_statement']."') ";
if (mysql_query($sql)) {
echo "<script>window.top.location='index'</script>";
  }
else {
echo mysql_error();
}

Upvotes: 0

Views: 3299

Answers (4)

user7789076
user7789076

Reputation: 798

There are two mistake in you code

First Change

1.<input type="button" id="save1" value="Save" name="save1" />

to

<input type="submit" id="save1" value="Save" name="save1" />

and second

$('#hello').html(data);

I cant see any hello id in your form so make one like

<div id="hello"></div> 

Thanks

Upvotes: 0

Johan
Johan

Reputation: 1026

Clicking an input element with type="button" won't submit the form. You need to change it to type="submit".

<td><input type="submit" id="save1" value="Save" name="save1" /></td>

Upvotes: 3

Atutouato
Atutouato

Reputation: 375

I think your problem is that you're running the code from file:// protocol. Server scripts like .php need http:// protocol.
In order to run this, look into Apache and try running that server. For Linux, you should put your files into /var/www, but for other operating systems, I'm not sure.
Good luck!

Upvotes: 0

Murali Murugesan
Murali Murugesan

Reputation: 22619

Try using serializeArray() to post your data

$.ajax({
    type: 'POST',
    data: $('#summary').serializeArray()
    url: 'test2.php',
    success: function (data) {
        $('#hello').html(data);
    }
});

Upvotes: 0

Related Questions