Link to home
Create AccountLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

simple Ajax program not working

I am writing a simple AJAXprgram but that is not working.
Can you please let me know where I am missing:

<script type="text/javascript">
 function jqsubmit(){
             jQuery.ajax({
                   type:"POST",
                   url:"yahoocontact.php",
                   data:"text="+ value,
                   timeout:3000,
                   success: function()
                   { $("#results").html("<p>hai</p>")}                   
             })}
</script>


I have in my form:
<tr>
                    <td colspan="2" align="center"><input type="submit" onclick="jqsubmit()" name="Submit" value="Submit" /></td>
                    </tr>
                </table>  
                      
                </form>

---
Created a div after Form:
    <div id="results">
    <p> this is the default text</p>
    </div>
   
--
Yahoocontact.php is :


<?php


      $name1 = $_POST['name'];
      $email1= $_POST['email'];
      $emailError = "";
      $validated = true;

      
if($validated)
{
snip
}
else
{
snip
}


$response= "Thanks Sir";
echo $response;



                   else\
{
?>
</span></p>
<p>&nbsp;</p>
<p>
<?PHP
}
?>

Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

In your jqsubmot function, the value variable is not defined. (Also couple semicolons are missing, but nowadays browsers excuse that).

function jqsubmit(){
     var value='some value'; // <-- Initializing the variable "value" used in post data
     
             jQuery.ajax({
                   type:"POST",
                   url:"yahoocontact.php",
                   data:"text="+ value,
                   timeout:3000,
                   success: function()
                   { $("#results").html("<p>hai</p>");}                   
             });}

Open in new window

Avatar of sigma19

ASKER

Thanks sudhakara.
I did try with ur suggestion of initializating the variable. I still dont see the out put
Avatar of sigma19

ASKER

is this declaration correct:
<tr>
                    <td colspan="2" align="center"><input type="submit" onclick="jqsubmit()" name="Submit" value="Submit" /></td>
                    </tr>
                </table>  
                     
                </form>
Avatar of mydropz
mydropz

i don't know how your form looks like but you can send al the form data with
function jqsubmit(){
     
     
             jQuery.ajax({
                   type:"POST",
                   url:"yahoocontact.php",
                   data:$('#form_id').serialize(), //use serialize to send the form values
                   timeout:3000,
                   success: function()
                   { $("#results").html("<p>hai</p>");}                   
             });}

Open in new window

instead to put the function in the onclick of the form you can bind it to the form submit event
$('#form_id').submit(function(){
jqsubmit();
return false;
});

Open in new window

Avatar of sigma19

ASKER

Thanks mydropz.
what does serialize do? I tried with that but no change.?

script:
             jQuery.ajax({
                   type:"POST",
                   url:"yahoocontact.php",
                   data:$('form123').serialize(),
                   timeout:3000,
                   success: function(success)
                   { $("#results").html("<p>hai"+success+"</p>")}                   
             })}
here is my form:

 <form id="form123" method="post" action="post" >

            <table width="53%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="16%">Name   </td>
                    <td width="84%"><input name="name" type="text" id="name" /></td>
                </tr>

               <tr>
                <td>Comments</td>
                <td><label for="Comments"></label>
                <textarea name="comments" id="comments" cols="45" rows="5"></textarea></td>
             </tr>
                  <tr>
                    <td colspan="2" align="center"><input type="button" onclick="jqsubmit()" value="Submit1" /></td>
                    </tr>
                </table>  
                      
                </form>

----
you forget the # tag
$('#form123').serialize() // don't forget #

Open in new window


more on jquery serialize
Avatar of sigma19

ASKER

I tried with this:

             jQuery.ajax({
                   type:"POST",
                   url:"yahoocontact.php",
                   data:$("#form123").serialize(),
                   timeout:3000,
                   success: function(success)
                   { $("#results").html("<p>hai"+success+"</p>")}                   
             })}

I can see in Post that the variables are been send (using firebug) . but there is no response from script.

my script is :
<?php


      $name1 = $_POST['name'];
      $email1= $_POST['email'];
      $emailError = "";
      $validated = true;

     
if($validated)
{
snip
}
else
{
snip
}


$response= "Thanks Sir";
echo $response;



                   else\
{
?>
</span></p>
<p>&nbsp;</p>
<p>
<?PHP
}
?>

is there anything I am missing there
Avatar of sigma19

ASKER

Thanks mydropz:.
I found after posting that I missed the tag :-)
ASKER CERTIFIED SOLUTION
Avatar of mydropz
mydropz

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sigma19

ASKER

Thanks mydropz. that was the issue:) I forgot to remove, initially I had them to check the post.