0

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.

  1. if I duplicate the FWRITE code for $name to the $city line the PHP breaks, why?
  2. I cannot get PHP to accept a semi-colon at the end of $name even when I try to escape the ; character
  3. I can't get the PHP to accept \n (line break) at the end of the string being written by FWRITE

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>
deceze
  • 510,633
  • 85
  • 743
  • 889
DMSJax
  • 1,709
  • 4
  • 22
  • 35

1 Answers1

0

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 $nameand $cityin '(single quotes)

michi
  • 6,565
  • 4
  • 33
  • 56
  • Michi -- Thanks but broke parse of PHP. "Parse error: syntax error, unexpected T_VARIABLE in variables.php on line 3" – DMSJax Apr 28 '13 at 19:56
  • add the semicolon to each line, also you have to check if the varibles are string – tttony Apr 28 '13 at 19:59
  • add the double quotes: `\$name = \"$name\";\n` – tttony Apr 28 '13 at 20:01
  • Adding the semi-colon after the two strings did the trick, thanks guys! – DMSJax Apr 28 '13 at 20:06
  • @tttony Thank you also - you were right about string check, it was broken when numbers or ( were used in the data, but after adding the double quotes all is right. Thanks – DMSJax Apr 28 '13 at 21:16