I am trying to process multiple arrays from a form, but I constantly face the issue where only the last field is submitted.
The form is echoing results (IP addresses and their rDNS settings) from an simple xml output and publishing these in an editable form and table.
<?php include_once("API.php");
$ips=array('xx.xx.xx.xx','xx.xx.xx.xx','xx.xx.xx.xx');
foreach($ips as $ip){
$API->GetRdns($ip);
$xml = simplexml_load_string($API);
}
foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
When the submit button is clicked, the changes have to be submitted.
<button type="submit" name="submit">Submit</button>
<?php if (isset($_POST['submit'])){
foreach ($ips as $ip) {
$API->Sent($ip,$_POST[hostname],$_POST[extra])
}
?>
How do I get the values 'hostname' and 'extra' submitted for each of the IP's ($ip)?
You should try creating an array instead of a single value. Try this:
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['hostname']" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['extra']" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
You can catch this with:
<?php if (isset($_POST['submit'])){
foreach ($_POST['ip'] as $ip => $info) {
$API->Sent($value,$info['hostname'],$info['extra']);
}
?>
You should bind your submit button into a <form> to get the values
<form>
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
<button type="submit" name="submit">Opslaan</button>
</form>
another problem is you missed '' here:
$API->Sent($value,$_POST['hostname'],$_POST['extra'])
a form by default will only send one value for each "name" entity in the form. Can you try adding the brackets to the [name] property of the inputs so it sends the values as an array ?
I think i understand a little more now, you need the association between the IP and the hostname/extra values. How about if you add the ip as a hidden form field
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="hidden" name="ips[]" value="<?php echo $ip; ?>"></td>
<td><input type="text" name="hostname[]" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra[]" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
then you can process in the backend something like this:
<?php
$ips = $_POST['ips'];
$hostnames = $_POST['hostname'];
$extras = $_POST['extras'];
for($i=0;$i<count($ips);$i++)
{
echo "IP: " . $ips[i] . ", Hostname: " . $hostnames[i] . ", Extra: " . $extras[$i];
}
Related
It's a table, each row consists a checkbox, when it's checked, would like to get the respective td values and echo out. Here im using the if statement, but it doesn't seems to work.
And iam using php here, is using jquery a way out, can jquery work with php
code, so could i send those checked table row values back to serve? Any thoughts? Thank you.
<form>
<table>
<tr>
<?php
$specific = [];
while($row = mysqli_fetch_array( $result ,MYSQL_ASSOC)) {?>
<td><input type="checkbox" name="p[]" value="<?php echo $row['id']; ?>">
</td>
<td><input type="text" name="patientid[]"
style="border: none" value="<?php echo $row['patientid'] ?>"></td>
<td>
<textarea name="msg" style="border: none" class='msg'>
<?php echo $row['message'];} ?> </textarea>
</td>
<td><input class="phone" type="text" value="<?php echo
$row['telMobile'] ?>"></td>
check whether table row(s) are checked
<?php if(!isset($_GET['p'])){
$specific[] = [
"phone" => $row["telMobile"],
"message" =>$row["message"],
];}
}
$result = json_encode($specific,JSON_UNESCAPED_UNICODE);
echo $result;
echo "</tr>";
echo "</table>";
echo "</form>";?>
The desired result for $result is to show only the data of the table row(s) that are checked.
[{"phone":"123456","message":"test"},
{"phone":"789456","message":"testing"}]
Change your HTML code as below:
<td><input type="text" name="patientid[<?php echo $row['id']; ?>]"
style="border: none" value="<?php echo $row['patientid'] ?>"></td>
<td>
<textarea name="msg[<?php echo $row['id']; ?>]" style="border: none" class='msg'>
<?php echo $row['message'];} ?> </textarea>
</td>
<td><input name="phone[<?php echo $row['id']; ?>]" class="phone" type="text" value="<?php echo
$row['telMobile'] ?>"></td>
I have added <?php echo $row['id']; ?> in the input control name.
Change your PHP code as below:
foreach($_GET["p"] as $id) {
$specific[] = [
"phone" => $_GET["phone"][$id],
"message" => $_GET["msg"][$id],
];
}
I have an edit page where a user can edit an invoice form. I need to echo the array into a table but separate each array into a new line on the table.
When i echo the value, its bunched up into one line. When i echo the value into an input field, i only get one record from the array.
Here's what it looks like in my table:
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
$partnumber = $res['partnumber'];
$partdescription = $res['partdescription'];
$partprice = $res['partprice'];
$partquantity = $res['partquantity'];
}
>
And then the table:
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $partnumber;?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $partdescription;?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $partprice;?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $partquantity;?>></td>
</tr>
I get this:
<tr>
<td><?php echo $partnumber;?></td>
<td><?php echo $partdescription;?></td>
<td><?php echo $partprice;?></td>
<td><?php echo $partquantity;?></td>
</tr>
I get this:
I tried the following but they both result in a blank echo:
<tr>
<td><? echo $res['partnumber']; ?></td>
</tr><tr>
<td><? echo $res['partdescription']; ?></td>
</tr><tr>
<td><? echo $res['partprice']; ?></td>
</tr><tr>
<td><? echo $res['partquantity']; ?></td>
</tr>
And
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
?>
This is the GOAL:
Please help
EDIT***************
I will fix the security issue once i have the table working.
I tried this and the output is still on ONE line.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id='".mysql_real_escape_string($id)."' ");
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $res['partnumber'];?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $res['partdescription'];?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $res['partprice'];?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $res['partquantity'];?>></td>
<?php
}
?>
Does this help?
Please note, its only displaying 1 record because i have multiple values in one record and them i'm separating them with a comma. Please check screenshots above. Here's the code:
$partnumber = $_POST['partnumber'];
$partnumberarray = implode( ", ", $partnumber);
$partdescription = $_POST['partdescription'];
$partdescriptionarray = implode( ", ", $partdescription);
$partprice = $_POST['partprice'];
$partpricearray = implode( ", ", $partprice);
$partquantity = $_POST['partquantity'];
$partquantityarray = implode( ", ", $partquantity);
$result = mysqli_query($mysqli, "INSERT INTO invoicespartnumber, partdescription, partprice, partquantity, login_id) VALUES('$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')");
Is there anyway i can echo the values from the 1 record which is separated by a comma into multiple lines on the table?
You have to put your html inside while loop that you do over result array.
It will look like this.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE ID='".mysql_real_escape_string($id)."'");
while($res = mysqli_fetch_array($result)){ //close php here to easier write html. ?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value="<?php echo $res['partnumber'];?>"></td>
<td><input type="text" class="input-small" name="partdescription[]" value="<?php echo $res['partdescription'];?>"></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value="<?php echo $res['partprice'];?>"></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value="<?php echo $res['partquantity'];?>"></td>
</tr>
<?php //open php tag again to add end scope to while loop
}
?>
<table width = "100%">
<thead>
<tr>
// here will be all columns names in th
</tr>
</thead>
<tbody>
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $res['partnumber']; ?></td> <!-- Also use input box here -->
<td><?php echo $res['partdescription'];?><td>
<td><?php echo $res['partprice']; ?></td>
<td><?php echo $res['partquantity'];?></td>
</tr>
<?php
}
?>
I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.
I try form validation in codeigniter.
how Re-populate checkbox which value come from database if it fails in validation in an edit form in Codeigniter.OR
How
<tr>
<td>Title</td>
<td><input type="text" name="title" value="<?php if(!empty($mid)){echo $movie1['title'];}elseif(validation_errors()){echo set_value('title'); } ?>"></td>
</tr>
<tr>
<td>Genre</td>
<td>
<?php
if(!empty($mid))
{
$g=explode(",",$movie1['genre']);
}
foreach($genre as $getd)
{
echo"<input type='checkbox' name='genre[]'";
if(!empty($mid))
{
if(in_array($getd['name'],$g))
{
echo"checked='checked' ";
}
}
echo"value='".$getd['name']."'>".$getd['name'];
}
?>
</td>
</tr>
Re-populate dynamic generated checkbox if it fails in validation in an edit form in Codeigniter
Add this statement after value attribute show the correct code.
if(validation_errors()){echo set_checkbox('genre[]', $getd['name']);}
<tr>
<td>Title</td>
<td><input type="text" name="title" value="<?php if(!empty($mid)){echo $movie1['title'];}elseif(validation_errors()){echo set_value('title'); } ?>"></td>
<td><?php echo form_error('title'); ?></td>
</tr>
<tr>
<td>Genre</td>
<td>
<?php
if(!empty($mid))
{
$g=explode(",",$movie1['genre']);
}
foreach($genre as $getd)
{
// echo form_checkbox('genre[]',$getd['name'],set_checkbox('genre[]', $getd['name'])).$getd['name'];
echo"<input type='checkbox' name='genre[]'";
if(!empty($mid))
{
if(in_array($getd['name'],$g))
{
echo"checked='checked' ";
}
}
echo"value='".$getd['name']."'";
if(validation_errors()){echo set_checkbox('genre[]', $getd['name']);}
echo ">".$getd['name'];
}
?>
</td>
<td><?php echo form_error('genre[]'); ?></td>
</tr>
I am creating a page that would allow the user to select an existing address, or input a new one, here are my codes.
<table cellpadding="10px">
<tr>
<td><input type="radio" id="huhu" name="huhu" value="<?php echo $_SESSION['home_address']; ?>"></td><td><?php echo $_SESSION['home_address']; ?></td>
</tr>
<tr>
<td><input type="radio" id="huhu" name="huhu" value="New"></td><td><input type="text" placeholder="New Address" id="newAdd" name="newAdd" disabled></td>
</tr>
</table>
and here are my codes at the next page.
<?php
if(isset($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['newAdd'];
echo $_POST['newAdd']."<br>";
}
else{
$_SESSION['home_address'];
}
echo $_SESSION['home_address'];
?>
When i click on the existing address, it just deletes it. and does not store anything. but when i input a new on in the text area. it works.
I need to make it so that when the user clicks the address, the same address from the existing session displays.
please help. thank you.
I think you have missed session_start() method in your PHP file. Try to add the following code at the beginning of PHP file
if (!isset($_SESSION))session_start();
if your session info is correctly set.. this should work out.
<?php
session_start();
// for my testing....
$_SESSION['home_address'] = 'curr_session_address';
var_dump($_POST);
var_dump($_SESSION);
$s_addr = isset($_SESSION['home_address']) ? $_SESSION['home_address'] : '';
$p_addr = isset($_POST['newAdd']) ? $_POST['newAdd'] : '';
if ( !empty($p_addr) ) {
$_SESSION['home_address'] = $p_addr;
echo "new_address = $p_addr<br>";
}
else {
echo "session_address = $s_addr<br>";
}
?>
<form method='post' action='?'>
<table cellpadding="10px">
<tr>
<td><input type="radio" id="huhu" name="huhu" value="<?php echo $_SESSION['home_address']; ?>"></td>
<td><?php echo $_SESSION['home_address']; ?></td>
</tr>
<tr>
<td><input type="radio" id="huhu" name="huhu" value="New"></td>
<td><input type="text" placeholder="New Address" id="newAdd" name="newAdd"></td>
</tr>
</table>
<input type='submit' value='submit'>
</form>
Try this one.
if(empty($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['huhu'];
}
else if(!empty($_POST['newAdd'])){
$_SESSION['home_address'] = $_POST['newAdd'];
}
and i suggest that you dont use $_SESSION in your radio button page. it leads to complications and it will always be over written.