Processing $_POST data through the following PHP script with 'mike' and 'tampa' being the post data:
<?php
$name = $_POST['name'];
$city = $_POST['city'];
$fh = fopen('variables.php', 'w') or die("can't open file");
fwrite($fh,"<?php \n");
fwrite($fh,"\$name = $name");
fwrite($fh,$city);
fwrite($fh,"\n ?>");
fclose($fh);
header("Location: main.php");
?>
results in the following being written to the variables.php file:
<?php
$name = miketampa
?>
However, what I ultimately need it to do is output as follows:
<?php
$name = mike;
$city = tampa;
?>
I have spent several days trying to work out how to get this to work properly through trial and error but I just can't get it to work. So here are the 3 problems I have and please someone tell me (show me) the correct way to get this to work.
I have included the code below just in case someone wants to see all of what I'm messing up. Thanks for any Help.**
FORM.PHP
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form action="process.php" method="post">
What is your name?<input name="name" type="text" />
what city do you live in?<input name="city" type="text" />
<input type="submit" name="submit" id="submit" value="Submit" /></form>
</body>
</html>
PROCESS.PHP
<?php
$name = $_POST['name'];
$city = $_POST['city'];
$fh = fopen('variables.php', 'w') or die("can't open file");
fwrite($fh,"<?php \n");
fwrite($fh,"\$name = $name");
fwrite($fh,"\$city = $city");
fwrite($fh,"\n ?>");
fclose($fh);
header("Location: main.php");
?>
VARIABLES.PHP - Is empty except what the process.php writes to it
MAIN.PHP
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<?php include ('variables.php') ?>
<body>
<div>Hello world, my name is <?=$name?> and I live in <?=$city?></div>
</body>
</html>
See marked lines for changes:
fwrite($fh,"\$name = '$name';\n"); // <-- change
fwrite($fh,"\$city = '$city';"); // <-- change
This should do the trick!
see my edit: ;
after $name
and $city
!
second edit: enclose $name
and $city
in '
(single quotes)