This is my code for a jquery mobile application. But the form i created with php result is not working. Form is not posting data on submit. I even tried data-ajax="flase".
<table id="stock">
<? $sql = mysqli_query($con, "SELECT * FROM products WHERE `product_id` = '1'");
while($row=mysqli_fetch_array($sql)){
$name = $row['name'];
$chamecal = $row['chamecal'];
$price = $row['selling_price'];
$bprice = $row['buying_price'];
$quantity = $row['quantity'];
$manufacturer = $row['manufacturer'];
$date = $row['date'];
$edate = $row['expire'];
$pid = $row['product_id'];
?>
<form id="stockupdate" method="post" action="medupdate.php">
<tr>
<td><?=$name;?></td>
<td><?=$chamecal;?></td>
<td><?=$manufacturer;?></td>
<td><input type="text" name="medbprice" value="<?=$bprice;?>" /></td>
<td><input type="text" name="medprice" value="<?=$price;?>" /></td>
<td><?=$date;?></td>
<td><?=$edate;?></td>
<td><input type="text" name="medstock" value="<?=$quantity;?>" /></td>
<td class="ui-screen-hidden"><input type="text" name="pid" value="<?=$pid;?>" /></td>
<td>
<input type="submit" title="Update" data-icon="gear" data-theme="f" data-inline="true" data-iconpos="notext" />
</td>
</tr>
<?
}
?>
</form>
</table>
Values Handler file medupdate.php
<?php session_start();?>
<?php include_once('include/config.php');?>
<?
$pid = $_POST['pid'];
$medbprice = $_POST['medbprice'];
$medprice = $_POST['medprice'];
$medstock = $_POST['medstock'];
$sql = mysqli_query($con, "UPDATE `products` SET `quantity` = '$medstock', `buying_price` = '$medbprice' , `selling_price` = '$medprice' WHERE `product_id` = '$pid'");
if($sql){
echo 1;
}
?>
If I was to hazard a guess, it's because you're placing
<form id="stockupdate" method="post" action="medupdate.php">
directly within a table element (which is invalid markup). Move this outside your table (before your <table>). When you create invalid markup, the browsers engine tries to correct it which can lead to issues like this.
This is what chrome does to your markup:
You cannot follow the html structure, a form inside table like,
<table>
<form>
<tr>
<td>
</td>
</tr>
</form>
</table>
Here is the working code,
Form php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<form id="stockupdate" method="post" action="medupdate.php" data-ajax="false">
<table>
<tr>
<td><input type="text" name="medbprice" value="xxxx" /></td>
<td><input type="text" name="medprice" value="yyyyy" /></td>
<td><input type="text" name="medstock" value="zzzzzz" /></td>
<td class="ui-screen-hidden"><input type="text" name="pid" value="111111" /></td>
<td>
<input type="submit" title="Update" data-icon="gear" data-theme="f" data-inline="true" data-iconpos="notext" />
</td>
</tr>
</table>
</form>
</body>
</html>
medupdate.php
<?php
session_start();
$pid = $_POST['pid'];
$medbprice = $_POST['medbprice'];
$medprice = $_POST['medprice'];
$medstock = $_POST['medstock'];
echo $pid;
// your database operations
?>
Related
Am was already created form like this and working perfect but on last two forms not working, it displays warning-Undefined variable: reg_no and cost. Am trying to follow algorithm as previous forms but nothing happen. My goal is to update inserted data and here is my form
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit invoice</title>
<link rel="stylesheet" href="box_style.css" />
</head>
<body>
<?php
include ("db_con.php");
if(isset($_GET['edit_invoice'])){
$edit_i_id = $_GET['edit_invoice'];
$select_invoice = "select * from invoice where i_id='$edit_i_id'";
$run_query = mysqli_query($con, $select_invoice);
while ($row_invoice=mysqli_fetch_array($run_query)){
$i_id = $row_invoice['i_id'];
$reg_no = $row_invoice['reg_no'];
$cost = $row_invoice['cost'];
}
}
?>
<div class='form'>
<form action="" method="post" enctype="multipart/form-data" >
<table width="745" align="center" border="2">
<p style="text-align: center;"><strong><span style="text-decoration: underline;">EDIT INVOICE:</span></strong></p>
<tr>
<td align="right" bgcolor="#dbe5f1"><strong>Registration Number:</strong></td>
<td><input type="text" name="reg_no" id="reg_no" size="35" class="text" placeholder="Registration Number" value="<?php echo $reg_no; ?>" required=""/></td>
</tr>
<tr>
<td align="right" bgcolor="#dbe5f1"><strong>Cost(Tshs):</strong></td>
<td><input type="text" name="cost" id="cost" size="35" class="text" placeholder="Cost" value="<?php echo $cost; ?>" required=""/></td>
</tr>
<tr>
<td colspan="6" align="center" bgcolor="#dbe5f1" ><input type="submit" name="update" class="submit-button" value="SAVE CHANGES"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Remove while loop from your php code since update is for one record based on id
The code will be as :
if(isset($_GET['edit_invoice'])){
$edit_i_id = $_GET['edit_invoice'];
$select_invoice = "select * from invoice where i_id='$edit_i_id'";
$run_query = mysqli_query($con, $select_invoice);
$row_invoice = mysqli_fetch_array($run_query);
$i_id = $row_invoice['i_id'];
$reg_no = $row_invoice['reg_no'];
$cost = $row_invoice['cost'];
}
if isset($_GET['edit_invoice']) is false, your $reg_no is not present in later script (where you want to echo it).
Put $reg_no above your isset($_GET...) check and set it null or empty string.
$reg_no = null;
if (isset($_GET['edit_invoice'])) {
// your code...
}
Edit: Do the same for $cost and $i_id ;)
PLEASE consider Tom Uddings comment with SQL injections!
Ok, I am trying to get content from a SQL database to populate fields when a button is pushed. The problem is that no matter which button is pushed, it always sends the values of the last row to php. I am a php/mySQL noob. I apologize if this has been asked/answered before, I have been searching the site for hours and not come across anything that has helped me figure it out.
Index page image and Code:
<?php
require_once('database.php');
$query = 'SELECT * FROM omniarticles
ORDER BY recid';
$statement1 = $db->prepare($query);
$statement1->execute();
$article = $statement1->fetchAll();
$statement1->closeCursor();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>AMS</title>
<link rel="stylesheet" type="text/css" href="basic.css">
</head>
<body>
<h3>Article List</h3>
<table>
<tr>
<th>Publication Date</th>
<th>Title</th>
<th>Action</th>
</tr>
<?php foreach ($article as $articles) : ?>
<tr>
<td><?php echo $articles['publicationDate']; ?></td>
<td><?php echo $articles['title']; ?></td>
<td><form action="view.php" method="post">
<input type="hidden" name="recid"
value="<?php echo $articles['recid'];?>">
<input type="submit" value="View">
<input type="submit" value="Edit">
</td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
view.php code:
<?php
$recid = filter_input(INPUT_POST, 'recid');
require_once('database.php');
$q = 'SELECT * FROM omniarticles
WHERE recid = :recid';
$s = $db->prepare($q);
$s->bindValue(':recid', $recid);
$s->execute();
$title = $s->fetch();
$s->closeCursor();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>AMS</title>
<link rel="stylesheet" type="text/css" href="Module5Lab.css">
</head>
<body>
<label>Article Title</label>
<input type="text" name="article_title" value="<?php echo $title['recid']; ?>"/>
<br/>
<br/>
<label>Article Summary</label>
<textarea rows="4" cols="50"></textarea>
<br/>
<label>Article Content</label>
<textarea rows="20" cols="50"><?php echo $title['content']; ?></textarea>
<br/>
<label>Publication Date</label>
<input type="text" name="publication_date"/>
<br/>
</body>
</html>
The result I am getting is always for the last record, no matter which button I push.
The problem is almost certainly with the line:
$recid = filter_input(INPUT_POST, 'recid');
I'd recommend doing var_dump($_POST) and seeing what's in the post data.
I have tried to insert html table multiple row data to database but not success without error.
Please refer below for my code and kindly advice what do I missed.
Retrieve data for dropdown selection
<?php
//including the database connection file
include_once("config_db.php");
$gettminfo=mysql_query("SELECT * FROM tb_tm ORDER BY tm_abb");
?>
Html Code for the table
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/spa_invoice.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!--Add & delete rows from table dynamically-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(function(){
$('#addRow').click(function(){
var html = $('#row_template').html();
$('#dataTable').append(html);
$(".tablerow").each(function(index) {
$(this).html(index + 1);
});
});
$('#deleteRow').click(function(){
$('#dataTable .mychkbox:checked').parents('tr').remove();
calc_ttl();
});
$('#dataTable').on('change','.select-desc',function(){
var cur_val = $(this).val();
$(this).parents('tr').find('input[name="order_unit_prc[]"]').val(cur_val);
});
$('#dataTable').on('keyup','input[name="order_qty[]"]', function(){
var qty = +$(this).val();
var unit = +$(this).parents('tr').find('input[name="order_unit_prc[]"]').val();
$(this).parents('tr').find('input[name="order_amt[]"]').val(qty*unit);
calc_ttl();
});
});
</script>
<form action="" method="post" name="form_spa_inv">
<fieldset class="row3">
<table>
<tr>
<td><input type="button" value="Add Treatment" id="addRow" /></td>
<td><input type="button" value="Remove Treatment" id="deleteRow" /></td>
</tr>
</table>
<table id="dataTable" class="form" border="1">
<tr>
<td></td>
<td>No</td>
<td>Description</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Amount</td>
</tr>
</table>
<!-- table row template here -->
<table id="row_template" style="display:none">
<tr>
<td><input type="checkbox" name="chk[]" class="mychkbox" /></td>
<td class="tablerow"></td>
<td>
<select name="order_desc[]" id="order_desc" class="select-desc">
<option value="">-- Select Treatment --</option>
<?php
while($dd=mysql_fetch_array($gettminfo)) {
?>
<option value="<?php echo $dd['tm_unit_prc']?>"><?php echo $dd['tm_abb'] ?> - <?php echo $dd['tm_desc'] ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="text" name="order_qty[]" id="order_qty" size="10">
</td>
<td>
<input type="text" name="order_unit_prc[]" id="order_unit_prc" readonly>
</td>
<td>
<input type="text" name="order_amt[]" id="order_amt" readonly>
</td>
</tr>
</table>
</fieldset>
<p style="text-align: center;"><input type="submit" name="Submit" value="Create">
</form>
</body>
</html>
Insert mysql query
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
mysql_query($insert_odrdtl);
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
Change server site code to following:
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
//mysql_query($insert_odrdtl); //delete this line of code
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
I'm struggling now for a few days to get the value of a checkbox in my code.
Basically I have an admin-page where the customer can select and deselect images that will put online.
You can select and deselect images that will be shown on the homepage, and separate on the gallery-page. Both checked is also possible.
I have another checkbox that can be selected to remove the image from the list(image_deleted).
There is still a database entry and the images are still on file-system but later on I'll create a cleanup-job.
Here is my code:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_start();
require('../../lib/dbconnection.php');
require("../../lib/checklogin.php");
require("includes/upload.inc.php");
$query = 'SELECT * FROM gallery where image_deleted != 1 order by id desc';
$result=$conn->query($query);
$count=$result->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<title>Classic Nails - CMS</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="ClassicNails">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/screen.css">
<link rel="stylesheet" href="../css/libs/magnific-popup.css">
<script src="../js/libs/min/jquery-min.js" type="text/javascript"></script>
<script src="../js/min/custom-min.js" type="text/javascript"></script>
<script src="js/jquery.magnific-popup.js"></script>
<script>
$(document).ready(function() {
$('.image-link').magnificPopup({
type:'image',
gallery:{
enabled:true
}
});
});
</script>
</head>
<body>
<?php include('includes/header.inc.php'); ?>
<?php include('includes/nav.inc.php'); ?>
<div class="wrapper">
<article class="content">
<h1>Foto gallery</h1>
<?php
if (isset($uploadResult)) {
echo "<p><strong>$uploadResult</strong></p>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
<p>
<label for="image">Upload image:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="images" id="imagesd" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload" />
</p>
</form>
<div id="maincontent">
<h2>Foto informatie</h2>
<form name="FotoInformatie" id="fotoInformatie" method="post" action="">
<table>
<tr>
<td align="center"><strong>Foto<strong></td>
<td align="center"><strong>Titel</strong></td>
<td align="center"><strong>Beschrijving</strong></td>
<td align="center"><strong>Homepage</strong></td>
</tr>
<?php
while ($rows=$result->fetch_assoc()) {
?>
<tr>
<td class="hide" align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td><img src="../img/thumbs/<?php echo $rows['filename']; ?>"></td>
<td align="center"><input name="title[]" type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
<td align="center"><input name="caption[]" type="text" id="caption" value="<?php echo $rows['caption']; ?>"></td>
<td><input type="checkbox" name="checkboxHome[]" id="checkBoxHome" value="<?php echo ($rows['home'] == 1) ? 'checked="checked"' : ''; ?>"/></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="submit" value="Submit">
</tr>
</table>
</form>
</div>
</article> <!-- end of content -->
</div> <!-- end of container -->
<?php include('includes/footer.inc.php'); ?>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$caption = $_POST['caption'];
if ($_POST['checkboxHome'] == "") {
$checkboxHome[] = '0';
} else {
$checkboxHome[] = '1';
}
for($i=0;$i<$count;$i++){
$result1=mysqli_query($conn, "UPDATE gallery SET title='$title[$i]', caption='$caption[$i]', home='$checkboxHome[$i]' WHERE id='$id[$i]'");
header("location:/admin/foto-admin.php");
}
}
?>
The checkbox only works on the first row in my DB. When I select another record, only the first record in my db will be updated.
Another issue is that my checkbox won't be checked so I don't know based on my screen when a image is online or not. in the database I see a 1 of a 0.
I know that sql-injection is possible and I have to prepare the statements, but that is the next step when I get this checkbox-issue working.
Hope someone can help me with my code. It's giving me a headache.
Check these
Attribute name="id[]" for id field is not given. And it should get inside
if(isset($_POST['submit'])) {
$id = $_POST['id'];
}
Incorrect spelling in getting Post value
change
$checkboxHome = $_POST['checkboxHome'];
$checkboxFotoboek= $_POST['checkboxFotoboek'];
$checkboxDelete = $_POST['image_deleted'];
to
$checkboxHome = $_POST['checkBoxHome'];
$checkboxFotoboek= $_POST['checkBoxFotoboek'];
$checkboxDelete = $_POST['checkboxDelete'];
You are trying to get wrong value.
Your check-box name is checkBoxHome and you are trying to get $_POST['checkboxHome'] instead of $_POST['checkBoxHome'] .
Try $_POST['checkBoxHome'] and print it as print_r('checkBoxHome')
Same mistake in checkBoxFotoboek check-box.
try this
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$caption = $_POST['caption'];
$checkboxHome = $_POST['checkBoxHome'];
$checkboxFotoboek= $_POST['checkBoxFotoboek'];
$checkboxDelete = $_POST['checkboxDelete'];
for($i=0;$i<$count;$i++){
$result1=mysqli_query($conn, "UPDATE gallery SET title='$title[$i]', caption='$caption[$i]', home='$checkboxHome[$i]', fotoboek='$checkboxFotoboek[$i]', image_deleted='$checkboxDelete[$i]' WHERE id='$id[$i]'");
header("location:/admin/foto-admin.php");
}
}
?>
I have a php application that fetches the requests from mysql database and displays them for further approval. The form is fetched from send_req.php and is displayed inside the div on showrequests.php. This is the code for send_req.php
<table style="border:0;border-color:transparent">
<tr style="background-color:lightblue">
<td>Product ID</td>
<td>Name</td>
<td>Quantity</td>
<td><input type="checkbox" name="selectAll" /></td>
<td>Authorized Quantity</td>
</tr>
<form method="post" action="send_req.php">
<?php
$reqNum = $_POST['rId'];
echo "<h3>Request # $reqNum</h3>";
$showReqs = mysql_query("Select * from request where request_number='".$reqNum."' and status=0");
while($resultA = mysql_fetch_array($showReqs))
{
$rBy = $resultA['requested_by'];
$rTime = $resultA['request_time'];
$rId = $resultA['id'];
$pId = $resultA['product_id'];
$getPrName = mysql_query("select name from products where id='$pId'");
$prN = mysql_fetch_array($getPrName);
$prName = $prN['name'];
$rQuantity = $resultA['requested_quantity'];
$status = $resultA['status'];
?>
<tr>
<input type="hidden" name="rId[]" value="<?php echo $rId; ?>"/>
<td style="background-color:orange"><input type="text" name="prId[]" value="<?php echo $pId; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"><input type="text" name="prName[]" value="<?php echo $prName; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"><input type="text" name="quantity[]" value="<?php echo $rQuantity; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"></td>
<td><input type="text" name="pQuantity[]" /></td>
</tr>
<?php }
?>
<tr>
<td></td>
<td></td>
<td></td>
<input type="hidden" name="rNum" value="<?php echo $reqNum; ?>" />
<td></td>
<td><input type="submit" name="submitReq" value="Send" id="submit_req" style="backgroundColor:Transparent;border:0;color:blue;width:100;"/></td>
</tr>
</form>
</table>
<?php
echo "Requested By:$rBy at ".substr($rTime,11,18)." ".substr($rTime,0,10);
?>
This is the showrequests.php page
<html>
<head>
<script type="text/javascript">
function getRequest(ob)
{
var id = ob.id;
if(window.XMLHttpRequest)
{
ajaxOb = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
ajaxOb = new ActiveXObject("Microsoft.XMLHTTP");
}
ajaxOb.open("POST", "send_req.php");
ajaxOb.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxOb.send("rId=" + id);
ajaxOb.onreadystatechange = function()
{
if(ajaxOb.readyState == 4)
{
if(ajaxOb.status == 200)
{
document.getElementById("showTable").innerHTML = ajaxOb.responseText;
}
}
}
}
</script>
</head>
<body>
<?php
$mysql_con = mysql_connect("localhost","root","") or die("Could not connect ".mysql_error());
$mysql_db = mysql_select_db("cart",$mysql_con) or die("Unable to select db ".mysql_error());
echo "<h2 align='center'>Pending Requests</h2>";
$showReq = mysql_query("Select distinct(request_number) as rNums from request where status=0");
?>
<div style="float:left;margin-right:15px;">
<br/>
<?php
while($result = mysql_fetch_array($showReq))
{
$rNum = $result['rNums'];
?>
<input type="button" name="fetchReq" id="<?php echo $rNum; ?>" value="<?php echo "Request # $rNum"; ?>" style="margin-bottom:5px;backgroundColor:Transparent;border:0;color:blue;width:100;text-Decoration:underline" onclick="getRequest(this)"/>
<?php
echo "<br/>";
}
?>
</div>
<div id="showTable" style="float: left">
</div>
</body>
</html>
My problem now is that everything works fine in chrome and IE but the form is not submitted when i click the submit button in firefox. I am using firefox 20.0.1. Update: I have removed the html,head and body tags from send_req.php
still not working
form is not allowed inside table. Please see also
Form inside a table
Regards,
Michael
Reminder : the structure of an HTML document is :
<!-- No div before html tag -->
<!DOCTYPE html> <!-- Doctype for HTML5 ; use whatever doctype you need -->
<html>
<head>
</head>
<!-- No div before body tag -->
<body>
<!-- Divs only belongs here -->
</body>
</html>
<!-- No div after html tag -->
If you don't follow this basic structure, you're forcing the browser to interpret your invalid code (+ quirks mode when you don't provide a doctype).
Some browser guess well what you tried to do, others don't, as Firefox might.
Please use a HTML validator as W3's validator to check your syntax.