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. Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/ Share on other sites More sharing options...
Proletarian Posted January 30, 2012 Share Posted January 30, 2012 What is your question? Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312488 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 Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312489 Share on other sites More sharing options...
darkfreaks Posted January 30, 2012 Share Posted January 30, 2012 yes check into array_push function Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312491 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> Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312496 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. Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312671 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; Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312776 Share on other sites More sharing options...
faux Posted January 30, 2012 Author Share Posted January 30, 2012 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 Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312785 Share on other sites More sharing options...
Proletarian Posted January 31, 2012 Share Posted January 31, 2012 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. Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312871 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. Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1312874 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. Link to comment https://forums.phpfreaks.com/topic/256026-input-and-output-array-i-think/#findComment-1313034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.