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
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>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.