Not able to get $_POST assigned to a variable PHP - php

I assume I have something stupid happening due to my lack of experience.
On a form on members.php I have:
$column = 0;
echo "<Form Name =member Method =POST ACTION = individual.php>";
echo "<table>";
echo "<tbody>";
while($row = $rs->fetch_assoc()) {
if ($column == 0) {
echo "<tr>";
}
echo '<td><INPUT TYPE = Submit NAME="'. $row['num'].'" VALUE ="'. $row['name'].'" id=Submit></td>';
$column++;
if ($column >= 5) {
echo "</tr>";
$row++;
$column=0;
}
}
echo "</tbody>";
echo "</table>";
echo "</form>";
On individual.php I have
print_r($_POST);
With the result being Array ( [16] => LUKE ) which is what is expected.
but when I try to
$name=$_POST['name'];
$num=$_POST['num'];
echo "<br>".$name." ".$num."<br>";
I do not get any results.
I mainly want to get $row['num'] but also did ['name'] to make sure I didn't transpose what I was trying to achieve.
I basically want to take what was selected on previous form and insert into a
select * from table where number=$num;

The name of your input fields are not num and name, instead, they are the values of these two keys in the array $row. That is why you are unable to retrieve these values on the backend.
Change your code to:
<input type="hidden" name="name" value="<?php echo $row['name']; ?>;">
<input type="hidden" name="num" value="<?php echo $row['num']; ?>;">
<input type="submit" value="Submit!" name="submit_form">

For obtain POST value for 'name' and 'num', you have to write:
<input type="text" name="name" value="<?php echo $row['name']; ?>">
<input type="text" name="num" value="<?php echo $row['num']; ?>">
type can be 'text' or 'hidden'
Enjoy your code!

If you want your form to return values that you can access using $_POST['name'] and $_POST['num'], you need to set name and num as input names on your form, and the appropriate values as the input values. If you don't want to add them as visible/editable values in your form, the usual way to do it is to use inputs of type hidden:
<form action="individual.php">
<input type="hidden" name="num" value="16" />
<input type="hidden" name="name" value="LUKE" />
<input type="submit" />
</form>
As it is, $_POST does contain the values that you're want, but they're not in an accessible format, because you've set 16 as an input name, so it turns into a key in the $_POST associative array. The form above will set num and name as keys in $_POST, and 16 and LUKE respectively as the values.

Related

How can i get multiple value from POST variable using same name

When user inputs text in 'ctext' field and press accept, I want to fill the value=" " field with user input, i achieved this but it fills all the value fields of same name in the page, how can i achieve it for different value of different ctext input? Anyone please give me solution with example, Many thanks
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
$i=0;
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here" value="<?php $ctext = false; if(isset($_POST['ctext'])){ $ctext = $_POST['ctext']; } echo $ctext[$i]; ?>"></input>
<input type="hidden" name="id" value="<?php $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
$i++;
endwhile;
endif;
endif;
?>
I hope I understand what you want. You want to access the ctext for each individual $pen when printing the corresponding form.
You just need to name your <input> with a unique name and then access that value when printing. A possible solution is this:
<input type="text" name="ctext[<?php echo $pen['id']; ?>]" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } echo $ctext; ?>"></input>
What does it do?
name="ctext[<?php echo $pen['id']; ?>]" ensures a unique name for each $pen. For a $pen with id 1 this will result in name="ctext[1]".
if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } uses $pen['id'] to look up the corresponding value in $_POST['ctext'].
By the way, when outputting user input you should always escape it, e.g. with htmlspecialchars. This will look like this: echo htmlspecialchars($ctext); That way malicious input like "><script>alert('Hello!')</script> won't execute the javascript.
Update: as requested a solution using session to store data:
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
session_start();
if (isset($_POST['ctext'])) {
$_SESSION['ctext'][$_POST['id']] = $_POST['ctext'];
}
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_SESSION['ctext'][$pen['id']])){ $ctext = $_SESSION['ctext'][$pen['id']]; } echo htmlspecialchars($ctext); ?>"></input>
<input type="hidden" name="id" value="<?php echo $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
endwhile;
endif;
endif;
Note: I removed the now unnecessary counter $i. The session handling is mainly done before the while loop (start a session and store POST data). During output the values from the session are used. The name of the input is not an array anymore.
Change name of an input to an array.like this . When you submit the form you will get these values as an array. Give it a try
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here"></input>
I guess your code is misleading you, your form is in while loop so once any of the ctext input is filled your variable $_POST['ctext'] is set on server side and according to your code it sets all the value of ctext once accept is pressed.
You can have different names as a solution or an array indexing in input field name=“ctext[]” to avoid this.

PHP - get uncertain number of fields

I have a problem with <form> in php .
i have uncertain number of fields (inputs) . it may be 1 or 100 input field .
Is there any function or class to get uncertain number of fields ?
Try that :
echo '<form>';
foreach ($data as $value) {
echo '<input name="field[]" value="',$value,'">';
}
echo '<input type="submit"></form>';
If you send that form, $_POST['field'] will be an indexed array in which every entry will correspond to one of the inputs.
$_POST would contain all the fields from the form on submit
print_r($_POST) will display them in an array for you
You can use an array name for the input fields in your form. For example, you can make an array of title fields by adding [] after the name:
<input type="text" name="title[]" />
<input type="text" name="title[]" />
<input type="text" name="title[]" />
Now in your PHP code, this value will be an array containing an amount of values equal to the number of fields with this name. The following code would print all titles on separate lines:
foreach ($_REQUEST['title'] as $value)
echo $value . "\n";
Just count the $_POST or $_GET what you use.
<?php
if(isset($_POST['submit'])){
echo "The total number of input fields is";
echo count($_POST); // include submit also
}
?>

Coudn't get textbox value

Here, I want to get value of textbox, but I coudn't.
Where am I wrong?
Is it must to use session if i want to get value of textbox?
<input type="submit" id="processorder" name="processorder" class="submit-green" value="Process Order"/>
<?php
foreach ($order_list as $row)
{
?>
<td class="align-center">
<input type="text" name="text[]" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
</td>
</tr>
<?php
$i++;
}
?>
if(isset($_POST['processorder']))
{
$txtvalue = $_GET['text[]'];
echo "helo".print_r($txtvalue);
}
this is wrong,
$txtvalue = $_GET['text[]'];
it should be
$txtvalue = $_GET['text'];
^ no [] here
Replace $_GET['text[]'] to $_GET['text']
The field would not be called text[] to PHP. It would just be called text, but would be an array instead of a string.
Try this:
if(isset($_POST['processorder']))
{
$arrayvalue = $_GET['text'];
foreach($arrayvalue as $txtvalue){
echo "helo" . $txtvalue . "<br>";
}
}
As your text input name is text[] with a bracket, it indicates that it is sending as an array, so to get the value you need
if (!empty($_POST["test"][0]) ) {
$test_value = $_POST["test"][0];
}
I am using 0 here since your code only shows one input text, for the server-end will only receive one array value
why you use array name="text[]" if you don't want to get multiple data using "text" than make it name="text"
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'] // get value
or
you want to multiple data using same name "Array"(mean using array)
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'][0] // 0 is a index value

Putting fetched array into <a href> value in PHP

I have a SQL query statement that will return a particular set of result. Such as ID, Names, Price. I have no problem with that.
However i am trying to add a link within the echo loop and set ID as the value so that i can post it to another page. Would that be possible ?
while ($row = mysql_fetch_array($result)) {
echo
"".$row{'Url'}."<br>";
echo
"Name:".$row{'Name'}."<br>";
echo
"Price: $ ".$row{'Price'}."<br>";
echo
'<div class = "qwerty" data-countdown= '.$row{'Time'}.'></div>';
echo
"Location:".$row{'Location'}."<br>";
echo
"Description:".$row{'Description'}."<br>";
echo ''.$row{'ID'}.'';
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value=".$row{'ID'}." />
</form>';
You missed quotes. value="'.$row['ID'].'"
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value="'.$row['ID'].'" />
</form>';
And php array use [ ]
http://ua2.php.net/manual/en/language.types.array.php

How to know the number of input tags in a submitted form?

I've coding for adding input elements in a form when a button outside the form is clicked.
<?php
$i=0;
$maxid = isset($_POST['max_id'])?$_POST['max_id']+1:0;
print '<form action="search.php" method="post" ><input type="hidden" name="max_id" value="' . $maxid . '" /><input name="ad_field_button" type="submit" value="Add Field" /></form>';
print '<form action="results1.php" method="post" >';
print '<table border="0">';
for($i=0;$i<=$maxid;$i++)
{
// code for adding input elements;
}
print '</table>';
print '<input name="ad_s_button" type="submit" value="Search" />';
print '</form></p>';
?>
I want to know the number of input element in the submitted form when button ad_s_button is clicked. Or how can I pass the 'max_id' value to the next page when ad_s_button is clicked. Any suggestions?
How about:
print '<input type="hidden" name="Inputelements" value="'.$maxid.'">';
so you have a hidden field with the value u want
Or how can I pass the 'max_id' value to the next page when ad_s_button
is clicked.
You could use a hidden field in your form and store the max_id there.
<input type="hidden" name="max_id" value="<?php echo $maxid; ?>">
$_POST contanis all the submitted elements.
In your case count($_POST)-1 will give you the count of the input elements. -1 becuase there is a button(submit buttom) will also be there in $_POST
You can also post array. For example:
<input type='text' name='request[name]' />
<input type='text' name='request[surname]' />
<input type='text' name='request[city]' />
And php part:
$request = $_POST['request'];
echo '<pre>'
echo var_dump($request);
echo '</pre>'
will produce
array(3){
'name' => 'abc',
'surname' => 'bcd',
'city' => 'Somewhere'
}
you can then process form data easily, and also count input fields easily, using count($request);

Categories