$_POST variable name is variable, how to retrieve? - php

Users of my website can generate a custom form. All the fields are saved in a database with a unique ID. When someone visits the form, the fields 'name' attribute is field*ID*, for example
<p>Your favorite band? <input type="text" name="field28"></p>
<p>Your Favorite color? <input type="text" name="field30"></p>
After submitting the form, I use php to validate the form, but I don't know retrieve the value of $_POST[field28] (or whatever number the field has).
<?
while($field = $query_formfields->fetch(PDO::FETCH_ASSOC))
{
$id = $field[id];
//this doesn't work!!
$user_input = $_POST[field$id];
//validation comes here
}
?>
If anybody can help me out, it's really appreciated!

Add some quotes:
$user_input = $_POST["field$id"];

I'd suggest taking advantage of PHP's array syntax for forms:
<input type="text' name="field[28]" />
You can access this in php with $_GET['field'][28]

$user_input = $_POST['field'.$id];

Remember that you are using a string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id];.
Also, I would suggest calling them into an array to retrieve all data:
<?php
$user_inputs=array();
while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) {
$id=$field['id'];
$user_inputs[]=$_POST['field'.$id];
}
?>

Related

How can I add text to the end of user input?

I have been given some spaghetti code for a login page to fix.
We've got two input fields ID and Password.
Here's what I've been asked.
So in terms of a user inputting their ID, I want to add '#email.com' onto the end.
Im assume placeholder="#email.com" would work if i could align it to the right, but I also need to it to be added into the POST method. So if the user entered 'ID123' it would post 'ID123#email.com'
Here is the form:
<form action="command.php" formmethod="post">
<div class="ID">User ID:<br><input name="ID" type="text"><br></div>
<div class="Pass"> Password:<br><input name="pwd" type="password"></div>
<input id="submit" type="submit" value="Sign In">
Can anyone help? Is it even possible?
Placeholder in the input type in HTML will just give a hint in a textbox and it will not be added into the input of the user. What you need to do is catch the input type in the command.php you created and add it.
for example, in the command.php
$ID = $_POST['ID'].'#gmail.com'
When you submit the form, your POST data will contain:
$_POST['ID'] = 'test_id'; // Sample Data
$_POST['pwd'] = 'test_pwd'; // Sample Data
So, if you want to add #email.com at the end of the ID, you could simply concatenate the field:
$_POST['ID'] .= '#email.com';
Note: This is a simple solution and doesn't specify SQL injection/vulnerability prevention.
I think you want something like if anyone give ID123#email.com as input it will not change but if only give ID123 then it will be ID123#email.com,then :-
$userId=$_POST['ID'];
if(!filter_var($userId, FILTER_VALIDATE_EMAIL)) {
$userId=$userId.'#email.com';
}
echo $userId;

How can I show an old submission value in form field?

I created a form by RSForm. I have two textbox in it.
First textbox name is km1 (new_km) and second textbox name is km2 (old_km).
In the first time, user will fill km1 field (new_km) by her car kilometer number.
When the user recourse again and fill the form, km2 (old_km) must shown value that user entered it in previous recourse.
Please guide me?
Best regards.
Right, I know, my English is very bad. sorry.
Like this image: http://persianupload.com/kleeja/do.php?imgf=141839395854871.jpg
I personally use session for that...
Your html:
<?php
session_start();
if(!isset($_SESSION['something'])) $_SESSION['something'] = '';
?>
<input type="text" name="something" value="<?php $_SESSION['something']?>"/>
Your receiver php:
<php
session_start();
if($_POST['something']) $_SESSION['something'] = $_POST['something'];
?>

How $_POST variable/operation is managed in PHP?

When I do something like:
foreach($_POST as $post_key => $post_value){
/* Any code here*/
}
So, something like:
$varSomething = $_POST['anything'];
$varSomethingElse = $_POST['somethingElse'];
Is it possible? When I catch a $_POST[' '], isn't that variable already consumed?
The main reason why I would do this is because after a form submission, I want to check wether some items of some type got certain value or not.
Is there aything else more appropiate?
Firstly the html code don't use variable types, for example, if you have
<input id="check" type="checkbox" />
without a established value, after that you have echo $_POST['chek'], you could think that the result would be a boolean value (false or true), but the correct result will be "on" or "off", you can coding this case. Also, if you want to know the type of your data, you can use regular expression on server side, for example:
<input type="text" id="number" value="1350" />
.....
PHP code
$data = $_POST['number'];
$regularExpression = "/^\d{1,10}$/";
if (preg_match($regularExpression, $data)) {
echo "Is numeric";
}
Good lucky.
if you don't know what is the name of element which is sending the data. the first method is ohk . but if know the name like password or username you can use second one
in html
<input type="password" name ="password" />
in php
$pass_recvd=$_POST['password'];
there is no way to check the type i.e. text/password/checkbox/select etc. you have to do it on client side BEST WAY IS USING Jquery
if you wanna check whether a variable is set or not simple check by using isset method
if( isset($_POST['someVariableName'])) {}else{}

PHP avoiding a long POST

This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}

Passing variables from HTML to PHP using form submission

I'm having a problem with the following piece of code. My desire is to have the heading Search results appear when the form is submitted using the 'Search' button, and I'm trying to implement this using the hidden input called searching. The idea is that when the form is submitted, this value is set to 'yes', and that will reveal the heading, but that is not what is happening here. Can anyone please tell me where I've gone wrong?
<html>
<head>
<title>Search</title>
</head>
<body>
<h1>Search</h1>
<form name = "search" action = "<?=$PHP_SELF?>" method = "get">
Search for <input type = "text" name = "find" />
<input type = "hidden" name = "searching" value = "yes" />
<input type = "submit" name = "search" value = "Search" />
</form>
<?php
if ($searching == "yes")
{
echo "<h2>Search results</h2>";
}
?>
</body>
</html>
#chris, you dont have to use a hidden field. you just can check if the form was submitted like this:
if(isset($_GET['search'])) echo 'foo';
#Boris, why should it be more secure to store the global into another var? I would agree if you check the global against a regex or whatever before.
Felix
You need to access the superglobal $_GET:
if($_GET["searching"]=="yes"){
//echo here
}
Unless you're using an old version of PHP, or a really unsecure configure, you're likely not using global variables.
Therefore, you need to first retrieve your $searching variable from the magic $_GET variable.
$searching = $_GET['searching'];

Categories