Newbie... Retaining array information

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
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Newbie... Retaining array information

Post by idq »

Hi! I have 2 files, main.html and process.php.

Codes in main.html:
-----------------------------------------------------------
<html>
<body>

<form action="process.php" method="post">
Your name: <input type="text" name="name" /><br/>
Your address: <input type="text" name="address" /><br/>
Your tel number: <input type="text" name="telNum" /><br/>
Your email: <input type="text" name="email" /><br/>
<input type="submit" />
</form>

</body>
</html>
-----------------------------------------------------------

Codes in process.php
------------------------------------------------------------

Code: Select all

<form action="main.html" method="post">

<?php

//validate email
$email = $_POST['email'];

if (!strpos($email, "@") && substr_count($email, ".") != 1) {
	echo "Invalid email! <a href = "question4d.html">Try again.</a>";
	exit();
}

//validate tel no
$telNum = $_POST['telNum'];

if ($telNum{0} != "0") {
	echo "Invalid tel number <a href = "question4d.html">Try again.</a>";
	exit();
}

//greet user
$name = $_POST['name'];
$address = $_POST['address'];

echo "<h3>Hi $name!</h3> <br/>";
echo "$name <br/>";
echo "$address <br/>";
echo "$telNum <br/>";
echo "$email <br/><br/>";

//save to array
$arrayName[] = $name;
$arrayAddress[] = $address;
$arrayTelNum[] = $telNum;
$arrayEmail[] = $email;

//again
echo "<input type="submit" value="Again">";

?>
-----------------------------------------------------------

How can I add new values to the existing arrays after the user click on Again button in process.php, add new data and click on Submit in main.html again?

Thank you.

?>
Last edited by idq on Wed Mar 17, 2004 3:27 pm, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Use the php tags :)

I don't understand your problem... Do you want to return to the main.html page so the user can re-enter the values?

Also, the line break tags should be <br /> not <br/>.

Code: Select all

<html> 
<body> 

<form action="process.php" method="post"> 
Your name: <input type="text" name="name" /><br/> 
Your address: <input type="text" name="address" /><br/> 
Your tel number: <input type="text" name="telNum" /><br/> 
Your email: <input type="text" name="email" /><br/> 
<input type="submit" /> 
</form> 

</body> 
</html> 
----------------------------------------------------------- 

Codes in process.php 
------------------------------------------------------------ 
<form action="main.html" method="post"> 

<?php 

//validate email 
$email = $_POST['email']; 

if (!strpos($email, "@") && substr_count($email, ".") != 1) { 
echo "Invalid email! <a href = "question4d.html">Try again.</a>"; 
exit(); 
} 

//validate tel no 
$telNum = $_POST['telNum']; 

if ($telNum{0} != "0") { 
echo "Invalid tel number <a href = "question4d.html">Try again.</a>"; 
exit(); 
} 

//greet user 
$name = $_POST['name']; 
$address = $_POST['address']; 

echo "<h3>Hi $name!</h3> <br/>"; 
echo "$name <br/>"; 
echo "$address <br/>"; 
echo "$telNum <br/>"; 
echo "$email <br/><br/>"; 

//save to array 
$arrayName[] = $name; 
$arrayAddress[] = $address; 
$arrayTelNum[] = $telNum; 
$arrayEmail[] = $email; 

//again 
echo "<input type="submit" value="Again">"; 

?>
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

I want to retain the array values so that a new user can add new information to the array.

Using the codes now, everytime when I clicked on Again and return to main.html and enter new values, all the arrays will be overwritten.

Basically, at the end of the day, I need to add another function which converts all the array to xml document. This is easy because I found the right resources but first I need you guys to help me on the arrays first.

Thank you.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

idq wrote:I want to retain the array values so that a new user can add new information to the array.

Using the codes now, everytime when I clicked on Again and return to main.html and enter new values, all the arrays will be overwritten.
It sounds like you're going to need to store these values in a database. You probably have access to mySQL.

You could write the entries to a text file, then bring those altogether at the end of the day. But that will be a pain in comparison to using the database, so I wouldn't recommend it.

But you can't just hold them in an array. Every user will have his own copy of the array that will live at best as long as his session does. And even then, you won't have access to the data outside of his session.
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

Whao.. I didn't expect it to be so complicated because this is a lab exercise.

Perhaps knowing the question will help. Here it goes:

1. Develop a form that inputs the following information from the client: Name, Address, Telephone number and email address. Process the form with a PHP script. This script must greet the user and perform some validation of the email address and the telephone code.

2. Develop this idea further storing any users that enter their names and details into an internally stored array. Produce another PHP script which allows this data to be viewed and output as an XML document.

The first one is easy. The problem with the second one is that I need to know how to retain array among the 2 files (main.html, process.php) to get it working.

For the last part where I need to output the data as an XML document. Does php has a built-in function that I can use?

I thought of writing a function by myself in the first place.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

why dont you use one file

ie.

Code: Select all

<?php
if(!isset($_POST['hidden']))
{
<html>
<body>

<form action="process.php" method="post">
Your name: <input type="text" name="name" /><br/>
Your address: <input type="text" name="address" /><br/>
Your tel number: <input type="text" name="telNum" /><br/>
Your email: <input type="text" name="email" /><br/>
<input type="hidden" name="hidden" /><input type="submit" />
</form>

</body>
</html>
}
else
{
//validate email
$email = $_POST['email'];

if (!strpos($email, "@") && substr_count($email, ".") != 1) {
echo "Invalid email! <a href = "question4d.html">Try again.</a>";
exit();
}

//validate tel no
$telNum = $_POST['telNum'];

if ($telNum{0} != "0") {
echo "Invalid tel number <a href = "question4d.html">Try again.</a>";
exit();
}

//greet user
$name = $_POST['name'];
$address = $_POST['address'];

echo "<h3>Hi $name!</h3> <br/>";
echo "$name <br/>";
echo "$address <br/>";
echo "$telNum <br/>";
echo "$email <br/><br/>";

//save to array
$arrayName[] = $name;
$arrayAddress[] = $address;
$arrayTelNum[] = $telNum;
$arrayEmail[] = $email;

//again
echo "<input type="submit" value="Again">";
}
?>
then send the array $vars on through session or something...
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

Does <form action="process.php" method="post"> need to be change to something else?

I have no idea how to call the form itself again.
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post by llanitedave »

If you're designing for multiple users and multiple entries, then you at LEAST need a read/write text file to get the array values from and append them to. A database would be better, but for something this simple it's not really necessary.
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

hmmm... but dull1554 mentioned that it is possible to use session. I'm confused over which implementation to use now.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

idq wrote:hmmm... but dull1554 mentioned that it is possible to use session. I'm confused over which implementation to use now.
A session's only good for one user. As soon as another user comes along, he gets a different session. ASP has an "application"-level thing that might let you keep the values of variables around for the life of the application, but I don't know if PHP has something similar.

For your purposes, the text file would work. You don't have to expect multiple users banging away on the thing, so collisions are unlikely.

If you manage to do it with only sessions, please post back with your code!
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

This is just a lab exercise.

Which means I need to test the array by myself on the same machine.

Can I use session still?

If yes, how do I do it?

Please guide me.

Thank you.
Last edited by idq on Wed Mar 17, 2004 6:11 pm, edited 1 time in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Another idea perhaps...

Code: Select all

<pre>
<?php
    $array = "";
    if (!empty($_POST)) {
        if (!empty($_POST['hidden'])) {
            $array = unserialize(base64_decode($_POST['hidden']));
        }
        $array[] = $_POST['foo'];
        // next line; debugging...
        print_r($array);
        $array = base64_encode(serialize($array));
    }
?>
<form method="post">
 <input type="text" name="foo" />
 <input type="hidden" name="hidden" value="<?php echo $array; ?>" />
 <input type="submit" value="Go" />
</form>
[php_man]serialize[/php_man] the array, and use [php_man]base64_encode[/php_man] on that to get rid of the quotes that will break the html itself, and re-send it to the same page multible times before pressing the [DONE] button (not available in the above) for actual processing...

basing the reply on this:
I want to retain the array values so that a new user can add new information to the array.

Using the codes now, everytime when I clicked on Again and return to main.html and enter new values, all the arrays will be overwritten.
If I'm way off, sorrry... Busy busy... ;)
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post by llanitedave »

Aaaah, the ooold hidden field trick! That would work.

Another word about sessions, since there really isn't any login here, a session wouldn't have much purpose -- and it will only last as long as the browser is open, is that right? If you close the browser and open it back up, you would need a new session and a new array.

(At least that's the impression I've gotten -- I haven't tried it from that perspective)

That's true for the hidden field as well, is it not? A text file is something that could be saved more permanently.
idq
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 2:29 pm

Post by idq »

Guys... dont think so complicated.

What my lecturer wants us to do is just to pass data between pages for that particular session.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

@llanitedave
Yah, as the data itself isn't in need to be hidden better, but also not really needed to be visible (a matter of choise) it works well as a temp storage imho. And yes, you need at least one browser window opened for the $_SESSION (or the hidden field approach) to work. If closing them is a must, there is also the use of $_COOKIE's, textfiles and/or databases.

@idq
Then your lecturer would love to see you reading this forum? ;)
Well, we (most of us) are doing this to learn others, if possible. I dont belive in the sure-here-you-go approach when it comes to dealing out code snippets. So what you have here above should be sufficient, if you combine that with some reading in the php manual.

Post bits of the code you've got sofar, and let us comment on that...
Post Reply