<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?
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']);
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.
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;
Related
I pretty newbie to php and javascript but more i am curious about PHP. I want to add elements into a empty array every time i add a new element trough a input form, and after that i want those elements to be displayed in to the browser .The code i use is this
<form action="index.php" method="POST">
<input type="text" name="name" placeholder="name"><br><br>
<input type="submit" name="submit" value="enter"><br><br>
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION['names']=array();
$names=$_SESSION['names'];
$name=$_POST['name'];
array_push($names,$name);
for($i=0;$i<count($names);$i++){
echo $names[$i];
}
};
How could i achieve to display every element inside the array that i add trough the input field in php?
You overwrite the value every time because you wipe out the array on every page load: $_SESSION['names']=array();. Instead check to see if that session variable exists (and is an array) first and, if it doesn't, then create it. Otherwise just append to that array.
<form action="index.php" method="POST">
<input type="text" name="name" placeholder="name"><br><br>
<input type="submit" name="submit" value="enter"><br><br>
</form>
<?php
session_start();
if (isset($_POST['submit'])) {
if (!isset($_SESSION['names']) || !is_array($_SESSION['names'])) {
$_SESSION['names'] = array();
}
$name = $_POST['name'];
$_SESSION['names'][] = $name;
$num_names = count($_SESSION['names']);
for($i=0;$i<$num_names;$i++){
echo $_SESSION['names'][$i];
}
};
Sorry for what is probably quite an easy question, but I'm trying to pick up some info from my php form:
This is my current code which works for the post name, but what if I want to also grab the colours boxes that were selected too?
main.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
$colour_array = [
"red" => "#9E2A2B",
"blue" => "#3E5C76",
"green" => "#335C67",
];
?>
...
<form action="/" method="post">
<input type="text" name="name" placeholder="Acme Corp"/>
<input name="colour" type="checkbox" value="red">Red<br>
<input name="colour" type="checkbox" value="blue">Blue<br>
<input name="colour" type="checkbox" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
I guess I confused because currently it is calling this:
pngfile.php
<?php
require_once 'functions.php'; // Requires and includes do not need brackets.
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
?>
Which calls functions.php
<?php
function process($inputdata)
{
...
The issue is I want to be able to separate out the values from the form, so I get both the input box and the colour selection in separate variables.
EDIT: What I have tried:
main.php
$data = $_POST['name'] && $_GET['colour']
functions.php
process($inputdata, $colours)
But I'm not really sure where to go from there.
In HTML, change name="colour" to name="colour[]"
To access the value of colour in php do something like below
<?php
if(!empty($_POST['colour'])) {
foreach($_POST['colour'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
}
}
?>
Is there any solution for this? Sorry if I didn't explain this good.
When I put some value in $_POST['name'] and submit form I want to put that value in array( $arr ) and when I put some diferent value second time in $_POST['name'] and submit form I want that value put in array ( $arr ) to. Now that array should have two element ( first value and second value from $_POST['name'] )
<?php
session_start();
$_SESSION['name'] = 1;
$arr =array();
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$arr[] = $_POST['name'];
}
print_r($arr);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I started session, chacked if is form submitted and if is an empty field.
I tried with hidden input with value of $token = rand(); adn it not worked.
This should be easy, but I have some trouble do fix it.Tnx!
PHP is a stateless language and doesn't store variables from one page load to another. Each time you are submitting the form the $arr variable is destroyed from memory when the page has been processed.
If you want to keep information across page loads then you need to store it in the super global variable $_SESSION as a key value pair.
If you use the modified code below it will store the value typed into the name input when the form is posted in a two level multidimensional array that can be accessed using $_SESSION['name'] and a numeric index.
<?php
session_start();
if(isset($_SESSION['name']) && !is_array($_SESSION['name']))
{
$_SESSION['name'] = array();
}
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$_SESSION['name'][] = $_POST['name'];
}
print_r($_SESSION);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I'm making a todo list and I've got everything working except for this one thing. I need to loop over inputs that's been submitted via a form, these inputs have the same name so what I've done is storing them as an array. Now I need to loop over them so I can send them into the database one by one. Here's what I tried:
if (isset($_POST['submit'])) {
$labelValues = $_POST['labelValue[]'];
$i = 0;
while($i < sizeof($labelValues)) {
$stmt = $db->prepare("INSERT INTO tenta_table (text) VALUES (:text)");
$stmt->bindParam(':text', $labelValues[$i]);
$stmt->execute();
$i++;
}
}
HTML, the inputs are marked with red:
But it doesn't seem to work, it's not giving me any errors so I have nothing to go on. Where am I going wrong here?
Your $_POST['labelValue'] will already be an array if you have named your inputs correctly, something like <input type="text" name="labelValue[]" /> would create and array called labelValue in your POST.
From there you should be able to use your current code with one minor change
if (isset($_POST['submit'])) {
$labelValues = $_POST['labelValue'];
$i = 0;
while($i < sizeof($labelValues)) {
$stmt = $db->prepare("INSERT INTO tenta_table (text) VALUES (:text)");
$stmt->bindParam(':text', $labelValues[$i]);
$stmt->execute();
$i++;
}
}
Above I have change $labelValues to equal $_POST['lableValue'] rather than $_POST['labelValue[]']
In your case only the last input element will be available.
If you want multiple inputs with the same name use name="foo[]" for the input name attribute. $_POST will then contain an array for foo with all values from the input elements.
<form method="post">
<input name="a[]" value="foo"/>
<input name="a[]" value="bar"/>
<input name="a[]" value="baz"/>
<input type="submit" />
</form>
The reason why $_POST will only contain the last value if you don't use [] is because PHP will basically just explode and foreach over the raw query string to populate $_POST. When it encounters a name/value pair that already exists, it will overwrite the previous one.
However, you can still access the raw query string like this:
$rawQueryString = file_get_contents('php://input'))
Assuming you have a form like this:
<form method="post">
<input type="hidden" name="a" value="foo"/>
<input type="hidden" name="a" value="bar"/>
<input type="hidden" name="a" value="baz"/>
<input type="submit" />
</form>
the $rawQueryString will then contain a=foo&a=bar&a=baz.
You can then use your own logic to parse this into an array. A naive approach would be
$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
list($key, $value) = explode('=', $keyValuePair);
$post[$key][] = $value;
}
which would then give you an array of arrays for each name in the query string.
or the best and simple approach for this
<form method="post">
<input name="a[0]" value="foo"/>
<input name="a[1]" value="bar"/>
<input name="a[2]" value="baz"/>
<input type="submit" />
</form>
You should replace
$labelValues = $_POST['labelValue[]'];
By
$labelValues = $_POST['labelValue'];
Not Sure, but as far as i remember it should be $labelValues = $_POST['labelValue']. I think your $labelValues is null and you don't even enter your loop. You should do a var_dump( $_POST ) to verify what you're working with.
I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal. I am displaying my check boxes in a for loop.
Here I am getting the all values in an array, but I want store the array elements based on the check box checked.
<?php
$j=0;
$arr = Array();
foreach($collection as $data) {
$mageid=$data['mageproductid'];
$products = Mage::getModel('catalog/product')->load($mageid);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
$checkeven=0;
$arr[$j]=$products->getId();
//echo $arr[$j];
$j++;
} ?>
My checkbox code:
<form id="check_all" action="" method="POST" name="check" >
<input type="checkbox" class="multid[]" id="<?php echo $products->getId();?>" value="checked" /> </form>
What do I have to do in order to get checked values in my array? Did I do anything wrong?
Use input name attribute
<input type="checkbox" name="multid[<?php echo $products->getId();?>]" value="checked" />
No in you php code, check if multid[yourProductId] is set, and store them if it is set.
<?php
$j=0;
$arr = Array();
foreach($collection as $data) {
$mageid=$data['mageproductid'];
$products = Mage::getModel('catalog/product')->load($mageid);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
$checkeven=0;
$arr[$j]=$products->getId();
if(!empty($_GET['multid['.$arr[$j].']']))
its checked, do something.
//echo $arr[$j];
$j++;
} ?>
After submiting the form you can get an array of checked products with $_POST['multid']
Can you use javascript? Try this one.
HTML:
<input type="hidden" id="hdnCheckedIDs" value="" />
Before submit, on submit button's client click, Javascript:
var CheckedIDs = "";
for each checkbox
if(document.getelementbyid('multid1').checked)
CheckedIDs = CheckedIDs + document.getelementbyid('multid1').id;
document.getelementbyid('checkboxID') = CheckedIDs;
In PHP, you can use this comma seperated string $_POST['hdnCheckedIDs'] to get the IDs of checked chekboxes.