PDA

Click to See Complete Forum and Search --> : [RESOLVED] getting data into array from form


Blue1974
Feb 12th, 2010, 10:27 AM
I'm learning how to take information from an html form and post the information into an aray. The ultimate goal is to write the data to a text file but I haven't been able to get the data from the form. I have a working example of an array in a php file to follow.


This is my working example:


<html>

<head>
<style type = "text/css">
h2 {color: orange; text-align: center;}
body {background-color: cyan;}
</style>

<title>Array</title>
</head>


<body>

<h2>Customer List</h2>
<p>
<?
$userList = array("John" => array(
"email" => "john@demo.com",
"website" => "www.john.com",
"age" => "22",
"password" => "pass")


);

//if you just want to produce info about John you do this:

foreach ($userList["John"] as $key=>$value) {
echo "<b>(John)</b>$key - $value<br/>";
}

?>
</body>
</html>


I used this code to try to get the information from my form. I haven't been able to get it working though. I hit submit from the form and get a white screen. Can anyone see what I've done wrong? I've coded out the part about writing to a text file for now, until I know I can get the data from the form into the array.


<html>
<head>
<title>Storing Data</title>
</head>
<body>
<h2>Array Storage</h2>
<?php

$user_list => array("user"=>array(
'fname'=> $_POST['fname'],
'email'=> $_POST['email'],
'website'=> $_POST['web'],
'age'=> $_POST['age'],
'password'=> $_POST['pass'])
);


//$handle = fopen("storage.txt", "a+");

//foreach($user_list["user"] as $key=>$value){
//echo fwrite($handle, "<b>user<b/>$key - $value<br/>";


foreach ($user_list["user"] as $key=>$value) {
echo "<b>user</b>$key - $value<br/>";
}
//fclose($handle);
?>

</body>
</html>

SambaNeko
Feb 12th, 2010, 10:51 AM
Please post your code for the form that is sending to this script. On this line:

$user_list => array("user"=>array(

...you should have a = not a => (for the first instance, anyway)

Blue1974
Feb 12th, 2010, 11:01 AM
SambaNeko, that's what the problem was. Thank you for pointing it out. I'll try to get it to post to the text file.

Blue1974
Feb 12th, 2010, 11:18 AM
The form must be ok, so I won't post that part. I tried to post to the text file and I'm not getting anything in there.

I checked and the write permissions are set to read and write for the file.

Does this look like it should write to the file?



<html>
<head>
<title>Storing Data</title>
</head>
<body>
<h2>Array Storage</h2>
<?php

$user_list = array("user"=>array(
'fname'=> $_POST['fname'],
'email'=> $_POST['email'],
'website'=> $_POST['web'],
'age'=> $_POST['age'],
'password'=> $_POST['pass'])
);

// To display data directly from the form.
//foreach ($user_list["user"] as $key=>$value) {
//echo "<b>user</b>$key - $value<br/>";
//}

$handle = fopen("storage.txt", "a+");

foreach($user_list["user"] as $key=>value){
echo fwrite($handle, "<b>user<b/>$key - $value<br/>");
}
echo "Data submitted";
fclose($handle);
?>

</body>
</html>

SambaNeko
Feb 12th, 2010, 11:46 AM
This line is missing a $:

foreach($user_list["user"] as $key=>$value){

Other than that, it should work correctly. You may need to ensure that the permissions on the directory containing storage.txt are set to read/write all (chmod 777).

Blue1974
Feb 12th, 2010, 11:53 AM
Ok, I'll give it a try.
Thank you for your help.

kows
Feb 12th, 2010, 07:48 PM
just as a side note, you shouldn't be trying to echo out fwrite().

Blue1974
Feb 14th, 2010, 10:03 AM
I think I kind of got it. Thanks for the tip kows and SambaNeko was right. It only wrote to the file with the 777 permission. I don't really understand, if I'm the owner of the file on the first 7 and I have rw permissions but it was also necessary give the group and others permission also? I don't need it explained but just point of confusion for me.

Is there anyway to write to the file making the key bold or an escape character to write it in bold?


$handle = fopen("storage.txt", "a+");

foreach($user_list["user"] as $key=>$value){
fwrite($handle, "$key - $value\n");
}
echo "Data submitted";

fclose($handle);

kows
Feb 14th, 2010, 12:31 PM
plain text files (TXT) are not rich and don't support anything that would give the look of "bold." HTML is plain text, styled richly with tags (for example, <strong> turns bold "on" in XHTML). RTF (rich text file) and DOC files are rich and do support something that would make text bold. however, these are binary files and cannot be displayed on the internet (which I would assume you would want). you'd need something to compile your text into them, though. there are many extensions/classes out there to do it for you, if you really wanted it.

Blue1974
Feb 24th, 2010, 03:04 PM
If your still there, does this look correct for taking the data from the form and creating a table from it. I've tried it different ways but it's not working. I tried to do it like the book. I've looked for the small things like missing parenthesis and misspellings but I'm not sure what I'm doing wrong? I took the two dimensional array and made it single, I think.




<html>
<head>
<title>Table</title>
</head>
<body>
<h2>Creating a table</h2>
<?php

function create_table($data){
echo "<table border=\"1\">";
reset(data); // Remember this is used to point to the beginning
$value = current($data);
while ($value) {
echo "<tr><td>".$value."</td></tr>\n";
$value = next($data);
}
echo "</table>";
}

$user_list = array('fname'=> $_POST['fname'],
'email'=> $_POST['email'],
'website'=> $_POST['web'],
'age'=> $_POST['age'],
'password'=> $_POST['pass']);

create_table($user_list);

echo "Data submitted";
?>

</body>
</html>

kows
Feb 24th, 2010, 03:22 PM
you really have to be more specific than "it's not working." if you are getting errors, then that's important. if you are not getting any output from the function, that would be important too. if the data is just in the wrong order, or it doesn't display all of the data you'd like, then that's also something you should mention.

one thing I will point out that would be a major problem if your script isn't running correctly at all, is this line:
reset(data);
in this case, you missed the scalar ("$"), and so PHP is trying to look for a constant named data, which probably doesn't exist. this may be completely irrelevant though because it just resets the array pointer (and it may not need to be reset). it could be giving you weird errors though?

but, I'll also make things a bit easier for you. use a foreach loop instead of manually moving the array pointer in a while loop (it's a lot of unnecessary work):
<?php
function createTable($array){
foreach($array as $value){
echo <<<TABLE
<tr><td>{$value}</td></tr>
<<<TABLE;
}
}
?>
if you want the key returned as well:
foreach($array as $key => $value){
echo $key . '=>' . $value;
}

Blue1974
Feb 24th, 2010, 04:52 PM
Sorry, I didn't explain it better. What I meant by not working, was I just get a white screen after submitting. I used the code given in the book. I thought I had all the bases covered but I still missed that scaler $ sign you pointed out. I put it in but still the same behavior. I'm not sure I understand the further changes you suggested but the code in the book should be good. I'd hope to get that working before trying something else. So, it looks like it should be functional, the way it is? I mean after adding the scaler $ I missed previously?

kows
Feb 24th, 2010, 05:08 PM
the loop I used is something you had used previously in the rest of the posts you've made in this thread.

have you looked at the source of the page after you open it, to see if the table is actually being created (whether it has any information or not)?

and, I hope you realize that this script will not do anything unless you're sending a post request from a form to it. that's what the $_POST superglobal is being used for.

Blue1974
Feb 24th, 2010, 07:30 PM
I'm not sure why they didn't use the same loop. I will use it though.

I did check the source code after the page is open and the table wasn't generated. This is all I'm getting:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8"></HEAD>
<BODY></BODY></HTML>


Does this mean the problem is back with the form?

The method is "post" in the form attribute.

I used the same code in another practice exercise and it was working:


<?php
function welcome_line(){
echo "Welcome to my website!";
}
welcome_line();
function create_table($data) {
echo "<table border=\"1\">";
reset($data); //Remember this is used to point to the beginning
$value = current($data);
while ($value) {
echo "<tr><td>".$value."</td></tr>\n";
$value = next($data);
}
echo "</table>";
}
//$my_array = array('Line One.', 'Line Two,', 'Line Three.');
//create_table($my_array);
?>

kows
Feb 24th, 2010, 07:57 PM
uh.. just post all of the code you're using, including the form you're using and the script you're submitting it to. I'm having too hard of a time trying to figure out what you're doing, so I'd rather just see if what you should be doing works for me.

and when you're posting PHP code, I'd suggest you use the and tags, not the HTML ones.

Blue1974
Feb 24th, 2010, 08:27 PM
ok...sorry for all the trouble.

This is the form:


<html>
<head>
<style type = "text/css">
h2, h3 {color: brown;}
body {background-color: cyan;}
#buttons {text-align:center;}
.reset {margin-left: 0 px;}
</style>


<title>Creating A Table</title>
</head>


<body>

<h2>Creating Table with PHP</h2>

<form name ="registration" action="store5.php" method = "post">

<table border = "0">
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" size="10" maxlength="10" /> </td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" size="25" maxlength="25" /> </td>
</tr>
<tr>
<td>Web Site:</td>
<td><input type="text" name="web" size="25" maxlength="25" /> </td>
</tr>
<tr>
<td>Age: </td>
<td><input type="text" name="age" size="3" maxlength="3" /> </td>
</tr>
<tr>
<td>Password: </td>
<td><input type="text" name="pass" size="15" maxlength="15" /> </td>
</tr>
<tr>
<td colspan="2" id="buttons"><input type="submit" name="submit" value="Submit Order"> <input type="reset" name="reset" class="reset" value="Reset"></td>
</tr>
</table>

</form>
</body>

</html>



and again this is the php file.


<html>
<head>
<title>Form Submitted</title>
</head>
<body>
<h1>Form Table Data</h1>
<?php

function create_table($data){
echo "<table border=\"1\";
reset($data); // Remember this is used to point to the beginning
$value = current($data);
while ($value) {
echo "<tr><td>".$value."</td></tr>\n";
$value = next($data);
}
echo "</table>";
}

$user_list = array('fname'=> $_POST['fname'],
'email'=> $_POST['email'],
'website'=> $_POST['web'],
'age'=> $_POST['age'],
'password'=> $_POST['pass']);

create_table($user_list);

echo "Data submitted";
?>

</body>
</html>

kows
Feb 24th, 2010, 09:51 PM
err. the code you posted earlier is different than the code you just gave me. specifically, you have this line in the code you just posted:
echo "<table border=\"1\";
and it needs to look like this:
echo "<table border=\"1\">";
this would cause a fatal error and would probably be the reason your script wasn't even running previously :/ but, it was only a syntax error in the code you posted just now and not the code posted earlier in this thread.

so, anyway, with the change I showed you above, the code executes as it should (as far as I know). however, one thing -- change this line:
echo "<tr><td>".$value."</td></tr>n";
to this:
echo "<tr><td>".$value."</td></tr>\n";
I only assume you missed the backslash ("\") to create a linebreak, but if not, just get rid of the "n" altogether.

hope that solves your problem.

Blue1974
Feb 25th, 2010, 08:22 AM
I think the reason the code was different was me just trying different things to get it working and it ended with me not getting things back to where they should have been. I know it makes it hard when you create further problems beyond what was originally wrong.

I made the corrections but still getting the white screen on submit. Sorry I made this so hard.




<html>
<head>
<title>Form Submitted</title>
</head>
<body>
<h1>Form Table Data</h1>
<?php

function create_table($data){
echo "<table border=\"1\">";
reset($data); // Remember this is used to point to the beginning
$value = current($data);
while ($value) {
echo "<tr><td>".$value."</td></tr>\n";
$value = next($data);
}
echo "</table>";
}

$user_list = array('fname'=> $_POST['fname'],
'email'=> $_POST['email'],
'website'=> $_POST['web'],
'age'=> $_POST['age'],
'password'=> $_POST['pass']);

create_table($user_list);

echo "Data submitted";
?>

</body>
</html>

kows
Feb 25th, 2010, 08:37 AM
the script you just posted is the exact same script that I have. if it is not working, I really don't know what to tell you. are you sure you're opening the right script up in the browser (store5.php)? do you have error_reporting on in your local PHP installation? you may want to try checking the error logs of Apache (found in the Apache/logs/error.log file) to see if PHP is just terminating outright, or something. if you take all of the code out of the script, and replace it with the following:
<?php print_r($_POST); ?>
and then try submitting the form, does anything happen, or is it still blank?

Blue1974
Feb 25th, 2010, 07:41 PM
Kows, It works!!!

I can't say for sure but I can't even connect to the server where I was uploading the files to. I was able to connect this morning but the program would blow up. I didn't change any of the code before trying on the other server. I don't really know why it didn't work this morning but it worked now.

I'm going to go in and change the loop like you suggested. Thanks for helping me walk through it. Hope I didn't try your patience too much.

Thank you!