0

issues are still there...pls help

I am unable to load external file while using AJAX jquery. I want to use Jquery ajax to pop up form then validate, enter data in mysql. but starting from a simple ajax function. kindly let me know where i am going wrong

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>

and contact.php file is

<?php
echo "Hello";
?>

4 Answers 4

3

You need to return false; to prevent the form from submitting and refreshing the page and check if your $("#div1") is missing.

$(document).ready(function(){
     $("#ajax-contact-form").submit(function(){
         var str = $(this).serialize();
         $.ajax(
         {
             type: "POST",
             url:"contact.php",
             data: str,
             success:function(result)
             {
                  $("#div1").html(result);
             }
          });
      return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

please mention missing div1
@legendinmaking: i think the div should be somewhere else that the OP does not show in his code. Anyway, updated the code to remind him in case he forgot. Thanks.
0

Simple. Since you are posting your form through ajax, you must prevent the default form submit by returning a false inside the submit method. Below is the correct version:

<script>
$(document).ready(function(){
  $("#ajax-contact-form").submit(function(){
   var str = $(this).serialize();
  $.ajax({
     type: "POST",
     url:"contact.php",
     data: str,
     success:function(result) {
        $("#div1").html(result);
     }
  });
  return false;
 });
});
</script>

Comments

0

You can use a more simple form of post request as follows:

$.post("url",{var1: value1, var2: value2},function(data,status){
 if(status=='success')
   alert(data);
});

the second argument you can pass as many using this post request. The first argument url, if ofcourse relative to the document in which this js is loaded or you can give the exact url on the server.

According to your php file, data=='Hello'.

Similar is the procedure for any GET request also.

Comments

0

Make sure you are missing the div1

please use

<div id="div1"><div>

1 Comment

Sorry you are using ajax submit $("#ajax-contact-form").submit(function(){ you are unable to show the result. You need to use return false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.