faux Posted January 30, 2012 Share Posted January 30, 2012 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. Quote Link to comment Share on other sites More sharing options...
Proletarian Posted January 30, 2012 Share Posted January 30, 2012 What is your question? Quote Link to comment Share on other sites More sharing options...
faux Posted January 30, 2012 Author Share Posted January 30, 2012 I'm asking how can I have the HTML form add a line of text to an php array Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted January 30, 2012 Share Posted January 30, 2012 yes check into array_push function Quote Link to comment Share on other sites More sharing options...
faux Posted January 30, 2012 Author Share Posted January 30, 2012 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> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted January 30, 2012 Share Posted January 30, 2012 your logic is abit flawed printing $stack will only output ARRAY=> you could do this however $array=array(); $arrayed= array_push($array,$name); echo $arrayed; then you will get the desired HTML you want with the name. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted January 30, 2012 Share Posted January 30, 2012 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 Link to comment Share on other sites More sharing options...
faux Posted January 30, 2012 Author Share Posted January 30, 2012 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 Link to comment Share on other sites More sharing options...
Proletarian Posted January 31, 2012 Share Posted January 31, 2012 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. Quote Link to comment Share on other sites More sharing options...
faux Posted January 31, 2012 Author Share Posted January 31, 2012 Yeah, I tried added one, but same error. Quote Link to comment Share on other sites More sharing options...
silkfire Posted January 31, 2012 Share Posted January 31, 2012 You might need AJAX to show a random name in a box by letting PHP read the text file line by line into an array and then spitting out a random name. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.