
|
View Full Version : Passing variables as variables
raulgonzalez 04-24-2006, 05:59 PM Hello,
How can I pass a variable or variables from an HTML form to a php script as variables?
Example.
<input type=text name=name value="$variable = value;">
So when I access the php script it can go like
<?
$name = $_POST['name'];
echo $name; //this will output "$variable = value;".
?>
But if I do
<?
$name = $_POST['name'];
echo $variable; //I predicted this would output "value" but it doesn't
?>
How can I accomplish that simple task?
In other words I want the script to look as if it was in this fashion.
<?
$variable = value;
echo $variable; //which would output "value"
?>
But I need to pass the value "$varaible = value;" through a form
PlanetWebHost 04-24-2006, 07:21 PM well, if I understand you, you have answered you own question.
HTML forms send variables to PHP as $_POST['variable']
unless you have register_globals turned on, in which case they are assigned as variables automatically.
I generally just use the $_POST['variables'] in the code, but if you want to save yourself a little typing, you can reassign them like you did above...
$var = $_POST['var']
raulgonzalez 04-24-2006, 07:34 PM Well its not working. See this is what I would have in a normal php page.
<?
$variable = value;
echo $variable; //which would output "value".
?>
instead I want this
<?
$name = $_POST['name']; //the value of this would be "$variable = value"
//which IF sent to the browser would output that
//but I don't want to output to the browser I want to
// keep it as variable
echo $variable; //which does not output "value".
?>
PlanetWebHost 04-24-2006, 07:38 PM $_POST is an array which holds all the values from your HTML form.
try
$variable = $_POST['name'];
raulgonzalez 04-24-2006, 08:22 PM well if I do that then
if I was to say
echo $variable; //that would output "$variable = value;"
I just want it to output "value".
PlanetWebHost 04-24-2006, 08:41 PM no it wouldn't,
$variable = $_POST['name']; // Assigns the from data to $variable
echo $variable; // echos the value of $variable.
you don't have to do that much though, this would work just as well...
echo $_POST['name'];
raulgonzalez 04-24-2006, 09:08 PM Ok this works for me
<?
//////////$all_records = $_POST['all_records'];
$all_records = $vNAME[0] = "Raul Gonzalez";;
echo $vNAME[0]; //This outputs "Raul Gonzalez"
?>
As you can see by putting the $vNAME[0]; manually it does what I want, but I want "$vNAME[0];" to come via a form and to serve the same purpose.
in essence this is what I want
<?
$all_records = $_POST['all_records'];
////////$all_records = $vNAME[0] = "Raul Gonzalez";;
echo $vNAME[0]; //I cant get this to outputs "Raul Gonzalez"
?>
PlanetWebHost 04-24-2006, 09:20 PM that first one only works because you are assigning it directly in you code.
$all_records = $vNAME[0] = "Raul Gonzalez";;
makes $all_records and $vNAME[0] both hold the value 'Rual gonzaliz"
You are not getting any post data that way,
PHP assigns everything it recieves from an HTML form to the array $_POST
you shold go read up on arrays http://us3.php.net/manual/en/language.types.array.php
Anyway, if your HTML input field is named 'name'
as in.. <input type="text" name="name" />
then $_POST['name'] is going to hold that value that is sent from your form,
so all you have do is
<?
echo $_POST['name'];
?>
raulgonzalez 04-24-2006, 09:49 PM well this is what I have on the first form
<textarea name="all_records" cols=60 rows=10>
$vNAME[0] = "AGUILAR, SAN JUANITA";
$vNAME[1] = "AYALA, ALBERTO";
$vNAME[2] = "BRIONES, JESUS";
$vNAME[3] = "CANTU, BRENDA";
</textarea>
and on the second one
<?
$all_records = $_POST['all_records'];
$i = 0;
while ($i < 4){
echo $vNAME[$i];
$i++;
}
?>
Instead of
<?
$vNAME[0] = "AGUILAR, SAN JUANITA";
$vNAME[1] = "AYALA, ALBERTO";
$vNAME[2] = "BRIONES, JESUS";
$vNAME[3] = "CANTU, BRENDA";
$i = 0;
while ($i < 4){
echo $vNAME[$i] . "<br>";
$i++;
}
?>
How can I accomplish that?
PlanetWebHost 04-24-2006, 10:00 PM well.. if your using a <textarea> like that with all the list of names seperated by line feeds, then you are going to get them all in one $_POST var. You will have to split up the lines using the explode function.
$names = explode("\n", $_POST['all_records'});
foreach ( $names as $name ){
echo $name."<br />";
}
( a foreach loops is better than a while loop in this situation )
Burhan 04-25-2006, 09:02 AM Actually, I know the answer to your question -- but what you are doing is basically opening up your script to all kinds of attacks from anyone.
Why do you need to pass PHP code through a form and then want to execute it? This is disaster.
To do what you want DO NOT DO THIS ON A LIVE SERVER:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$code = $_POST['foo'];
eval("$code;");
echo $z;
}
?>
<form method="POST">
<input type="text" name="foo" value='$z = 12' />
<input type="submit" name="submit" value="submit" />
</form>
The above will print 12 when you hit submit. Why this is a bad idea? Because if this is hosted on your site, I can write a script on my server, which posts to your site, and can execute any PHP code I want on your server, displaying all kinds of files.
Just to show you how dangerous this is:
<input type="text" name="foo" value='$z = 12; echo highlight_file(basename($_SERVER[PHP_SELF]));' />
Change the file to that, and run it.
raulgonzalez 04-25-2006, 07:39 PM Ok fyrestrtr so it can be done, but its not a good idea.
Thanks, I will search for a better way to accomplish what I want to accomplish.
An instructor of mine told me that I could use a query within a query. I've never done such thing, but I will research. Thanks
Ok ok.
I have a database that logs peaple in and out at a Learning Center.
Among other fields on the database there is a "time in" , "time out" field.
when I display the records in the browser, I get all the records for one student with their elapse times. example
TABLE
________
[RECORD 1]
NAME | TIME IN | TIME OUT | ELAPSE
_____ ________ __________ _______
name 1 | 12:00 AM | 1:00 PM | 1Hour
name 1 | 11:00 AM | 1:00 PM | 2Hour
name 1 | 12:00 AM | 3:00 PM | 3Hour
Total = 6 Hours
___________________________________________________
[RECORD 2]
_____ ________ __________ _______
NAME | TIME IN | TIME OUT | ELAPSE
_____ ________ __________ _______
name 2 | 12:00 AM | 1:00 PM | 1Hour
name 2 | 11:00 AM | 1:00 PM | 2Hour
name 2 | 12:00 AM | 3:00 PM | 3Hour
Total = 6 Hours
______________________________________________
I WANT TO ACCOMPLISH
NAME | TIMES | ELAPSE
____ ______ ________
name 1 | 3 | 6 Hours
name 2 | 3 | 6 Hours
I have accopmlished it by making an array of the names, I have to do this to be able to calculate the 6 hours without showing the 3 times the student was there, which the instructor doesn't care. But I have to generate the array first, then copy and paste it on the querey, which I wanted to avoid by just generating it and then submiting it through the form, thus eliminating the copy and paste.
Burhan 04-26-2006, 03:05 AM You can just modify the query to sum up the difference in hours for each student.
Suppose your table is like this (this example is in MySQL):
CREATE TABLE `student_log` (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`stu_name` VARCHAR(255), `time_in` TIME, `time_out` TIME);
Adding some sample records into the database:
SELECT `stu_name`,`time_in`,`time_out` FROM `student_log`;
+----------+----------+----------+
| stu_name | time_in | time_out |
+----------+----------+----------+
| name1 | 09:53:29 | 11:53:29 |
| name1 | 12:53:29 | 14:53:29 |
| name2 | 12:53:30 | 16:53:30 |
| name2 | 14:53:30 | 17:03:00 |
+----------+----------+----------+
4 rows in set (0.00 sec)
Finally, to calculate how much time each student spent:
mysql> SELECT SUM(TIMEDIFF(time_out,time_in)) as `hours` FROM student_log WHERE stu_name = 'name2';
+-------+
| hours |
+-------+
| 6 |
+-------+
1 row in set (0.00 sec)
Or you can do this :
mysql> SELECT stu_name, SUM(TIMEDIFF(time_out,time_in)) as `hours` FROM student_log GROUP BY stu_name;
+----------+-------+
| stu_name | hours |
+----------+-------+
| name1 | 4 |
| name2 | 6 |
+----------+-------+
2 rows in set (0.00 sec)
El-Vino 04-26-2006, 05:07 AM Hello,
How can I pass a variable or variables from an HTML form to a php script as variables?
Example.
<input type=text name=name value="$variable = value;">
So when I access the php script it can go like
<?
$name = $_POST['name'];
echo $name; //this will output "$variable = value;".
?>
But if I do
<?
$name = $_POST['name'];
echo $variable; //I predicted this would output "value" but it doesn't
?>
How can I accomplish that simple task?
In other words I want the script to look as if it was in this fashion.
<?
$variable = value;
echo $variable; //which would output "value"
?>
But I need to pass the value "$varaible = value;" through a form
Hmm, I must be missing something here... you want to pass a variable value from a HTML page to a php script, using POST, but without any output to the user interface.... <input type="hidden" name="name" value="value" /> does the trick, doesn't it ? just don't make it a type="text"
Cannot be that simple, can it ?
raulgonzalez 04-26-2006, 07:27 AM fyrestrtr that's deffenetly a good ideas. Is "SUM(TIMEDIFF(time_out,time_in)) as `hours`" a MYSQL function? I am using MS ACCESSS.
Burhan 04-26-2006, 09:13 AM TIMEDIFF is MySQL specific, SUM() is standard SQL, iirc.
|