Getting a specific array value from input form PHP - php

Okay, so I have a PHP array pulling from a mysql table. The array is generated based on the items in a table where items are frequently added and deleted. I have a button next to the item name, "Submit." I want the button to identify with the item that is in the same index. It will then pass the item submitted item to a new table.
<form class="omb_loginForm" action="inc/contribute_item.php" autocomplete="off" method="POST">
<?php
$item_array;
$index = 0;
$index_2 = 1;
$r = "r";
$b="b";
foreach ($item_array as $id_array){ ?>
<tr id="<?php echo $r.$index_2; ?>">
<td><?php echo $item_array[$index] ?></td>
<td> <?php echo $quantity_array[$index]; ?></td>
<td> <?php echo $price_array[$index];
$selectedItem = $item_array[$index]; ?>
<input type='hidden' name='hidden' value='<?php $selectedItem ?>'>
<input type='submit' name='submit' value"submit">
</form> </td>
<?php $index++;
$index_2++; ?>
</tr>
Here is the PHP:
if ($_POST['submit']) {
$connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_contrib = $_SESSION['first_name'];
$selected = $selectedItem;
$connect->query("UPDATE `items` SET `purchased_by` = '$user_contrib' WHERE `name` = '$selected'");
}

You're on the right track, just make sure that your opening and closing html tags are properly aligned.
Using one form per row
If you want to transmit the selected value via an hidden input, make sure, that each of these inputs is inside its own form together with the corresponding submit button:
<!-- row 1: -->
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="1"/>
<input type="submit" value="submit"/>
</form>
<!-- row 2: -->
<form action="inc/contribute_item.php">
<input type="hidden" name="myValue" value="2"/>
<input type="submit" value="submit"/>
</form>
Then in PHP access the selected value by using $_POST['myValue'].
Don't nest form tags. And don't put form tags between your table, tr, td tags. Close them in the same order you open them.
To be more specific, this is how your loop could look like:
<!-- don't start your form here -->
<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="<?= $index ?>"/>
<input type="submit" value="submit"/>
</form>
</td>
</tr>
<?php } ?>
</table>
Using radio buttons or check boxes
Yet another option would be to use <input type="radio" ... /> elements in each row. This way you could use just one global form:
<form action="inc/contribute_item.php" method="post">
<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<input type="radio" name="myValue" value="<?= $index ?>"/>
</td>
</tr>
<?php } ?>
</table>
<input type="submit" value="submit"/>
</form>

Related

Get id from a post php

So, i want to get id from $_POST["id"] when i push a button in a table
<form action="dosomething.php" method="post">
<td> <?php echo $row["id"]; ?> </td>
<td> <button>Do something</button> </td>
</form>
is that even possible?
thanks for any answers and help
Yes it is.
You will need to set a hidden input somewhere inside your form tags, like :
<form action="dosomething.php" method="post">
<input type="hidden" name="id" value="<?= $row["id"]; ?>" />
<td> <?php echo $row["id"]; ?> </td>
<td> <button onclick="this.form.submit();">Do something</button> </td>
</form>
This is not an elegant code and you should probably consider using javascript, especially if you have a lot rows and buttons.

Show text from MySql into input type text

I want to show the query result in the input type="text"but for some reason the text is blank. I only have one result row so maybe I don't need the while statement. But yet I can't make it to show in the input type text. Can you help me a see if this method is okay our not? Thanks in advance
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
while ($row = mysqli_fetch_array($result6)){
?>
<tr>
<td><input type="text" value="<?php echo $row['titulogrupo']; ?>" name="grupo1" id="velhas"></td>
</tr>
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
If you are certain you are only getting 1 result you can try this:
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
$row = mysqli_fetch_array($result6)
if $row['titulogrupo']!='' { ?>
<tr>
<td><input type="text" value="<?php echo $row['titulogrupo']; ?>" name="grupo1" id="velhas"></td>
</tr>
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>

Multiple Values (1 or 0) for checkbox array with same name

Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked.
I can't use " input type="hidden" name="chk_name[]" value="1" " because of the array. It doubles my array.
If I try to set it after using isset it doesn't put it into my array.
I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.
So how can I put a 1 into the array for wherever it isn't checked?
Form:
<?php
require_once ("con.php");
$p=payment();
$result = mysqli_query($mysqli,$p);
while($res=mysqli_fetch_array($result))
{
?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]"
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>
php:
$paid=$_POST['paid'];
$active=$_POST['active'];
foreach($_POST as $key=>$value)
{
$ID=$ID[$key];
$paid=$paid[$key];
$active=$active[$key];
$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up);
echo "Information stored successfully";
}
?>
my function:
function updatePayment($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
:Updated code:
I can see the arrays are coming out fine for everything now. The hidden method worked.
Thanks for everyone's help!
As soon the hidden field and the checkbox has the same name it will work no matter if the name is array or not.
The simple reason for this is that when is unchecked the checkbox value is not posted.
Try this code and you will see the result
<pre>
<?php
print_r($_POST);
?>
</pre>
<form method="POST">
<input type="hidden" name="fields[1]" value="1"/>
<input type="checkbox" name="fields[1]" value="0"/>
<input type="hidden" name="fields[2]" value="0"/>
<input type="checkbox" name="fields[2]" value="1"/>
<input type="hidden" name="fields[3][something][else]" value="3"/>
<input type="checkbox" name="fields[3][something][else]" value="3"/>
<button type="submit">Submit</button>
</form>

How to update the data in particular table row in HTML?

Since the <form> can not inside <tr>, I can not assign the form for each row. And at the end I have create a from like this
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
So, it seems when I submit data it submit every input, how can I send only a specific row? Thanks
Since forms send all the values, (since your using <button>), you could assign each row a key that corresponds to that row. Then use that in the textbox. Note: Textbox values must be an array structure, so that particular key from the submit update can be used (kinda like filtering). Consider this example:
<?php
if(isset($_POST['update'])) {
$product_key = $_POST['update'];
$product_name = $_POST['my_product_name'][$product_key];
echo $product_name;
// this should correspond to that same textbox row that you selected
}
?>
<form method="POST" action="">
<table>
<tr>
<td><input type="text" name="my_product_name[1]"></td>
<td><button name="update" type="submit" value="1">Update</button></td>
</tr>
<tr>
<td><input type="text" name="my_product_name[2]"></td>
<td><button name="update" type="submit" value="2">Update</button></td>
</tr>
</table>
</form>
This is not possible. A form sends everything in it. You have to use multiple forms in order to do that. Can you explain why you want to send each row separately. Let us know the complete problem so we can provide you much better solution :)
You must use separate forms. To avoid this issue, you could try using div's and style them with css.
<div class="big-wrapper">
<div class="form-wrapper">
<form id="first-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 1</button>
</form>
</div>
<div class="form-wrapper">
<form id="second-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 2</button>
</form>
</div>
Example of above code (with no additional css): http://postimg.org/image/p0olzl933/
One method would be to have form tags that wrap tables and each table effectively becomes a row.
Honestly, there are a lot of ways to do this... you could handle it using Javascript with something like I have below or generating a special table with a server-side script and writing that to the page. You could use ajax calls to send the values that change to a web service, etc.
Hope that thelps.
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
...
Collect the value then push it into the form on submit.
<script>
function dosubmit(e){
var value = $(this).parents('tr').find('input.some').val();
$(this).parents('tr').find('input.my_product_name').val(value);
}
</script>
<table>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
</table>
I don't know how much good a jsbin will be, but here it is
You can submit the value in form by setting in hidden parameter as follows:
HTML:
<form action="" id="formId">
<input type="hidden" name="product_name" id="hiddenValue" />
</form>
<table>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
</table>
Script:
<script>
function submitForm(id){
var input = id.parentElement.parentElement.getElementsByTagName("input")[0].value;
//Retrieving value of specific rows
document.getElementById("hiddenValue").value = input;
//Submits the form with given id.
document.getElementById("formId").submit();
}
</script>

Create a back button for form

I've created a 2 step form with the following code:
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
How can I add a back button on step 2? There will be several steps added not just 2 so the user needs to be able to move forward and back through the steps whilst keeping what they've filled in.
Try this
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="<?= $_REQUEST["title"]?>" placeholder="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
$field ="";
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; $field .= $name."=".$value."&";}
} ?>
<div><a href="<?php bloginfo('url'); ?>/contact-us/?<?= $field ?>" >Back</a></div>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
First add it on top of your page
// keep the data
$_SESSION['data'] = $_POST;
$_SESSION['step'] = $_REQUEST['step'];
Also replace the if statement to something like this:
if ($_REQUEST['step'] == 1) {
// continue
Write dynamic values on your form
<input type="text" name="title" id="title"
value="<?php echo $_SESSION['data']['title']; ?>" />
Use a link for Back and Next:
Back
Next
I guess it would works for you! :)
To navigate back and forwards you will want to pass the data from the previous form back to it again so you would need to store the value of each form somewhere. Your best bet would be to create a session and store each form info in a session variable. Then when you load each form you should check to see if the data is already stored in a session.
Then you could add a jquery button to change the value of the step and submit the form.
To do this you would need to the 'step' hidden input an id like 'step'
<input type="hidden" name="step" id="step" value="2" />
and then add a button like
<button class="back-button" type="submit" name="back" onclick="$('#step').val('1');">Back</button>
This would change the value of the step input and then submit the form taking you back to the previous form.
You would need to have another hidden input to tell the form where it came from so it would know whether to look at the session data (is you're going backwards) or the POST data (if youre going forwards).

Categories