...

View Full Version : should i use ajax for this?



paddyfields
02-07-2011, 02:32 PM
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/4141470-fleet-foxes-london-united-kingdom-live-at-hmv-hammersmith-apollo-on-june-01-2011

Many thanks,
Pat. :thumbsup:

venegal
02-07-2011, 02:57 PM
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.

paddyfields
02-07-2011, 03:34 PM
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.

paddyfields
02-07-2011, 03:41 PM
do you mean the page that is requested during the 'post' includes some script such as "javascript:history.go(-1)" ?

venegal
02-07-2011, 03:58 PM
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:



<?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.

paddyfields
02-07-2011, 04:06 PM
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.

paddyfields
02-07-2011, 04:14 PM
ah... just a thought. would header('Location: '.basename(__FILE__));
work on pages with query strings in the address?

paddyfields
02-07-2011, 08:31 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum