Jump to content

Recommended Posts

Hello phpfreaks  ;)

 

I'm a bit new to PHP, as well as this forum.

 

I'm learning in a fast way, but there's something I can't seem to figure out, nor find it on google.

I'm sure it's a simple and basic trick, but like I said, I'm just a beginner ;)

 

So the question is: How can I store multiple words to an array, using only 1 input form field?

 

I guess it's with a for-loop..?

 

So everytime you hit the 'submit' button, a new word should be added to the array. My problem is that I'm always overwriting ONE word..

 

This is what I've got so far.. :

 

<?php
if (isset($_POST['word'])){
	$word = $_POST['word'];
	$word = strip_tags($word);
	$word = trim($word);
	$word = htmlentities($word);
}
else {
	$word = "";
}				
?>

<div id="arrays">
<p>Add a word to the array below:<br /><br /></p>

<form action="index.php" method="post">
	<input id="word" name="word" type="text" value="<?php echo $word; ?>" />
	<br /><input class="btn" type="submit" value="Submit" />
</form>

<br />

<p>
	<?php 
		$array = array("apple", "banana", "lemon", $word);

		for ($i = 0; $i < count($array); $i++){
			echo $array[$i] . " ";
		}
	?>
</p>
</div>

 

 

Also: I'm using PHP 5, in combination with xHTML strict 1.0.

Every time you hit submit you make a new request. data does not persist over multiple requests.

 

A few ways you can do it. The easiest being to use the $_SESSION array, this does persist.

 

At the VERY top of your file place this....

 

<?php session_start(); ?>

 

Then....

 

<?php
// save the word from the form into a new spot within the session array
$_SESSION[] = $word;

// make the contents of the $_SESSION array into a string and echo it.
echo implode(" ", $_SESSION);
?>

The reason why it isn't working is because every time the script is ran, the array gets reset. In order to store something so you can come back to it and use it later, you must use some form of storage. This could be sessions, cookies, text files, databases etc..

 

<?php
session_start();

function sanitize($input) {
  if (!empty($input)) {
    $input = strip_tags($input);
    $input = htmlentities($input);
    
    return trim($input);
  }
  else {
    return FALSE;
  }
}

if (isset($_POST['submit'])) {
  $word = $_POST['word'];
  
  if (sanitize($word)) {
    $_SESSION['words'][] = $word;
  }
  else {
    echo 'Please enter a word';
  }
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
  <input type="text" name="word" />
  <input type="submit" name="submit" value="submit" />
</form>

 

Every time you enter a word and hit submit, the value will be appended to the $_SESSION['words'] array.

 

**EDIT** thorpe got there before me.

Thanks both you guys!

 

I can see that it now stores the words.

 

But now I can't figure out how to display them? :-[

 

I tried:

 

<?php
for ($i=0; $i < count($_SESSION); $i++){
  echo $_SESSION[$i];
}
?>

 

but that's not working..  :-[

If I do this, it only displays the last word I entered :

 

for($i=0; $i < count($_SESSION); $i++){
echo $_SESSION['words'][$i];
}

 

I used this with the code from Wolphie.

 

 

 

UPDATE: I figured it out..  It should be:

 

for($i=0; $i < count($_SESSION['words']); $i++){
echo $_SESSION['words'][$i];
}

 

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.