Results 1 to 6 of 6

Thread: Post data with ajax. No output no error

  1. #1
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Post data with ajax. No output no error

    Hi

    I was to post data via ajax.

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

    Code:
    <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
    Code:
    <!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

  2. #2
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    When i put

    Code:
    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

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    <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
    Code:
    <?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
    Last edited by vinpkl; 05-01-2015 at 03:17 PM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    url: "home_news_signup_footer.php",
    to:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks jscheuer

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

    Thanks
    Vineet

Similar Threads

  1. Resolved best practice login (secure data) via ajax - POST JSON AJAX
    By lse123 in forum JavaScript
    Replies: 12
    Last Post: 01-01-2013, 12:22 AM
  2. Resolved Ajax/jQuery Post Data not working ?
    By bennyy007 in forum JavaScript
    Replies: 0
    Last Post: 08-10-2009, 02:20 PM
  3. Ajax script error: POST method not allowed
    By b4greg in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 04-17-2008, 01:57 PM
  4. Grouping Output Data
    By oriecat in forum MySQL and other databases
    Replies: 2
    Last Post: 09-22-2006, 05:00 PM
  5. data output help
    By soddengecko in forum MySQL and other databases
    Replies: 4
    Last Post: 09-14-2006, 02:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •