I'm trying to inserting multiple items connected with quantity and price for each. all of the items will be showing the same invoice number and vendor id.
<form name="order" id="order">
<label>Invoice Number</label>
<input type="text" name="invoicenumber" value="">
<p><label>Vendor's ID</label>
<select name="vendorid" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM customers");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $id; ?> "><?php echo $vendorname; ?></option>
<?php
} ?>
</select> </p>
<table class="table table-bordered" id="dynamic_field">
<!-- Product code begining -->
<td><tr><label>Items sold</label>
<select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select> </td>
<td><label>Quantity</label>
<input type="text" name="tquantity[]" value=""></td>
<td><label>Price</label>
<input type="text" name="saleprice[]" value=""></td>
<td><button name="add" id="add" class="btn btn-success">ADD MORE</button></td></tr>
</table>
<!-- Product code END -->
<input type="submit" name="submit" value="submit">
</form>
<!--Jquery code to add more fields -->
<script>
$(document).ready(function(){
var i = 1;
$(#add).click(function(){
i++;
$(#dynamic_field).append('<tr id="row'+i+'"><td>
<select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select> </td>
<td><label>Quantity</label>
<input type="text" name="tquantity[]" value=""></td>
<td><label>Price</label>
<input type="text" name="saleprice[]" value=""></td>
<td><button name="remove" id="remove" class="btn btn-danger btn_remove">Remove</button></td></tr>');
});
$(document).on('click','.btn_remove',funcion(){
var button_id= $(this).attr("id");
$("#row"+button_id+"").remove();
});
$(#submit).click(function(){
$.ajax({
url:"processinvoice.php",
method:"POST",
data:$('#order').serialize(),
success:function(data){
alert(data);
$('#order')[0].reset();
}
});
});
}
</script>
So I'm trying to add input fields dynamically using jQuery ajax following a tutorial but i can't seem to make it work
When I click add to add fields only the link changes to /admin/addinvoice.php?invoicenumber=&vendorid=+2+&proid[]=+10+&tquantity[]=&saleprice[]=&add=
and it's not adding a field
Related
I have a HTML/PHP form that needs validation of each of the entered fields before updating MySQL database. It would be nice if this cold be done as each field is entered...however, I could use the form's action attribute to run a different validation script but then if there are errors I would need to return to the form and re-populate it with previously entered data for correction - not sure how I would do all this but I guess it's possible. However, I've seen a post here that shows how this can be done with a javascript function for the validation but is this possible to do in PHP? I'm just learning PHP so I'd hate to now have to also learn javascript :( Some guidance appreciated.
<?php
if(isset($_POST)) {
echo __LINE__ . " Form has been posted <br>";
echo __LINE__ . $_POST['state_city'];
}
?>
<form action="" method="post">
<table border="0" align="center">
<tr height="29px" width = "220px">
<td>
<label for="student_id"><b>Student-ID</b></label>
</td>
<td>
<input type="text" placeholder="Student ID" name="studentid" value="<?php echo $row['studentid'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px">
<td>
<label for="student_name"><b>Student name</b></label>
</td>
<td>
<input type="text" placeholder="Student name" name="name" value="<?php echo $row['$studentname'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px"><td><label for="start_date"><b>Start Date</b></label></td>
<td><input type="date" placeholder="Enter start date" name="start_date" value="<?php echo $row['startdate'];;?>" required></td>
</tr>
<tr height="29px"><td width = "220px">
<b>Student Grade</b></td>
<td>
<select name="sgrade" id="sgrade">
<option value="1" <?php if ($row['$sgrade'] == '1') echo "selected"; ?>>Grade 1<option>
<option value="2" <?php if ($row['$sgrade'] == '2') echo "selected"; ?>>Grade 2<option>
<option value="3" <?php if ($row['$sgrade'] == '3') echo "selected"; ?>>Grade 3<option>
<option value="4" <?php if ($row['$sgrade'] == '4') echo "selected"; ?>>Grade 4<option>
</select>
</td>
<tr>
// value?
<tr height="29px"><td width = "220px"><b>Country of Birth</b></td>
<td>
<select name="country" id="country">
<?php
$sql = "SELECT * FROM countries";
$result = mysqli_query($con, $sql);
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
</td>
</tr>
<tr height="29px" width = "220px">
<td><label for="state_city"><b>State and/or main city</b></label></td>
<td><input type="text" placeholder="Student State and/or Main City" name="state_city" value="<?php echo $row['$state_city'];?>" required></td>
</tr>
<tr height="29px" width = "220px">
<td><label for="healthcheck"><b>Checked?</b></label></td>
<td>
<input type="radio" id="checkyes" name="check" value="1" <?php if ($row['check']) echo "checked";?>>
<label for="checkyes"> Yes</label><br>
<input type="radio" id="checkno" name="check" value="0" <?php if (!$row['check']) echo "checked";?>>
<label for="checkno"> No</label><br><br>
</td>
</tr>
</table>
<button type="submit">Save</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="history.back()" value="Go back!" class="cancelbtn">Cancel</button>
</div>
</form>
<script>
function check() {
document.getElementById("checkyes").checked = true;
}
function uncheck() {
document.getElementById(hcheckyes").checked = false;
}
</script>
</body>
</html>
If you want it to be dynamic as the user enters the information you will have to use JavaScript as that would be a front-end solution. With PHP you can do you validation/sanitizing of the data on the server-side as the form is submitted. It is suggested you do both.
For your question of repopulating this is one way
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
For more info : click here
I want to do crud on my page and I have to get content from another table and display it in a select tag. my plan was to make an update page wherein it will get the data from another table to display (in this case the category_name, cat_id was the foreign key at the menu table).
I already tried to join because that's what other forums have said but I think I was wrong.
help would really be appreciated.
this is my model:
function inputMenuToUpdate($menu_id){
$this->db->select('cat_id, category.category_id, category.category_name AS category_name');
$this->db->from('menu');
$this->db->join('category', 'menu.cat_id = category.category_id');
$this->db->where('menu_id',$menu_id);
return $query->row();
}
this is my controller:
public function inputToUpdate($menu_id){
$data['row'] = $this->adminMenuModel->inputMenuToUpdate($menu_id);
$this->load->view('adminEdit', $data);
}
this is my view:
<tbody>
<!-- $result is from the $data in CrudController-->
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->menu_name; ?></td>
<td><?php echo $row->category_name; ?></td>
<td><?php echo $row->price; ?></td>
<td><?php echo $row->description; ?> </td>
<td>
<i class="fa fa-pencil"></i>
</td>
</tr>
<?php } ?>
</tbody>
and this is my update page:
<div class="container" style="width: 40rem;">
<form method="POST" action="<?php echo site_url('adminMenuController/update'); ?> / <?php echo $row->category_id; ?>">
<button type="button" class="btn btn-link"> Go Back </button>
<h1> UPDATE MENU </h1>
<div class="border-bottom"></div>
<br>
<br>
<label for="upMName">New Drink Name</label>
<input type="text" name="mn" class="form-control" id="upMName" value="<?php echo $row->menu_name ?>"></input>
<!-- <label for="cat"> Category </label>
<select id="cat" class="form-control">
<option> <?php echo $row->category_name; ?> </option>
</select> -->
<label for="upPrice">New Price</label>
<input type="number" name="price" class="form-control" id="upPrice" value="<?php echo $row->price ?>"> </input>
<label for="updesc">New Description </label>
<input type="text" name="desc" class="form-control" id="updesc" value="<?php echo $row->description ?>"> </input>
<br>
<button type="submit" class="btn btn-info"> Update </button>
<button type="reset" class="btn btn-danger"> Cancel </button>
</form>
</div>
here i am creating a attendance sheet that insert data into the database
after clicking submit button. The insertion of the data is base on the 2
radio button which is absent/present, the problem is after clicking submit
button the other component works accordingly except for the dropdown button,
the dropdown button insert only a single string instead of the service time
by the way i declare the service time in the database as varchar so i am
confident that it's not the database that causes the error. maybe someone
can help me any help is much appreciated. thank you in advance
enter code here
<?php
include("Sampledb.php");
include("Sample.php");
$flag=0;
if(isset($_POST['submit']))
{
foreach($_POST['attendance_status'] as $id1=>$attendance_status)
{
$name=$_POST['name'][$id1];
$id=$_POST['id'][$id1];
$date=date("Y-m-d H:i:s");
$servicetime=$_POST['servicetime'][$id1];
$result=mysqli_query($objconn,"insert into
attendrecord(id,name,attendance_status,attendance_date,servicetime)
values('$id','$name','$attendance_status','$date','$servicetime')");
if($result)
{
$flag=1;
}
}
header("Location:sampleindex.php");
exit;
}
?>
<div class="panel panel-default">
<div class="panel panel-heading">
<form action="sampleindex.php" method="Post">
<label>Service time:</label>
<select name="servicetime">
<option>00:00</option>
<option value="7:30-9:30">7:30-9:30</option>
<option value="10:30-12:30">10:30-12:30</option>
<option value="1:30-3:30">1:30-3:30</option>
<option value="4:30-6:30">4:30-6:30</option>
</select>
<a class="btn btn-info pull-right" href="view.php">View all<a/>
</div>
</div>
<div id="alert" style="display:none;" class="alert alert-success" ">
×
<strong>Success!</strong>Save
</div>
<h3>
<div class="well text-center">Date:<?php echo date("Y-m-d"); ?> </div>
</h3>
<div class="panel panel-body">
<tr>
<table class="table table-striped">
</tr>
<th>ID Number</th><th>Member Name</th><th>Attendace Status</th>
<?php $result=mysqli_query($objconn,"select* from attend");
$id = 0;
$counter=0;
while($row=mysqli_fetch_array($result))
{
$id++;
?>
<tr>
<td><?php echo $row['id']; ?></td>
<input type="hidden" value="<?php echo $row['id']; ?>"name="id[]">
<td><?php echo $row['name']; ?></td>
<input type="hidden" value="<?php echo $row['name']; ?>"name="name[]">
<td>
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="present">Present
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="absent">Absent
</td>
</tr>
<?php
$counter++;
}
?>
</table>
<input type="submit" name="submit" value="submit" onclick="return mess();">
</div>
<script type="text/javascript">
function mess()
{
if($flag=1){
alert ("Record Save!");
return true;}
else
alert ("Record Not Save!");
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$('#alert').css('display','block');
});
});
</script>
</div>
</div>
Your service time is not an array as it keeps same for all record. So you need to change your PHP code as below:
$servicetime=$_POST['servicetime'];
Hope it helps you.
Instead of $_POST['servicetime'][$id] use $_POST['servicetime']
I am trying to update some table rows (there can be 40-50 rows) generated from a MYSQL query with a Approve/Rejected column at the end of each row, I would like to use jQuery to update the row using a select dropdown, the problem I am having is that it only updates the first row.
I have tried to look at other Stackoverflow threads, there seems to be quite a few with similar issues, but I am still trying to get my head around jQuery so I couldn't work out how to do it.
I am guessing it's because of the same class names?
My HTML table/forms
<table>
<tr>
<td>1</td>
<td>Ren</td>
<td>
<form method="post" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" name="id" value="1">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
<tr>
<td>2</td>
<td>Stimpy</td>
<td>
<form method="post" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" name="id" value="2">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
</table>
jQuery:
$('.save_button').click(function() {
var approval = $('.approval').val();
var id = $('.id').val();
$('.save_status').text('loading...');
$.post('updateRow.php',{
approval: approval,
id: id
}, function(data) {
$('.save_status').text(data);
}
);
});
PHP File:
if(isset($_POST['approval'],$_POST['id'])) {
$approval = $_POST['approval'];
$id = $_POST['id'];
if($approval !="" && $id !="") {
$pdo->ApproveOrDeny($approval, $id);
} else {
echo "The same thing we do every night, Pinky - try to take over the world!";
}
}
Thanks in advance.
You need to access the clicked form values .instead of just using class name
1) Setup unique name for each form
2) Access the value using form name like this $('form[name="form1"]>.id').val()
$('.save_button').click(function() {
var form = $(this).parents('form:first').attr('name');
alert(form);
var id = $('form[name="'+form+'"]>.id').val();
var option =$('form[name="'+form+'"] .approval').val();
alert(id);
alert(option);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>Ren</td>
<td>
<form method="post" name="form1" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" class="id" value="1">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
<tr>
<td>2</td>
<td>Stimpy</td>
<td>
<form method="post" name="form2" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" class="id" value="2">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
</table>
I want to display the selected price, category and size on the dropdown slideingDiv. But what I have done below is not working. I have try to echo out to see if the data have been sent thought but I got nothing. Is anything wrong with my code?
Script
<script type="text/javascript">
$(document).ready(function () {
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function () {
$(".slidingDiv").slideToggle();
});
});
</script>
Button
<button a href="product.php?ProdID=<?php echo $id; ?>" class="show_hide" id="button" name="button">Add to cart</a></button>
PHP
<?php
dbconnect();
if(isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && `isset($_POST['Category'])){`
$pid = $_POST['pid'];
$length = $_POST["length"];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];
echo $pid; // I have add this echo to see data have been passed though but i get nothing in return
echo $length;
$stmt4 = $conn->prepare("
SELECT Product.Name as ProductName, Category.Name, size, Price
FROM item_Product, Product, Category
WHERE Product.ProdID =:pid
AND size= :length AND Category.Name = :Category Limit 1");
$stmt4->bindParam('pid',$pid);
$stmt4->bindParam('length',$length);
$stmt4->bindParam('Category',$Category);
$stmt4->execute();
foreach ($stmt4->fetchAll(PDO::FETCH_ASSOC) as $row4 ) {
$product_name = $row4["ProductName"];
$price = $row4["Price"];
$length = $row4["size"];
$Category = $row4["Name"];
?>
Item was added shopping bag </br>
Name:<?php echo $row4['ProductName']; ?> </br>
Length:<?php echo $row4['size']; ?> </br>
Category:<?php echo $row4['Name']; ?> </br>
Price:<?php echo $row4['Price']; ?> </br>
<?php } }
?>
View Cart
</div>
TABLE
<td width="160">Price:</td>
echo '<td name="pricetag" id="pricetag">'.$row2['Price'].'</td>';
</tr>
<tr>
<td>Category</td>
<td>
<select id="Category" name="Category">
echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
</select>
</td>
</tr>
<tr>
<td width="160">Length:</td>
<td>
<select name="length" id="length">
<option SELECTED value="'.$row3['size'].'">'.$row3['size'].</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<div class="button">
<button class="show_hide" id="button" name="button">Add to cart</button>
</div>
Try this: SEE MY FIDDLE
HTML
<table>
<tr>
<td>Price</td>
<td>
<select id='priceTag'>
<option value='p1'>price 1</option>
<option value='p2'>price 2</option>
</select>
</td>
</tr>
<tr>
<td>Category</td>
<td>
<select id='Category'>
<option value='c1'>cat 1</option>
<option value='c2'>cat 2</option>
</select>
</td>
</tr>
<tr>
<td>Size</td>
<td>
<select id='Size'>
<option value='s1'>size 1</option>
<option value='s2'>size 2</option>
</select>
</td>
</tr>
</table>
<button id='button'>Submit</button>
<div id='selected'>
<table cellpadding="5" border='1'>
<tr>
<td>Price:</td>
<td id='sprice'></td>
</tr>
<tr>
<td>Category:</td>
<td id='scat'></td>
</tr>
<tr>
<td>Size:</td>
<td id='ssize'></td>
</tr>
</table>
</div>
JQuery
$(document).ready(function()
{
$('#selected').hide();
$('#button').click(function()
{
var price = $('#priceTag').val();
var cat = $('#Category').val();
var size = $('#Size').val();
$('#sprice').text(price);
$('#scat').text(cat);
$('#ssize').text(size);
$('#selected').slideDown();
});
});
Seems that you do not have a space in your <?php} ?>
Do something like <?php } ?>
Error:
<?php} ?>
try this,
<?php } ?> // add space between php and }
<?php}
should be
<?php }
Also
<button a href="product.php?ProdID=<?php echo $id; ?>" class="show_hide" id="button" name="button">Add to cart</a></button> is in correct
should be
<button>Add to cart</button>