Jump to content

Input and output array... i think


faux

Recommended Posts

I'm kind of a beginner, but I really need this script and I'm not sure how quite to do it as needed...

 

With some html forms I need it so you can type a name in then click submit, then (with php) it will save that text into a text file or something. Later, when I hit a different button it will randomly display one of those texts/names from before.

Link to comment
https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/
Share on other sites

I don't understand how that will work with my script. Something like...?

<?php
$name = $_REQUEST["name"];
$stack = array();
array_push($stack, $name);
print $stack;
?>
<form name="input" action="index.php" method="get">
Name: <input type="text" name="name" /><br />
<input type="submit" value="Submit" />
</form>

try
{
   $names = explode(',', file_get_contents('yourfile.txt'));

   if ( isset($_POST['name']) )
   {
      if ( !ctype_alpha($_POST['name']) )
         throw new Exception('Name can only consist of alphabetic characters');
      array_push($names, $_POST['name']);
      file_put_contents('yourfile.txt', implode(',', $names), FILE_APPEND);
   }
   if ( !count($names) )
      throw new Exception('No names to display');
   shuffle($names);
   echo $names[0];

catch ( Exception $e )
{
   $error = $e->getMessage();
}
if ( isset($error) )
   echo $error;

  Quote

try
{
   $names = explode(',', file_get_contents('yourfile.txt'));

   if ( isset($_POST['name']) )
   {
      if ( !ctype_alpha($_POST['name']) )
         throw new Exception('Name can only consist of alphabetic characters');
      array_push($names, $_POST['name']);
      file_put_contents('yourfile.txt', implode(',', $names), FILE_APPEND);
   }
   if ( !count($names) )
      throw new Exception('No names to display');
   shuffle($names);
   echo $names[0];

catch ( Exception $e )
{
   $error = $e->getMessage();
}
if ( isset($error) )
   echo $error;

 

I got one error so far with that.

Parse error: syntax error, unexpected T_CATCH in /var/www/index.php on line 18

  Quote

  Quote

try
{
   $names = explode(',', file_get_contents('yourfile.txt'));

   if ( isset($_POST['name']) )
   {
      if ( !ctype_alpha($_POST['name']) )
         throw new Exception('Name can only consist of alphabetic characters');
      array_push($names, $_POST['name']);
      file_put_contents('yourfile.txt', implode(',', $names), FILE_APPEND);
   }
   if ( !count($names) )
      throw new Exception('No names to display');
   shuffle($names);
   echo $names[0];

catch ( Exception $e )
{
   $error = $e->getMessage();
}
if ( isset($error) )
   echo $error;

 

I got one error so far with that.

Parse error: syntax error, unexpected T_CATCH in /var/www/index.php on line 18

 

He forgot a closing bracket for his try block.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.