I am working with an array in PHP. For each thing in the array, I want to create a form and button attached to it. In addition to that, an action when the button is pushed. I fear it may have something to do with timing or such.
Here is my code:
foreach($projects as $proj){
echo "<form action='post'><input type='button' name='forminput' Value ='Yup'></form>";
$name = $_POST['forminput'];
}
if($name){
echo "ye";
}
Thanks!
Something like this?
<?php
$arrayName = array('test1' => '1', 'test2' => '2', 'test3' => '3', 'test4' => '4');
foreach ($arrayName as $key => $value) {
$$key = $value;
echo '
<form action="" method="post" enctype="multipart/form-data">
<input type="submit" name="forminput" Value ="'.$$key.'">
</form>
';
}
foreach ($_POST as $key => $value) {
$key = strip_tags($key); // to prevent scripts being injected into the page
$value = strip_tags($value);
echo $key . ' ' . $value;
}
?>
Related
I have the ff code which stores values inputted in form's textfield to a session array which I named "numbers". I need to display the value of the array but everytime I try echo $value; I get an error Array to string conversion in
I used echo var_dump($value); and verified that all the inputted values are stored to the session array.
My goal is to store the user input to an array everytime the user hits the submit button.
How do I correct this?
<?php
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="index.php">
<label>Enter a number</label>
<input type="text" name="num" required />
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
if (isset($_POST["num"]) && !empty($_POST["num"])){
$_SESSION['numbers'][] = $_POST["num"];
foreach($_SESSION as $key => $value){
echo ($value);
}
}
?>
Thank you.
When doing $_SESSION['numbers'][] = $_POST["num"];, you are making $_SESSION['numbers'] an array: so you'll either need to do that differently, or check whether $value within your foreach loop is an array or not.
if (isset($_POST["num"]) && !empty($_POST["num"])){
$_SESSION['numbers'][] = $_POST["num"];
foreach($_SESSION as $key => $value){
if (is_array($value)) {
foreach ($value as $valueNested) {
echo ($valueNested);
}
} else {
echo ($value);
}
}
}
OR
if (isset($_POST["num"]) && !empty($_POST["num"])){
$_SESSION['numbers'] = $_POST["num"];
foreach($_SESSION as $key => $value){
echo ($value);
}
}
The latter is probably what you are actually trying to accomplish.
If you want to echo all entered numbers, your for each cycle should be:
foreach($_SESSION[‘numbers’] as $key => $value) {
echo $value;
}
This is because the $_SESSION[‘numbers’] itself is the array that contains the numbers.
The error is because SESSION[“numbers”] is an array and you can just echo an array. It will throw an error “Array to string converstion”.
Iterate through the array and print it instead.
Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}
In my view i have
forach($array as $arr)
{
$data = array('fname' => $arr['first_name'],lname => $arr['lname']);
<input type="hidden" value="<?php print_r($data);?>" name="fnameData[]">
}
Now i am submitting form to controller and print print_r($this->input->post(fnameData)) it prints following array
Array(
[0] => Array([fname] => abc lname => aaa)
[1] => Array([fname] => xyz lname => bbb)
)
Now I want to print fname and lname both using foreach loop in controller
It gives me Illegal string offset 'fname'
Simple
foreach($your_array as $arr)
{
echo $arr['fname'];
}
UPDATE 2 :
<?php
forach($array as $arr)
{
$data = $arr['first_name'];
?>
<input type="hidden" value="<?php echo $data;?>" name="fnameData[]">
<?php
}
?>
foreach($array as $value){
echo $value["fname"];
}
<?php
foreach($array as $arr)
{
$data = $arr['first_name'];
?>
<input type="hidden" value="<?php echo $data;?>" name="first_name[]">
<?php
}
?>
I am in need of a PHP-program which will take all values from a HTML-link (with GET), regardless of how much one changes things in the address-bar, and then prints it out on the page. The code I have now is this:
HTML:
Link
PHP:
<?php
Echo "Value1: "; Echo $_GET["a"]; Echo "\n";
Echo "Value2: "; Echo $_GET["b"]; Echo "\n";
Echo "Value3: "; Echo $_GET ["c"];
?>
It works as intended for just these values, but it cannot cope with, for example, another variable being added in the address bar. I need some kind of PHP-function which can look for all kinds of variables through the GET-function.
Any help is appreciated!
Use a loop (modify as needed):
foreach ($_GET as $num => $value)
{
echo 'Value ' . $num . ': ' . htmlentities($value). "<br>" . PHP_EOL;
}
References:
Control structures
htmlentities()
Something like this:
<?php
foreach ($_GET as $key => $value) {
echo htmlspecialchars($key) . " - " . htmlspecialchars($value) . "<br>";
}
?>
<?php
print_r($_GET);
?>
Use a foreach loop
foreach ($_GET as $key => $val){
echo htmlentities($key).": ".htmlentities($val)."<br />\n";
}
or simply -
echo "<pre>".print_r($_GET,1)."</pre>";
To get all values and their corresponding names:
foreach($_GET as $key => $value)
{
echo $key . ' - ' . $value;
}
$my_get = array(); // store $_GET vars
if ('GET' == $_SERVER['REQUEST_METHOD']) // check that request method is "get"
foreach ($_GET as $key => $val)
{
$my_get[$key] = $val; // store
}
output $my_get:
array (
'a' => 'value1',
'b' => 'value2',
'c' => 'value3',
)
Since $_GET is nothing but an array, you can use for each loop to get the elements of it.
you get all $_GET values using print_r($GET) ...
otherwise you could to something like this
foreach(array_keys($_GET) as $key) {
echo htmlspecialchars($_GET[$key]);
}
Later Edit : took into account the security issue exposed in the comment
using this method is good because you know what kind of variable you have then you could do something cool like :
if ($key == 'a') do this
if ($key == 'b' && $_GET[$key] == 'bar') do that
HTML example:
<form method="post" id="form" action="form_action.php">
<input name="email" type="text" />
</form>
User fills input field with: dfg#dfg.com
echo $_POST['email']; //output: dfg#dfg.com
The name and value of each input within the form is send to the server.
Is there a way to get the name property?
So something like..
echo $_POST['email'].name; //output: email
EDIT:
To clarify some confusion about my question;
The idea was to validate each input dynamically using a switch. I use a separate validation class to keep everything clean. This is a short example of my end result:
//Main php page
if (is_validForm()) {
//All input is valid, do something..
}
//Separate validation class
function is_validForm () {
foreach ($_POST as $name => $value) {
if (!is_validInput($name, $value)) return false;
}
return true;
}
function is_validInput($name, $value) {
if (!$this->is_input($value)) return false;
switch($name) {
case email: return $this->is_email($value);
break;
case password: return $this->is_password($value);
break;
//and all other inputs
}
}
Thanks to raina77ow and everyone else!
You can process $_POST in foreach loop to get both names and their values, like this:
foreach ($_POST as $name => $value) {
echo $name; // email, for example
echo $value; // the same as echo $_POST['email'], in this case
}
But you're not able to fetch the name of property from $_POST['email'] value - it's a simple string, and it does not store its "origin".
foreach($_POST as $key => $value)
{
echo 'Key is: '.$key;
echo 'Value is: '.$value;
}
If you wanted to do it dynamically though, you could do it like this:
foreach ($_POST as $key => $value)
{
echo 'Name: ', $key, "\nValue: ", $value, "\n";
}
Loop your object/array with foreach:
foreach($_POST as $key => $items) {
echo $key . "<br />";
}
Or you can use var_dump or print_r to debug large variables like arrays or objects:
echo '<pre>' . print_r($_POST, true) . '</pre>';
Or
echo '<pre>';
var_dump($_POST);
echo '</pre>';
Actually I found something that might work for you, have a look at this-
http://php.net/manual/en/function.key.php
This page says that the code below,
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
would give you the output,
fruit1
fruit4
fruit5
As simple as that.
current(array_keys($_POST))
should give you what you are looking for. Haven't tested though.
Nice neat way to see the form names that are, or will be submitted
<?php
$i=1;
foreach ($_POST as $name => $value) {
echo "</b><p>".$i." ".$name;
echo " = <b>".$value;
$i++;
}
?>
Just send your form to this script and it will show you what is being submitted.
You can use a foreach Loop to get all values that are set.
foreach ($_POST AS $k=>$v){
echo "$k is $v";
}
Your example
echo $_POST['email'].name; //output: email
wouldnt make sense, since you already know the name of the value you are accessing?