Note that there are some explanatory texts on larger screens.

plurals
  1. POBroken PHP script - fwrite and variables
    primarykey
    data
    text
    <p>Processing $_POST data through the following PHP script with 'mike' and 'tampa' being the post data:</p> <pre><code>&lt;?php $name = $_POST['name']; $city = $_POST['city']; $fh = fopen('variables.php', 'w') or die("can't open file"); fwrite($fh,"&lt;?php \n"); fwrite($fh,"\$name = $name"); fwrite($fh,$city); fwrite($fh,"\n ?&gt;"); fclose($fh); header("Location: main.php"); ?&gt; </code></pre> <p>results in the following being written to the variables.php file:</p> <pre><code>&lt;?php $name = miketampa ?&gt; </code></pre> <p>However, what I ultimately need it to do is output as follows:</p> <pre><code>&lt;?php $name = mike; $city = tampa; ?&gt; </code></pre> <p>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.</p> <ol> <li>if I duplicate the FWRITE code for $name to the $city line the PHP breaks, why?</li> <li>I cannot get PHP to accept a semi-colon at the end of $name even when I try to escape the ; character</li> <li>I can't get the PHP to accept \n (line break) at the end of the string being written by FWRITE</li> </ol> <p>I have included the code below just in case someone wants to see all of what I'm messing up. Thanks for any Help.**</p> <p>FORM.PHP</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="process.php" method="post"&gt; What is your name?&lt;input name="name" type="text" /&gt; what city do you live in?&lt;input name="city" type="text" /&gt; &lt;input type="submit" name="submit" id="submit" value="Submit" /&gt;&lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>PROCESS.PHP</p> <pre><code>&lt;?php $name = $_POST['name']; $city = $_POST['city']; $fh = fopen('variables.php', 'w') or die("can't open file"); fwrite($fh,"&lt;?php \n"); fwrite($fh,"\$name = $name"); fwrite($fh,"\$city = $city"); fwrite($fh,"\n ?&gt;"); fclose($fh); header("Location: main.php"); ?&gt; </code></pre> <p>VARIABLES.PHP - Is empty except what the process.php writes to it</p> <p>MAIN.PHP</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;/head&gt; &lt;?php include ('variables.php') ?&gt; &lt;body&gt; &lt;div&gt;Hello world, my name is &lt;?=$name?&gt; and I live in &lt;?=$city?&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload