Need to send an array in PHP - php

I'm trying to send multiples values to a PHP form.
This is the form that I use to send values:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td><?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php } while ($row_test1 = mysqli_fetch_assoc($test1)); ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
And this is the action file:
if(isset($_POST['Update']))
{
$ProductID=$_POST['Product'];
print_r(json_encode($ProductID));
}
The problem that I have is when I send multiple values, for example the below table:
ProductID SKU
103 WH004BI
137 VO007BI
I alway get this result:
{"id":{"ProductID":"137","SKU":"VO007BI"}}
When I actually want to get a result like this:
{"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]}

You're going to want to do something like this:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php $i = 0; ?>
<?php while ($row_test1 = mysqli_fetch_assoc($test1) { ?>
<tr>
<td>
<?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td>
<?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php $i++; ?>
<?php } ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
Note that I have put a $i = 0 at the start of the loop, and $i++ at the end of the loop.
Additionally, I have changed the names to the following:
name="Product[id][<?= $i; ?>][SKU]"
Which will prevent the issue you were having in the comment section regarding an ill-formed array.

Related

HTML form WON'T POST data ($_POST[] is empty)

One of my html forms is not POSTing data, the submit button respect the action attribute and redirect the page as spected BUT $_POST[] is empty for some reason. could sonebody help me find the issue here?
I already tried using html method POST and even ajax to process the form and submit the data, and theres no case. $_POST[] will allways stay empty (this is the only page that does that, I hava an exact copy of the form but without the GET method and it works fine...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="nocep-rules.css" rel="stylesheet" type="text/css">
<?php
session_start();
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<?php
if(!isset($_SESSION['logged'])){
header("location: /bubale/login.php");
}
?>
</head>
<body>
<div class="header">
<img class="logo" src="/bubale/img/logo1.png" alt="" />
<img class="foro-title" src="/bubale/img/loja_title.png" alt="" />
</div>
<div class="menu">
<nav class="site-nav">
<ul>
<li>HOME</li>
<li>LOJA</li>
<li>FORUM</li>
<li>CHECKOUT</li>
<li>SAIR</li>
</ul>
</nav>
</div>
<div class="info">
<table width="100%">
<tr>
<td width="20%">
<h2 class="text">USUÁRIO:</h2>
<?php echo $_SESSION['logged']; ?>
<h2 class="text2">PONTOS:</h2>
<?php
$sql5= "Select points from profile where user = '".$_SESSION['logged']."'";
$result5 = mysqli_query($link, $sql5);
if(mysqli_num_rows($result5)==1){
while($row5 = mysqli_fetch_assoc($result5)) {
echo $row5['points'];
}
}
?>
</td>
<td width="30%">
<?php
$sql2= "select * from bolsinhas where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result2 = mysqli_query($link, $sql2);
$count = mysqli_num_rows($result2);
?>
<h2 class="text">BOLSINHAS:</h2>
<?php echo $count; ?>
<h2 class="text2">PRODUTOS:</h2>
<?php
$sql3= "select * from fullsize where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result3 = mysqli_query($link, $sql3);
$count3 = mysqli_num_rows($result3);
echo $count3;
?>
</td>
<td>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
}else{$cart_count = "0";}
?>
<img id="icon1" src="img/basket.png" title="Compras" ><span id = "count_wrapper"><a id="count">
<?php echo $cart_count; ?></a></span>
<img class="icon" src="img/sale.png" title="Promoção" alt="" >
<img class="icon" src="img/points.png" title="Fidelidade Buba'le" alt="" >
</td>
</table>
</div>
<h1 class="frete">FRETE:</h1>
<div class="wrapper_frete">
<?php
$rua1 = $_GET['1'];
$numero1 = $_GET['2'];
$apt1 = $_GET['3'];
$cep1 = $_GET['4'];
?>
<table id="forma_frete" width="100%">
<tbody>
<th>DADOS PESSOAIS</th>
<th>OOPS!!!</th>
<tr>
<form id="modform" method="POST" action="nocep2.php">
<td width="40%" class="td2">
<table id="nested" width="100%">
<tr>
<td id="td-nested-left" width="30%">
<label class="label-form" for="user">USUÁRIO:</label><br>
<label class="label-form" for="rua">RUA:</label><br>
<label class="label-form" for="numero">NÚMERO:</label><br>
<label class="label-form" for="apt">APT:</label><br>
<label class="label-form" for="cep">CEP:</label><br>
</td>
<td id="td-nested-right">
<input name="user" form="buttons" type="text" maxlenght="20" disabled value="<?php echo $_SESSION['logged']; ?>" /><br>
<input name="rua" form="buttons" type="text" value="<?php echo $rua1; ?>" /><br>
<input name="numero" form="buttons" type="text" value="<?php echo $numero1;?>" /><br>
<input name="apt" form="buttons" type="text" value="<?php echo $apt1; ?>" /><br>
<input name="cep" form="buttons" type="text" value="<?php echo $cep1;?>" />
</td>
</tr>
</table>
<input type="submit" class="refresh" value="ATUALIZAR">
</td>
</form>
<td id="td_mensage">
<h3 id="mensage">-irrelevant text-. </h3>
</td>
</tr>
<tr>
<td id="td_ob" colspan="2">
<h3 id="comentario">OBSERVAÇAÕ:</h3>
<textarea form="buttons" name="observacion" id="text_ob" placeholder="Deixe aqui sua observaçaõ..."></textarea>
</td>
</tr>
</tbody>
</table>
</div>
<h1 class="frete">SUA COMPRA:</h1>
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<?php } ?>
<table class="table">
<tbody>
<tr>
<th></th>
<th>PRODUTO</th>
<th>QUANTIDADE</th>
<th>VALOR UNITARIO</th>
<th>VALOR TOTAL</th>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td id="imgtd"><img src='<?php echo $product["image"]; ?>' width="90" height="80" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
</form></td>
<td><form method='post' action=''>
<input type='hidden' name='code2' value="<?php echo $product["code"]; ?>" />
<?php echo $product["quantity"];?>
</form></td>
<td><?php echo "R$".$product["price"]; ?></td>
<td><?php echo "R$".$product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td class="td_tot" colspan="5" align="right"><strong id="total">TOTAL: <?php echo "R$".$total_price; ?></strong></td>
</tr>
</tbody>
</table>
<form method="post" action="contact-venta.php" id="buttons">
<?php
?>
<input type="hidden" name="compras" value="<?php echo print_r($_SESSION["shopping_cart"], TRUE); ?>"/>
<input type="hidden" name="total" value="<?php echo $total_price; ?>">
<input type="submit" id="submit" value="ENVIAR">
</form>
<?php mysqli_close($link); ?>
</body>
</html>
the issue is with the first submit (the one with the value ACTUALIZAR), the form will go to nocep2.php BUT $_POST[] will remain empty .... I posted the entire page just in case there is an issue I'm not seeing....
Your HTML is rather confused.
Firstly, <form> is not a valid element in <tr>. The browser is rendering them outside the table, and separate from your <input>. Move your form tags inside the <td>, or move them outside the <table>
Eg
<table>
<tr>
<td>
<form method="POST">
<input type="text" name="inputName">
<input type="submit">
</form>
</td>
</tr>
</table>
Or, if you need to spread your form over several cells:
<form method="POST">
<table>
<tr>
<td>
<input type="text" name="inputName1">
</td>
<td>
<input type="text" name="inputName2">
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
Secondly, you're creating a number of <input> elements of type "hidden", each with their own form tags, so they are each part of a different form. Reorder your code so that the hidden elements appear within the same <form> as the rest of your <input>

I am trying to find 1 selected rows at once after click on next button then it's to next row

I am trying to fetch records from my table, but it shows all the records at once.
I want a single row data to be viewed. After clicking on next button, it jumps to the next row. How can we achieve this?
<?php include("conect.php"); ?>
<?php
session_start();
if($_SESSION['u_name'==''])
{
echo "log in failed";
echo '<script> window.locaion="index.php";</script>';
}?>
<?php
$query=mysql_query("select * from question_paper ");
$i=1;
while($res= mysql_fetch_array($query))
{
$id=$row['main_id'];
$question=$res['question'];
$optionA=$res['option1'];
$optionB=$res['option2'];
$optionC=$res['option3'];
$optionD=$res['option4'];
?>
<html>
<head><title>exam</title></head>
<body>
<form name="student_form" action="" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="36" colspan="2" bgcolor="#85A157"> (<?php echo $i;?>)<?php echo $question;?> </td>
</tr>
<tr>
<td colspan="2"><table width="100%" height="64" border="2" cellpadding="0" cellspacing="0" bordercolor="#A6BF79">
<tr>
<td height="32" width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionA;?>" id="optionA" />
<?php echo $optionA;?></td>
<td width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionC;?>" id="optionC" />
<?php echo $optionC;?></td>
</tr>
<tr>
<td height="32" width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionB;?>" id="optionB" />
<?php echo $optionB;?></td>
<td width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionD;?>" id="optionD" />
<?php echo $optionD;?></td>
</tr>
</table></td>
</tr>
</table>
<?php
$i=$i+1;
}
?>
<?php
$records = mysql_num_rows($query);
if($records!=0)
{
?>
<input type="submit" name="button" id="button" value="Submit" />
<?php
}
?>
</form>
</body>
</html>
Its better to use pagination and with select query use limit and for pagination
see the following link
http://www.tutorialspoint.com/php/mysql_paging_php.htm
$sql="SELECT * FROM tbl_name WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM tbl_name ) ORDER BY id LIMIT $no_of_question_you_want to select";

how to save radio button has been selected at the time the page is reloaded?

how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.
here is my view code
<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><b><?php echo $no.'. ';?></b></td>
<td><?php echo $row->pertanyaan;?></td>
</tr>
<tr>
<td rowspan="5"> </td>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a. <?php echo $row->a;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b. <?php echo $row->b;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c. <?php echo $row->c;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d. <?php echo $row->d;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e. <?php echo $row->e;?></label></span></td>
</tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
</tr>
</table>
</form>
</div>
here my controller code
public function mulai_tes(){
$id=$this->uri->segment(3);
$this->load->model('m_mahasiswa');
$result=$this->m_mahasiswa->data_mahasiswa();
foreach($result->result() as $row):
$data['user']=$row->username;
$data['nama']=$row->nama;
endforeach;
$cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
foreach($cek->result() as $c){
if($c->id_sesi==$id){ ?>
<script type="text/javascript" language="javascript">
alert("Anda telah mengikuti tes soal ini");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
}
}
$data['hasil']=$this->m_mahasiswa->mulaites($id);
$data['jumlah']=$data['hasil']->num_rows();
$data['judul']='Mulai Tes';
$this->load->view('elearning/template',$data);
}
How I set cookie and where I can put cookie code in controller?
Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).

pass relevant hidden field values from the form to the controller

Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>

Update multiple rows at once

I have this code which permits me to display all the data in the database as a textarea, I need to update them by clicking a update button!
Based on this one, is supposed to make me edit them, but when i click submit it doesn't...
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $id[]=$rows['id']; ?>
<? echo $rows['id']; ?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="Submit" value="Submit"/>
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit)
{
for($i=0;$i<$count;$i++)
{
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1)
{
header("location:update_multiple.php");
}
Yes because you forget your id's
<? $id[]=$rows['id']; ?> cannot be passed like that
<input type="hidden" name="id[]" value ="<?php echo $rows['id']; ?>" /><? echo $rows['id']; ?>
and script if($Submit){ should be if($_POST['Submit'] != ''){
You aren't defining $Submit in your post, so the stuff in the { ... } is never executed.
You should try something like this for your update:
if(isset($_POST[$name]))
{
// update stuff
}
In your code, as the if statement never executes, $result is never set, so the user isn't redirected away - it will just show the same page each time.
To check if a post occur when clicking a button should be set as follow:
In the <form>tag add the following
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
And lastly, where you check if the button was clicked:
if(isset($_POST['Submit'])) {
//Update fields
}
Remember that the submit button name field is case sensitive in php

Categories