Passing an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Passing an array

Post by llimllib »

So here's my question to you guys: If you pass a registration form from one page to another, then find that the username is taken, what's the most efficient way to pass that array back to the referer so that the fields will be automagically filled in (by me)? Can you post without writing a curl script to do it? You don't want to use the URL because that information shouldn't be available in the history.
Alternatively, would you check the database from the original page before posting the data? I think that's what I'm leaning towards doing, as it makes everything simpler, but it doesn't seem very elegant to me. Let me know what you think.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You don't really have a choice as you will need to post the page for PHP to do any kind of checking, PHP is server based.

You could do the check, if any errors then display a message that tells them to use the back button (values will still be in the form when they use the back button).

or

The php page calls itself, does the checks if any errors redisplays your form populating values that have been passed to it anyway, if no errors maybe call another page using

header("location:newpage.php");
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Well, I know. What I wanted to do was to turn red the items that were incorrectly filled out. To do that, instead of using header, you can use CURL to send an array of post variables back to the original page, have it check which ones are erroneous, and display the labels in red. All I was wondering was if anyone knew a better way to do that than I could come up with.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

ill try to write something to give you the correct idea..

Code: Select all

<?
$action = $_POST&#1111;'action'];
$required = $_POST&#1111;'required'];

if($action == "go")&#123;
 if($required != "")&#123;
//execute registration
&#125; else &#123;
   echo "<form action="$PHP_SELF" method="POST">
   if($required == "")&#123;
   //display red input
   &#125; else &#123;
   echo "<input type="text" name="required" value="$required" />";
   &#125;
   echo "<input type="hidden" name="action" value="go" />";
   echo "<input type="submit" />";
   echo "</form>";
&#125;
&#125; else &#123;
?>
<form action="<?=$PHP_SELF;?>" method="POST">
<input type="text" name="required" />
<input type="hidden" name="action" value="go" />
<input type="submit" />
</form>
<?
&#125;
?>
somebody will probably come along and write a more efficient one though
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

hmmm

Post by llimllib »

Well, the problem is that that script gets really nasty when you're doing complex validation on 15 variables. I was shooting for separating the validation/database insertion into another file, making everything more elegant. To do that, I still don't see any way but using CURL to post the variables back to the original page, but that's a really ugly solution, as it requires me to go back through them again on the referring side.
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

how about making them session variables
ajaypatil
Forum Newbie
Posts: 17
Joined: Wed Jul 03, 2002 2:15 am
Location: Pune, India
Contact:

Java-Struts

Post by ajaypatil »

Java-Struts has a nice validation framework that automatically shows
the same form again if some values are incorrect.

In PHP, we can maybe pack all the input values into a PHP class
object and put it in session.

When form is shown, always fill in the form input fields from
the object in session. First time, when there is no object in session,
simply create a blank object i.e one with all fields blank.

Ajay
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

no kidding, you can put an object in a session? that's hot...
Post Reply