How to get the value in my form - php

Good Day
my main problem is how can I supply the value in the form to query.
this code gets the id from other table:
if (isset($_GET['machine_no_id']) && is_numeric($_GET['machine_no_id']))
{
$id = $_GET['machine_no_id'];
echo $id;
}
then this code only get the value in form:
if (isset($_GET['submit']))
{
$qty = $_GET['qty'];
echo $qty;
}
enter code here

Assign name to your submit button like <input type="submit" Value="Add" name="submit">.
and in php part,
if(isset($_GET['submit'])){
//insert query
}
and you will get the quantity value by $_GET['add_qty']
and its better if u use the method = 'post' and call the variables using $_POST instead of $_GET

Try this
<input type="submit" Value="Add" name="submit">
if (isset($_GET['submit']))
{
// get id value
$id = $_GET['machine_no_id'];
$qty = $_GET['add_qty'];
}

Try to add submit button in HTML form like,
<input type="submit" Value="Add" name="submit">
And set method to 'POST' in HTML form As it is better to use POST method instead of GET, In PHP code use $_POST to check whether form is submitted or not like,
if($_POST){
// put your PHP code here
}
And Then you can get the quantity value by $_POST['add_qty'];

<html>
<body>
<form action="" method="POST">
<table border="1" cellpadding="5" align="center">
<tr>
<td>ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="add_qty"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" Value="Add"></td>
</tr>
/table>
</form>
</body>
<?php
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_REQUEST['submit']))
{
// get id value
$id = $_POST['id'];
$qty = $_POST['add_qty'];
echo $id;
echo $qty;
//mysql_query("UPDATE master_tbl SET qty_left = qty_left + '$qty' WHERE machine_no_id='$id'");
//header("location: show_result.php");
}
?>

Related

CI Multi Value insert

I use jquery append for more fields in CI. But Don't know How to to insert multi value into the Db. Please help me.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$tr="<tr><td><input type='input' name='title' /></td><td><textarea name='text'></textarea></td></tr>";
$("#btn2").click(function(){
$("#tb").append($tr);
});
});
</script>
<?php echo validation_errors(); ?>
<button id="btn2">add New</button>
<?php echo form_open('welcome/create'); ?>
<table id="tb" width="100%">
<tr>
<td>Title
</td>
<td>Text
</td>
</tr>
</table>
<input type="submit" name="submit" value="Create news item" />
</form>
Try this
Use [] with the name attribute of an input field
$tr = "<tr><td><input type='input' name='title[]' /></td><td><textarea name='text[]'></textarea></td></tr>";
This will take all the values as an array from input field having the same name attribute
Make You input/textarea field as array:
<tr><td><input type='input' name='title[]' /></td><td><textarea name='text[]'></textarea></td></tr>
In php controller:
//Load model
//Pass Post data to model function
$this->model_name->function_name($_POST);
In php model:
function_name($data)
{
$name_arr = $data['name'];
$text_arr = $data['text'];
for($i=0;$i<count($name_arr);$i++)
{
//Insert query with values like $name_arr[$i],$text_arr[$i];
}
}
Note: Still you are not able to understand then go through CI Tutorials.

Foreach through elements retain the last value of the element

I would like to get the value of what I have echoed in the value of an HTML element but I used a foreach so it retains only the last value of the last loop. I would update the reqStatus depending on its ID through a post method to pass the variables.
HTML w/ foreach:
<form method = "POST" action="">
<table>
<th> Request ID:</th>
<th> Request Status:</th>
<?php
$requests = new OrderRequests();
$requests->getAllOrders();
foreach($requests->orderList as $oList){?>
<tr>
<td>
<input type="hidden" name="reqId" value="<?php echo $oList["request_id"];?>"><?php echo $oList["request_id"];?>
</td>
<td>
<input type="hidden" name ="reqStatus" value="<?php echo $oList["request_status"];?> "><?php echo $oList["request_status"];?>
</td>
<?php if($oList["request_status"]=="Delivered"){?>
<input type="hidden" id="reqIDUpdate" name="reqIDUpdate" value ="<?php echo $oList["request_id"];?>">
<td>
<input type="submit" class="button" value="Confirm Delivery">
</td>
<?php }}?>
</tr>
</table>
</form>
After POST:
if (!empty($_POST["reqIDUpdate"])){
$requestIDUpdate = $_POST["reqIDUpdate"];
global $connection;
$sql = "UPDATE order_request SET request_status = 'Received Delivery'
WHERE request_id = {$requestIDUpdate} ";
mysqli_query($connection, $sql);
unset($_POST["reqIDUpdate"]);
}
Though you are echoing unique values, the value that gets assigned to the form hidden fields (reqId and reqStatus) are getting overwritten by new values in the foreach loop. By the end of the foreach loop, only the last values are saved in these hidden fields.

how to push data from html grid to mysql?

I am trying to push the data from this simple html grid into sql but I can't do it. in the beginning, I got the error that there was no index for the var impressions. I fixed that. when I use just the advertiser value I can push the data into mysql while with the second value, I can't succeed. can you explain me what I am doing wrong?
Thanks in advance
<?php
include_once 'con_ui.php';
if(isset($_POST['btn-save']))
{
$advertiser = $_POST["advertiser"];
$impressions = (isset($POST["impressions"])?
$_POST["impressions"]:'');
$sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
mysql_query($sql_query);
// sql query for inserting data into database
}
?>
<html>
<head>
</head>
<body>
<form method="post">
<table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1>
<tr>
<th>advertiser</th>
<th>impressions</th>
</tr>
<td>
<select name="advertiser" id="advertiser">
<option value="">Select advertiser</option>
<option value = "Brita ">Brita</option>
<option value = "Sammontana">Sammontana</option>
</select>
</td>
<td name= "impressions" id="impressions" >1000000</td>
<td>
<button type="submit" name="btn-save"><strong>SAVE</strong></button>
</td>
</form>
</body>
</html>
There's a few things wrong here:
Firstly the $POST in
$impressions = (isset($POST["impressions"])?$_POST["impressions"]:'');
Is missing an underscore and should be a $_POST
$impressions = (isset($_POST["impressions"])?$_POST["impressions"]:'');
Secondly the browser won't recognize
<td name= "impressions" id="impressions" >1000000</td>
as a form field. If you want to pass on values that the user can't edit you have a few options. You can use a hidden field like:
<td><input type="hidden" name="impressions" id="impressions" value="1000000">1000000</td>
or you can use a text input and disable user input like
<td><input type="text" name="impressions" id="impressions" value="1000000" disabled></td>
here you have the details of the code that i am using to achieve for adding the raws automatically, have the users entering data and keeping the code editable
Hey thanks for your reply. Problem being is i need to have the user enter the date into the cell and and have and the content should be editable. in addition i'm having a button add row, to insert a new raw but doesn't work if use the form type as input. here you have the full code
<?php
include_once 'con_ui.php';
if(isset($_POST['btn-save']))
{
// variables for input data
$advertiser = $_POST["advertiser"];
$impressions = (isset($_POST["impressions"])?
$_POST["impressions"]:'');
// variables for input data
// sql query for inserting data into database
$sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
mysql_query($sql_query);
// sql query for inserting data into database
}
?>
<html>
<head>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var new_client = document.getElementById("advertiser_row1").innerHTML;
var new_impressions = document.getElementById("impressions_row1").innerHTML;
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
}
</script>
</head>
<body>
<table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1>
<form method="post">
<tr>
<th>advertiser</th>
<th>impressions</th>
</tr>
<td>
<select name="advertiser" id="advertiser_row1">
<option value="">Select advertiser</option>
<option value = "Brita ">Brita</option>
<option value = "Sammontana">Sammontana</option>
</select>
</td>
<td><input type="text" name="impressions" id="impressions_row1" value="1000000"></td>
<td>
<button onclick="myFunction()">Add Row</button>
</td>
<td>
<button type="submit" name="btn-save">Save</button>
</td>
</tr>
</form>
</table>
</body>
</html>

why if (isset($_POST['accept'])) not working?

i have small problem and i don't see any mistakes in my code and server also don't returns any errors but clouse if (isset($_POST['accept'])) doesn't recognize that button was clicked.And is it some way to get value of button id?Please Help.
include '../../config.php';
db_connect();
$zapytanie = "SELECT * FROM messages ORDER BY message_id DESC";
$r = mysql_query ($zapytanie) or die(mysql_error());
$war="L".$_SESSION['lekarz_id'];
echo $war;
if (isset($_POST['accept']))
{
echo "something";
}
else{
while ($row = mysql_fetch_array ($r))
{
if($war == $row['message_recipient'] AND !$row['message_title']=='LP' ){
echo $war;
print "<table border=2 width=98% align=center>
<tr><td><font size=1><p align=left>Od: {$row['message_recipient']}</td> <td><p align=right>Wysłana: {$row['date_of_posting']}</align></font></td></tr>
<tr><td><p align=left><font size=1>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b></td></tr>
<tr><td colspan=2><br /> {$row['message_text']}</align></font></td></tr>
</table><hr />";
}
else if($war == $row['message_recipient'] AND $row['message_title']=='LP'){
echo "
<form>
<table border=2 width=80%>
<tr>
<td>Od:Wysłana: {$row['date_of_posting']}</td>
<td><p align=center>Wysłana: {$row['date_of_posting']}</align></font></td>
</tr>
<tr>
<td><p align=center><font size=5>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b>
</td>
</tr>
<tr>
<td ><br /> <font size=5>{$row['message_text']}</align></font>
</td>
</tr>
<tr>";
$id = $row['message_sender'];
$id=substr($id, -1);
print'
<td>
<form action="messages.php" method="post">
<input type="submit" name="accept" value="accept" id=.{$id}./>
</form>
</td>
<td> </td>
</tr>
</table>
</form><hr />
';
}
}
db_close($db);
}
?>
You didn't explicitly set the form action to POST. As a result your form values willbe sent via GET.
Change:
<form>
to
<form method="POST">
Or look for for your value in the $_GET superglobal.
if (isset($_GET['accept']))
You also have a form embedded within a second form. Removing the outer form would also solve your problem.
Check for $_REQUEST['accept']. This combines $_GET, $_POST and $_COOKIE. It will contain the form value regardless of submission method.
http://www.php.net/manual/en/reserved.variables.request.php
I also have to mention, that it's not a good idea to use submit button value to send parameter. What if you need to write "Accept" instead of "accept" on your button?
if ($_POST['accept']=='accept') will fail for such button. Better practice is to use hidden field with name="accept" (<input type="hidden" name="accept" value="any_value" />) and let submit button to do what it has to do - to submit form.
Also it makes your form reusable
phpfidle

save PHP form data without submit on page refresh

I have two forms in the page one is filter, second is the item list, when I apply the filter to item list form, selected values reset to default. For filter I can save selected values, because I submit this form via POST method, but other remains not submited is possible to save selected that form values after page refresh? Here is some code of second form:
<form name="forma" method="post" onMouseOver="kaina();" onSubmit="return tikrinimas()" action="pagrindinis.php?page=generuoti.php">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>";
?></td>
</tr>
</form>
You need to set the selected attribute of your select element based on what is in $_POST. Something like:
$selected = $_POST['Client'];
while($nt=mysql_fetch_array($result)){
if ($selected == $nt[pers_code]) {
echo "<option value=$nt[pers_code] selected="selected">$nt[name] $nt[surname]</option>";
}
else {
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
}
Also note that you should probably sanitize any values you get from $_POST.
Unfortunately there is no easy way to do this, and it's something that PHP and web developers in general have maligned for years, because repopulating form fields is never easy AND clean.
Values in the form you aren't submitting wont be sent (via GET or POST), so you're left with writing a custom workaround.
While it's a bit Q&D, I would recommend sending an Ajax call on form submit to store the values for your second form in the $_SESSION variable.
I'm sorry to say there's no easy alternative - due to the stateless nature of HTTP requests, this is something that every programmer has to struggle with.
just to break the question down to a more simplified form, let's assume we don't have the hassle of working with dropdowns. The real issue you're having is to take a value from form 1 and have it work even after form 2 has been submitted.
If you use a hidden field on form 2, of the same name as form 1, and populate it based on both form 1 and form 2's POST data, it will be "remembered"
<? // mostly html with a bit of php. ?>
<form id="f1" method="POST">
<input type ="text" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type="submit">
</form>
<form id="f2" method="POST">
<input type="hidden" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type ="text" name="f2val" value="<?= htmlspecialchars( array_key_exists( 'f2val', $_POST )?$_POST['f2val']:'' ); ?>">
<input type="submit">
</form>
<script type="text/javascript" src="js/jquery1.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#forma").submit(function(event) {
var 1stformfield = $form.find( 'input[name="misc"]' ).val();
/* stop form from submitting normally */
//event.preventDefault();
$.post("1stformsubmit.php", $("#forma").serialize());
//after it posts you can have it do whatever
//just post code here
$('#iframe1').attr('src', 'iframepage.php?client=' + 1stformfield);
window.frames["iframe1"].location.reload();
return false;
});
});
</script>
<form name="1stform" method="post" action="/">
<input type="text" name="misc" id="misc" />
<input type="submit" name="submit" id="submit" value="submit 1st form"/>
</form>
<iframe id="iframe1" src="" scrolling="no" ></iframe>
iframe page
<form name="forma" method="post" onmouseover="kaina();" action="/">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$selected = $_GET['Client'];
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>"; ?></td>
</tr>
</form>
This will post all inputs inside your form to the file you specify then you can have it do whatever you like, for example show a thank you message..Hope it helps.

Categories