PDA

View Full Version : Post data with ajax. No output no error



vinpkl
05-01-2015, 10:24 AM
Hi

I was to post data via ajax.

But i neither its submitting anything , nor its showing any error



<script type="text/javascript">
$(document).ready(function(){
$("#newsignup_bt2").click(function(){
var formData = {nemail_news2: '<?php echo $_POST['nemail_name2']; ?>',nemail_email2: '<?php echo $_POST['nemail_email2'] ?>'}; //Array

$.ajax({
url : "home_news_signup.php",
type: "POST",
data : formData,
success: function(data, textStatus, jqXHR)
{
$("#home_result").html(data);
},
error: function (jqXHR, textStatus, errorThrown)
{

}
});
});
});
</script>


Here is the form


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div id="home_result"></div>
<form method="post">
<input type="text" name="nemail_name2" id="nemail_name2" class="form-input" value="Name">
<input type="text" name="nemail_email2" id="nemail_email2" class="form-input" value="Email" >
<input type="submit" value="SIGNUP NOW" name="newsignup_bt2" id="newsignup_bt2">
</form>
</body>
</html>


Thanks
Vineet

vinpkl
05-01-2015, 10:36 AM
When i put



error: function (jqXHR, textStatus, errorThrown)
{
alert( "error" );
}


Its showing an alert both and in firefox console Its now showing

uncaught exception: out of memory

Thanks
Vineet

jscheuer1
05-01-2015, 01:06 PM
Where is this script? As far as I can tell, formData has two items in it, both of which will be given their values from PHP post data, which might not exist and/or might not have values for those things at the time the page is parsed. After the page is parsed, it's too late for PHP to do anything. Normally one would want to be getting those values from the form anyway. Why aren't you doing that? Also, if you want to post via AJAX, you usually do that on submit of the form, with the form's normal submission event prevented. You are using the submit button's click event, which cannot reliably be used to prevent the form from submitting (no effort seems to be made at that anyway though perhaps it should), so the form will also be submitting in the normal way, probably to itself, but that's unclear because there's no action attribute specified for it.

It's hard to see how this would cause an out of memory error though, unless somehow something is setting up an endless loop of some kind.

I could be wrong, but I think you are misunderstanding what posting via AJAX can do.

And I'm not sure what you hope to accomplish, could you explain that in plain language - what do you want this code to do. Is home_news_signup.php a separate page, or is it the page this code is on?

Once I know what you want it to do and can see all of the code (a link to the page, and possibly a look at the PHP code on home_news_signup.php), then I could probably figure out what needs to be done.

vinpkl
05-01-2015, 03:04 PM
Hi jscheuer

Let me make it clear.

I have a newsletter form in the footer of every page. I want to submit that form without refreshing the page. So i am posting it using ajax.

I was able to find a code which works for me but only on homepage. Dont know why its showing strange behaviour on inside pages.



<script type="text/javascript">
$(document).ready(function() {
$("#newsignup_bt").click(function() {
$("#footer_result").show();
$("#footer_result").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "home_news_signup_footer.php",
data: $("#newslet_footer").serialize(),
cache: false,
success: function(data) {
$("#footer_result").html(data);
document.getElementById('foot_name').value='';
document.getElementById('foot_email').value='';
}

});

return false;
});

});
</script>



HERE IS PHP


<?php
$foot_name = $_REQUEST['foot_name'];
$foot_email = $_REQUEST['foot_email'];

$qry = "Insert into table(Name,Email) VALUES('$foot_name','$foot_email')";
if(mysql_query($qry))
{
echo $msg = "<p>Your Name Email added to Database. Thanks</p>";
}

?>

Now what is happening is that this script is working perfectly on HOMEPAGE and sending and submitting data to database perfectly.

But when i go to inside pages and click the submit button, strange thing happens is

Like if i go to about us page or contact us page and these pages also have same form in the footer.

When i click the submit button on the about us page, Then the full about us page itself gets displayed in the RESULT DIV and form doesnt get submit.

RESULT DIV (#footer_result) is the DIV which displays the "success" OR "error" message.

I Dont why is this happening. I have never experienced this kind of thing.

Hope you will be able to help me.

Thanks
Vineet

jscheuer1
05-01-2015, 09:28 PM
Are the inside pages in different folder than the home page? If so, you should use what is known as the network path to the home_news_signup_footer.php file. I'm assuming it is in the root of the domain. If so, change (in the javascript):


url: "home_news_signup_footer.php",

to:


url: "/home_news_signup_footer.php",

If not, supply the path to it from the root after the red / which represents the root.

After the appropriate changes, any pages that were giving you this problem may need to be refreshed before they can work properly. The browser cache may also need to be cleared.

If you want more help. please post a link to the pages.


Oh, and BTW - By returning false as I see now (but not shown in the earlier posts in this thread), you are preventing the default submission in most cases. It's still better to do all this on submit of the form, rather than on click of a button. But most browsers will work it out with click. Once we resolve the problem here, we can deal with this (which event upon which element) if you're still interested.

vinpkl
05-02-2015, 12:27 PM
Thanks jscheuer

Its was exactly the path issue. The problem is solved now.

Thanks
Vineet