I need to insert values selected by pg_query and organized into array into specific fields of table with input type=text:
<tr>
<td>
<label for="street">Street:</label>
</td>
<td>
<input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)">
</td>
</tr>
<tr>
<td>
<label for="building_no">Building No:</label>
</td>
<td>
<input type="text" placeholder="Number of building (XXXX)" name="bld_no">
</td>
</tr>
<tr>
<td>
<label for="ID Sector">ID Sector:</label>
</td>
<td>
<input type="text" placeholder="IotaNet Sector (5)" name="id_sector">
</td>
</tr>
All values are strings and array has all selected values:
$qexist = "SELECT
test.tb.street,
test.tb.bld_no,
test.tb.id_sector,
FROM test.tb
WHERE test.tb.street = '$street'
AND test.tb.bld_no = '$bld_no'
";
$ress = pg_query($qexist);
while ($row = pg_fetch_array($ress)) {
// values of $row[] have to be inserted into exact fields
echo $row[1].'street';
echo $row[2].'bld_no';
echo $row[3].'id_sector';
? ? ? ? ? ?
var_dump($row);
/* result of var_dump
array(30) { [0]=> string(20) "90315464612890004 " ["id_bld"]=> string(20) "90315464612890004 "
[1]=> string(50) "Street" ["street"]=> string(50) "Street"
[2]=> string(4) "0004" ["bld_no"]=> string(4) "0004"
[3]=> string(1) "5" ["id_sector"]=> string(1) "5"................ */
}
Please advise how to solve this issue. I tried different ways, but unsuccessfully, despite the fact that the site displays echo and var_damp results, the fields remain empty. Thank you.
<input type="text" placeholder="Street name" required="required" id="street" name="street" value="<?php echo $row[1] ?>">
<input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $row[2] ?>">
<input type="text" placeholder="IotaNet Sector (5)" name="id_sector" value="<?php echo $row[3]?>">
I have found solution by experimental way, but it's works. I have created new variable array $value[] , assign them values of $row[] and code begin to work
<?php>
$qexist = "SELECT
test.tb.id_bld,
test.tb.street,
test.tb.bld_no,
FROM test.tb
WHERE test.tb.street = '$street'
AND test.tb.bld_no = '$bld_no'
";
$ress = pg_query($qexist);
while ($row = pg_fetch_array($ress)) {
$value[0] = $row[0];
$value[1] = $row[1];
$value[2] = $row[2];
}
?>
<table>
<tbody>
<form method="get" action="t54646.php">
<tr>
<td>
<label for="id_bld">Building ID:</label>
</td>
<td>
<input type="text" placeholder="Not for fill - system ordered" name="id_bld" value="<?php echo $value[0] ?>">
</td>
</tr>
<tr>
<td>
<label for="street">Street:</label>
</td>
<td>
<input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)" value="<?php echo $value[1] ?>">
</td>
</tr>
<tr>
<td>
<label for="building_no">Building No:</label>
</td>
<td>
<input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $value[2] ?>">
</td>
</tr>
If somebody can explain why array begin works after variables renaming this will be useful for many people
Related
I am using a table inside HTML form and when i enter the details in form I want the data to be saved in two rows. Currently when i input data the row 1 gets overwritten by row 2 and only information submitted in row two gets displayed.
<form name="contact-form" action="" method="post" id="contact-form">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email ID</th>
<th>Phone No</th>
<th>Comments</th>
</tr>
</thead>
<tr>
<td> <input type="text" class="form-control" name="your_name[]" placeholder="Name" required> </td>
<td> <input type="text" class="form-control" name="your_email[]" placeholder="Email" required></td>
<td> <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required></td>
<td><input type="text" class="form-control" name="comments[]" placeholder="Comments" required></td>
</tr>
<tr>
<td> <input type="text" class="form-control" name="your_name[]" placeholder="Name" required> </td>
<td> <input type="text" class="form-control" name="your_email[]" placeholder="Email" required></td>
<td> <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required></td>
<td><input type="text" class="form-control" name="comments[]" placeholder="Comments" required></td>
</tr>
</table>
<button type="submit" class="btn btn-primary" name="submit" value="Submit" id="submit_form">Submit</button>
<img src="img/loading.gif" id="loading-img">
</form>
on the response side my code looks like this :
<?php
require_once("database_connection.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))
{
require_once("contact_mail.php");
for ($i = 0; $i<count($_POST['your_name']); $i++){
$yourName = $conn->real_escape_string($_POST['your_name'][$i]);
$yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
$yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
$comments = $conn->real_escape_string($_POST['comments'][$i]);
$sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";
}
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
else
{
echo "Thank you! We will contact you soon";
}
}
else
{
echo "Please fill Name and Email";
}
?>
Your brackets were misplaced, check my code. Disclaimer - have'nt tried executing it.
<?php
require_once("database_connection.php");
require_once("contact_mail.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))
{
for ($i = 0; $i<count($_POST['your_name']); $i++) {
$yourName = $conn->real_escape_string($_POST['your_name'][$i]);
$yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
$yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
$comments = $conn->real_escape_string($_POST['comments'][$i]);
$sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
}
echo "Thank you! We will contact you soon";
}
else
{
echo "Please fill Name and Email";
}
?>
Just a suggestion - try using MySQLi with prepared statements
I have form which lists all the in stock products from database with price.User will add quantity , it calculated the amounts and submits the form. in the list there are 5 products. Each time user can fill any one or all the 5. While submiting, i want to get only quantity filled values to database. I am not getting how to do it
Here is my code
<tr>
<td><?php echo $Myrow['product']; ?></td>
<input type="hidden" name="product_id[]" value="<?php echo Myrow['id']; ?>" />
<td><input type="text" name="rte[]" value="<?php echo $Myrow['rate']; ?>" readonly="readonly" class="price form-control" /></td>
<td><input type="text" name="Qty[]" class="Qty form-control" /></td>
<td><input type="text" id="amount" name="amount[]" class="amount form-control" /></td>
</tr>
<?php } ?>
</tbody>
How can is submit only non-emptied values. Please help
I think this could help you
$product_id=$_POST['product_id'];
$qty=$_POST['qty'];
foreach ($qty as $key => $value) {
if($value){
.... here you can save $product_id[$key], qty and other
}
}
I have a huge form which has a table in it. I add lines of this table with jQuery when the user press some button and try to catch all theses values in PHP
But I can't get other values than the first line of the table!
Got
Undefined index: categorie#2
when I'm trying to get it by $_POST['categorie#2']
HTML looks like this:
<form>
...[some working inputs]
<table id="matos" class="table table-responsive synthese">
<thead>
<tr>
<th>Matériel</th>
<th>Fournisseur</th>
<th>Numéro de série</th>
<th>Catégorie</th>
<th>Description</th>
<th>Date d'achat</th>
<th>Etat</th>
</tr>
</thead>
<tbody>
<tr class="" id="ligne#1">
<td><input type="text" name="materiel#1" id="materiel#1" class="form-control" value=""></td>
<td>
<select name="fournisseur#1" id="fournisseur#1" class="form-control" value="">
<?php
$list = listing_fournisseurs();
foreach ($list as $key => $value)
{
echo '<option value='.$key.'>'.$value.'</option>';
}
?>
</select>
</td>
<td><input type="text" name="num_serie#1" id="num_serie#1" class="form-control" value=""></td>
<td>
<select name="categorie#1" id="categorie#1" class="form-control" value="">
<?php
$list = listing_categories();
foreach ($list as $key => $value) {
echo ' <option value='.$key.'>'.$value.'</option>';
}
?>
</select>
</td>
<td><input type="text" name="description_materiel#1" id="description_materiel#1" class="form-control" value=""></td>
<td><input type="text" name="buy_date#1" id="buy_date#1" class="date form-control" value=""></td>
<td>
<select name="etat#1" id="etat#1" class="form-control" value="">
<?php
$list = listing_etats();
foreach ($list as $key => $value) {
echo ' <option value='.$key.'>'.$value.'</option>';
}
?>
</select>
</td>
</tr>
</tbody>
</table>
How I add a line in jQuery?
var num= parseInt($('#matos tr:last').prop("id").split('#')[1])+1;
$('#matos tr:last').after('<tr id="ligne#'+num+'">'+
'<td><input type="text" name="materiel#'+num+'" id="materiel#'+num+'" class="form-control" value=""></td>'+
'<td><select name="fournisseur#'+num+'" id="fournisseur#'+num+'" class="form-control" value="">'+
opt_fournisseurs+
'</select></td>'+
'<td><input type="text" name="num_serie#'+num+'" id="num_serie#'+num+'" class="form-control" value=""></td>'+
'<td><select name="categorie#'+num+'" id="categorie#'+num+'" class="form-control" value="">'+
opt_categories+
'</select></td><td><input type="text" name="description_materiel#'+num+'" id="description_materiel#'+num+'" class="form-control" value=""></td>'+
'<td><input type="text" name="buy_date#'+num+'" id="buy_date#'+num+'" class="date form-control" value=""></td>'+
'<td><select name="etat#1" id="etat#1" class="form-control" value="">'+
opt_states+
'</select></td></tr>');
$('#nbLignes').val(num);
And well in PHP I'm trying:
$_POST['materiel#2'] // doesn't work
$_POST['materiel#1'] // works ! ( because first line ! )
I've read some issues that form don't work if they're not into table tr td ... But in my case they are ... What's wrong ?
My bad here ! I just messed up with my DOM-structure ...
You shouldn't close a div, that you oppened before your <form>, before the end of your </form> that's why !
Won't work :
<div id="some_div">
<form>
[Some inputs...]
</div><!-- /some_div -->
</form>
Should work :
<div id="some_div">
<form>
[Some inputs...]
</form>
</div><!-- /some_div -->
Seems obvious but not when you have a very large DOM ;)
You will make your life ALOT easier, if you use this naming convention <input name="category[]" /> instead of <input name="category#1" />
That way you will get your variables in an array on the PHP end, which makes traversing the data ALOT easier!
eg:
<?php
foreach($_POST['categories'] as $num => $value){
}
?>
var opt_fournisseurs = '<option value="gg">gg</option><option value="dd">dd</option>';
var opt_categories = '<option value="ss">ss</option><option value="aa">aa</option>';
var opt_states = '<option value="ww">ww</option><option value="ee">ee</option>';
var num= parseInt($('#matos tr:last').prop("id").split('#')[1])+1;
$('#matos tr:last').after('<tr id="ligne#'+num+'">'+
'<td><input type="text" name="materiel#'+num+'" id="materiel#'+num+'" class="form-control" value=""></td>'+
'<td><select name="fournisseur#'+num+'" id="fournisseur#'+num+'" class="form-control" value="">'+
opt_fournisseurs+
'</select></td>'+
'<td><input type="text" name="num_serie#'+num+'" id="num_serie#'+num+'" class="form-control" value=""></td>'+
'<td><select name="categorie#'+num+'" id="categorie#'+num+'" class="form-control" value="">'+
opt_categories+
'</select></td><td><input type="text" name="description_materiel#'+num+'" id="description_materiel#'+num+'" class="form-control" value=""></td>'+
'<td><input type="text" name="buy_date#'+num+'" id="buy_date#'+num+'" class="date form-control" value=""></td>'+
'<td><select name="etat#1" id="etat#1" class="form-control" value="">'+
opt_states+
'</select></td></tr>');
$('#nbLignes').val(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
var_dump($_POST);
?>
<form method="post">
<table id="matos" class="table table-responsive synthese">
<thead>
<tr>
<th>Matériel</th>
<th>Fournisseur</th>
<th>Numéro de série</th>
<th>Catégorie</th>
<th>Description</th>
<th>Date d'achat</th>
<th>Etat</th>
</tr>
</thead>
<tbody>
<tr class="" id="ligne#1">
<td><input type="text" name="materiel#1" id="materiel#1" class="form-control" value=""></td>
<td>
<select name="fournisseur#1" id="fournisseur#1" class="form-control" value="">
<option value="one">one</option>
<option value="two">two</option>
</select>
</td>
<td><input type="text" name="num_serie#1" id="num_serie#1" class="form-control" value=""></td>
<td>
<select name="categorie#1" id="categorie#1" class="form-control" value="">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td><input type="text" name="description_materiel#1" id="description_materiel#1" class="form-control" value=""></td>
<td><input type="text" name="buy_date#1" id="buy_date#1" class="date form-control" value=""></td>
<td>
<select name="etat#1" id="etat#1" class="form-control" value="">
<option value="1a">1a</option>
<option value="2a">2a</option>
</select>
</td>
</tr>
</tbody>
</table>
<input type="submit" name="txtsubmit" value="submit"/>
This is a sample created from the code you have provided, which was working for me.
I have my table named i_table with columns:
id name req_qty req_date rcv_qty rec_date
1 metal 1 2014-03-04
2 spring 5 2014-03-04
in my html/php:
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu
In your form, add the id as the key -
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
Then on your insert.php where you post your form, get the id in your foreach -
<?php
if(isset($_POST['id'])){
foreach($_POST['id'] as $id=>$value){
$sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
mysql_query($sql,$con);
}
}
?>
note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?
i make two web page,i want to get data from first page on second page,if user click edit message then move first page and show the entered data of user,here is my code:
first.php
<h1>Compose Message</h1>
<script type="text/javascript" src="<?=MURL?>/js/ckeditor/ckeditor.js"></script>
<script src="//ticket_inspector_new.com/js/tooltip.js" type="text/javascript"> </script>
<form action="" method="post" id="form" enctype="multipart/form-data">
<table class="form">
<tr class="heading">
<th style="width: 25%;">Recipients</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="campaigns">Campaigns</label>
</td>
<td>
<select name="events[]" multiple size="10" >
<?
$select = sprintf ("SELECT event_id,event_name
FROM `events`
WHERE (`user_id` = '%s') order by event_name",
$GLOBALS ['mysqli']->real_escape_string ($_SESSION['user_id']));
$res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
if ($res->num_rows > 0)
{
while($row = $res->fetch_assoc ())
{
?>
<option value="<?=$row['event_id']?>"><?=$row['event_name']?></option>
<?
}
}
?>
</select>
</td>
</tr>
<tr>
<td style="padding-left:95px;">
<label for="fromdate">Registrants From</label>
</td>
<td style="padding-left:10px;">
<input name="fromdate" type="text" value="" class="calendar time" id="fromdate" size="30" />
<label for="todate">To</label>
<input name="todate" type="text" value="" class="calendar time" id="todate" size="30" />
</td>
</tr>
<tr>
<td>
<label for="upload">Upload CSV</label>
<span class="helptip">
Select CSV file for upload.
</span>
</td>
<td>
<input name="uploadcsv" type="file" />
</td>
</tr>
<tr class="heading">
<th style="width: 25%;">Message</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="description">Description(optional)</label>
</td>
<td>
<input name="description" type="text" value="" id="description" size="35" />
</td>
</tr>
<tr>
<td>
<label for="subject">Subject</label>
</td>
<td>
<input name="subject" type="text" value="" id="subject" size="35" />
</td>
</tr>
<tr>
<td>
<label for="fromname">From Name</label>
</td>
<td>
<input name="fromname" type="text" value="" id="fromname" size="27" />
</td>
</tr>
<tr>
<td>
<label for="replyto">Reply To</label>
</td>
<td>
<input name="replyto" type="text" value="" id="replyto" size="27" />
</td>
</tr>
<tr>
<td>
<label for="senddatetime">Send Date/Time</label>
<span class="helptip">
Click the calender to select the date you wish ans select time zone from select box.
</span>
</td>
<td>
<input name="senddatetime" type="text" value="" class="calendar time" id="senddatetime" size="30" />
<select name="timezone" id="timezone">
<option value="Pacific/Honolulu">Hawaii-Aleutian Time (Honolulu, no DST)
</option><option value="America/Anchorage">Alaska Time (Anchorage)</option><option value="America/Los_Angeles"
selected="selected">Pacific Time (Los Angeles)</option><option value="America/Denver">Mountain Time (Denver)</option><option
value="America/Phoenix">Mountain Time (Phoenix, no DST)</option><option value="America/Chicago">Central Time (Chicago)
</option><option value="America/Regina">Central Time (Regina, no DST)</option><option value="America/New_York">Eastern Time
(New York)</option><option value="America/Halifax">Atlantic Time (Halifax)</option>
</select>
<script>
var list = document.getElementById('timezone');
var selval = "0";
for(var i = 0; i < list.options.length; ++i)
{
if(list.options[i].value==selval)
{
list.options[i].selected = true;
i=list.options.length;
}
}
</script>
</td>
</tr>
<tr>
<td>
<label for="message">Message</label>
</td>
<td colspan="2">
<textarea name="message" class="ckeditor" id="message" cols="90" rows="15" style="width: 100%;"></textarea>
</td>
</tr>
</table>
<table class="form">
<tr class="heading">
<th style="width:100%; background-color:#C4C4FE; font-size:10px; font-weight:normal;">Emails can take upto 30 minutes to Send.We have zero tolerance for spam messages.Every message sent out is reviewed for spam.Any spam messages sent will result in termination of account.</th>
</tr>
</table>
<p class="center_align">
<input type="submit" class="button arrow" name="submit_skip" value="Continue" />
</p>
</form>
and here is my second page:
<h1>Confirm Message</h1>
<?
$recepients=0;
$count=count($_POST['events']);
?>
<input type="button" class="button edit" name="submit_skip" value="Edit Message" />
<input type="submit" class="button email" name="submit_email" value="Send Message" />
i want when user click on edit massage it moves previous page and values shown in fields
?>
Since you want to send form to a two different scripts I suggest that you use 2 forms:
<form action="formfilling.php">
<?php
//prepare the recived POST to send back:
foreach( $_POST as $key => $value ){
if( is_array($_POST[$key]) ){ //if post is array e.g. your events[]
foreach( $_POST[$key] as $subvalue ){
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'[]"'
.' value="'.htmlspecialchars($subvalue).'">'."\n";
}
} else{
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'"'
.' value="'.htmlspecialchars($value).'">'."\n";
}
}
?>
<input type="submit" class="button edit" name="submit_skip" value="Edit Message" />
</form>
<form action="submitemail.php">
<?php //possibly show message? ?>
<input type="submit" class="button email" name="submit_email" value="Send Message" />
</form>
And then in formfilling check if input is not empty if not => echo its value
if( !empty($_POST['inputName']) ) echo htmlspecialchars($_POST['inputName']);
And for the array something like
if( !empty($_POST['inputName']) ){
foreach( $_POST['inputName'] as $val ){
//Test if the current option has the value of $val if so:
echo /*OPTION with SELECTED*/;
}
}
ALWAYS USE HTML ESCAPING when printing/echoing $_POST/$_GET
=> dont trust the users!
=> in PHP there is a htmlspecialchars() function
go to the previous page with javascript
window.history.go(-1)