Page 1 of 1
Newbie... Retaining array information
Posted: Wed Mar 17, 2004 2:29 pm
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.
?>
Posted: Wed Mar 17, 2004 2:40 pm
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">";
?>
Posted: Wed Mar 17, 2004 3:24 pm
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.
Posted: Wed Mar 17, 2004 3:37 pm
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.
Posted: Wed Mar 17, 2004 3:48 pm
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.
Posted: Wed Mar 17, 2004 4:22 pm
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...
Posted: Wed Mar 17, 2004 5:33 pm
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.
Posted: Wed Mar 17, 2004 5:44 pm
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.
Posted: Wed Mar 17, 2004 5:48 pm
by idq
hmmm... but dull1554 mentioned that it is possible to use session. I'm confused over which implementation to use now.
Posted: Wed Mar 17, 2004 5:55 pm
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!
Posted: Wed Mar 17, 2004 6:10 pm
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.
Posted: Wed Mar 17, 2004 6:11 pm
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...

Posted: Wed Mar 17, 2004 6:25 pm
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.
Posted: Wed Mar 17, 2004 6:30 pm
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.
Posted: Wed Mar 17, 2004 6:50 pm
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...