homlyn
homlyn

Reputation: 253

storing multiple values in a single variable?

<form action="" method="post">
<input type="submit" name="foo" value="A" />
<input type="submit" name="foo" value="B" />
<input type="submit" name="foo" value="C" />
<input type="submit" name="foo" value="D" />
</form>

<?php
  $letters = $_POST['foo'];
?>

each time form submit $letters will be filled with respective values. so $letters can be "A" or "B" or "C" etc.

is it possible store all submitted values at a time? say when I echo $letters it should give "ABCD" or "BCDBA" or whatever order form is submitted.

I think this can be done through $_SESSION but no Idea how...any ideas?

Upvotes: 1

Views: 4900

Answers (3)

Harshvardhan Singh
Harshvardhan Singh

Reputation: 1

Try this

$email_to = "";
$get= mysql_query("SELECT * FROM `newsletter`");

while ($row = mysql_fetch_array($get)) {
    $email_to = $email_to.",".$row['mail'];
}

echo $email_to;

Upvotes: 0

CoursesWeb
CoursesWeb

Reputation: 4237

Hy Try make the attribute name in each submit button: name="foo[]" Then, in php, $_POST['foo'] contains an array with the value of each "foo[]"

echo implode('', $_POST['foo']);

Upvotes: 2

Andrew Jackman
Andrew Jackman

Reputation: 13976

You could use the session, so first: $_SESSION['letters']=array(); and then each post you can do $_SESSION['letters'][] = $_POST['foo']; and then you will have an ordered array.

This is how the top of your page will look:

session_start(); //important that this is first thing in the document
if ( !$_SESSION['letters'] ) {
    $_SESSION['letters'] = array(); //create the session variable if it doesn't exist
}
if ( $_POST['foo'] ) { //if foo has been posted back
    $_SESSION['letters'][] = $_POST['foo'];  // the [] after the session variable means add to the array
}

You can use print_r $_SESSION['letters']; to output the array as a string, or you can use any of the many php array functions to manipulate the data.

Upvotes: 0

Related Questions