Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts

    should i use ajax for this?

    Hello.

    Before I spend hours and hours trying to learn some AJAX could anyone tell me if this is the right method for my problem!

    Basically I have an event page where i want people to select if they are 'attending' or 'not attending' by pressing the relevant button. When selected i want it to send some SQL/PHP to my database to update the 'attending' table. I subsequently want the button to then change in relation to which one they've selected (ie - the one they select becomes highlighted)

    I've tried this with simple html POST forms with update.php as the action (for example), which works... however i don't want the whole page to refersh as you get the 'resend information' prompt if the user tried to press back or forward on the browser.

    SO... from what I read, AJAX allows you to stay on the same page? yar?

    If you're feeling very very helpful, and pointers on how to achieve it would be great.. but so long as I know AJAX is the right method then I'll press on with trying to learn it!

    also: an example of people doing EXACTLY what i want is here (where you can see attendance options top left) http://www.bandsintown.com/event/414...n-june-01-2011

    Many thanks,
    Pat.
    Last edited by paddyfields; 02-07-2011 at 01:35 PM.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    The site you linked to doesn't use AJAX. The reason why there are no "resend information" prompts is simply that the page where the form is submitted to redirects you to the page where you came from after processing the form submission, so it won't end up in the browser history. That's all there is to it.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    i'm going to have to try this when i get home, but i'm pretty sure this is what i was trying before, and it was still appearing in the browser history.

    the method i was using was to 'post' the html form using '#', and then atthe top of the page have an if(isset('submit')) .. { followed by the php code }... and then header location: $currentpage..

    but this method would create another instance of the page?

    do you know where i could find any examples at all? thank you very much by the way.. if i can ge this working i'll be over the moon.

    cheers.

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    do you mean the page that is requested during the 'post' includes some script such as "javascript:history.go(-1)" ?

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    No, I don't mean that at all. It's just a PHP redirection, like you described in your previous post.

    You must have done it wrong, somehow, because it works perfectly for me:


    Code:
    <?php
    
    session_start();
    if ($_POST) {
    	$_SESSION['submitted'] = TRUE;
    	header('Location: '.basename(__FILE__));
    	die();
    }
    
    ?><!doctype html>
    <html>
    
    <head>
    	<title>Form submission without "resend information" prompt</title>
    </head>
    
    <body>
    	<?php if (isset($_SESSION['submitted'])) echo 'form has been submitted'; ?>
    	<form action="#" method="POST">
    		<div><input type="submit" name="submit" /></div>
    	</form>
    </body>
    
    </html>
    I used the session to persist whether the form has been submitted, so the information is available after the redirection. You'd probably want to use your database for that.

  • Users who have thanked venegal for this post:

    paddyfields (02-07-2011)

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    thank you very much, you've saved me alot of headache. i can't try it now because i'm at work.. but i trust it will work!

    many thanks.

  • #7
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    ah... just a thought. would header('Location: '.basename(__FILE__));
    work on pages with query strings in the address?
    Last edited by paddyfields; 02-07-2011 at 04:17 PM.

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    after all that it wasn't even to do with the php redirect.. my original code was right. it was an if statement problem displaying the submit button images.. how frustrating.


  •  

    Posting Permissions

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