I am unable to pass the form id variable to javascript
where the date input = mm/dd/yyyy with selection of dropdown text A and text B should populate the JS output
$('#Option').change(function(){
if ($(this).val()== 'A') {
$('#text1').val('3');
$('#text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +1 Weekdays')); ?>");
} else if ($(this).val()== 'B') {
$('#text1').val('8');
$('#text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +8 Weekdays')); ?>");
}
});
<div class="col-xs-3">
<label class="control-label" placeholder="mm/dd/yyyy">Date</label>
<input type="text" class="form-control" id="Date" name="Date" />
</div>
<div class="col-xs-3">
<label class="control-label">Option</label>
<select class="form-control" id="Option" name="Option">
<option value="">Select</option>
<option value="A" >A</option>
<option value="B" >B</option>
</select>
</div>
<div class="col-xs-3">
<label class="control-label">Text1</label>
<input type="text" class="form-control" id="Text1" name="Text1" />
</div>
<div class="col-xs-3">
<label class="control-label">Text2</label>
<input type="text" class="form-control" id="Text2" name="Text2" />
</div>
https://jsfiddle.net/hufuso0d/3/
The ids are Text1 and Text2. Try as below :
<script>
$('#Option1').change(function(){
if ($(this).val()== 'A') {
$('#Text1').val('3');
$('#Text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +1 Weekdays')); ?>");
} else if ($(this).val()== 'B') {
$('#Text1').val('8');
$('#Text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +8 Weekdays')); ?>");
}
});
</script>
It works!
Pay attention to the correct id: you should insert
$('#Text1').val('3');
instead of
$('#text1').val('3');
Related
what wrong in this code? I get undefined function error for javascript.
1-
Uncaught ReferenceError: add_row_weekly is not defined
2-
Uncaught ReferenceError: add_link_menu is not defined
3- and some other like that.
I searched in many websites but can't solve. I am not a developer
Thanks
<div class="fields fields3">
<label class="title">کلاس آیکن:</label>
<input dir="ltr" type="text" id="menu_link_icon[]" name="menu_link_icon[]" value="
<?php echo $menu['menu_link_icon'][$i];
?>" placeholder="
<?php bloginfo('url');
?>">
</div>
<span class="close" onclick="$(this).parent().slideUp(function(){$(this).remove();
});
"><i class="icon-remove"></i></span>
</div>
<?php }
?>
</div>
<button type="button" class="add_link_menu" onclick="add_link_menu();
">افزودن لینک</button>
<script>
function add_link_menu(){ var rand_num = Math.floor((Math.random() * 1000000000) + 1000);
$("#add_link").append('<div class="item"><div class="fields fields3"><div class="title">عنوان لینک :</div><input type="text" name="menu_link_name[]" placeholder="باکس آفیس"></div><div class="fields fields3"><label class="title">آدرس لینک :</label><input dir="ltr" type="text" id="menu_link_url[]" name="menu_link_url[]" placeholder="
<?php bloginfo('url');
?>"></div><div class="fields fields3"><label class="title">عنوان انگلیسی :</label><input dir="ltr" type="text" id="menu_link_name_en[]" name="menu_link_name_en[]" placeholder="مثال : Menu Link Name"></div><div class="fields fields3"><label class="title">کلاس آیکن :</label><input dir="ltr" type="text" id="menu_link_icon[]" name="menu_link_icon[]" placeholder="مثال : icon-Movie"></div><span class="close" onclick="$(this).parent().slideUp(function(){$(this).remove();
});
"><i class="icon-remove"></i></span></div>');
$('div[id=hide]').slideDown(function(){ $(this).removeAttr('id');
$(this).removeAttr('style');
});
}
</script>
</div><!-- content -->
</div><!-- i_sections web_section -->
<div class="i_sections jadvalpakhsh_section" id="jadvalpakhsh" style="display:none">
<div class="content">
<div class="fields">
<label class="title" for="active_weekly_table">نمایش و فعال سازی جدول پخش هفتگی :</label>
<label class="switch">
<input type="checkbox" value="on" id="active_weekly_table" name="active_weekly_table"
<?php echo ds_option('active_weekly_table') == 'on' ? "checked":"";
?> onclick="validate()">
<div class="slider round"></div>
</label>
</div>
<div id="add_row">
<?php $week = ds_option('weekly_table');
for($i = 0;
$i < count($week['week_day']);
$i++){
?>
<div class="items">
<div class="fields fields4">
<div class="title">در روز :</div>
<select id="week_day[]" name="week_day[]">
<option value="0"
<?php if ( $week['week_day'][$i] == '0' ) echo 'selected="selected"';
?>>شنبه</option>
<option value="1"
<?php if ( $week['week_day'][$i] == '1' ) echo 'selected="selected"';
?>>یکشنبه</option>
<option value="2"
<?php if ( $week['week_day'][$i] == '2' ) echo 'selected="selected"';
?>>دوشنبه</option>
<option value="3"
<?php if ( $week['week_day'][$i] == '3' ) echo 'selected="selected"';
?>>سشنبه</option>
<option value="4"
<?php if ( $week['week_day'][$i] == '4' ) echo 'selected="selected"';
?>>چهارشنبه</option>
<option value="5"
<?php if ( $week['week_day'][$i] == '5' ) echo 'selected="selected"';
?>>پنجشنبه</option>
<option value="6"
<?php if ( $week['week_day'][$i] == '6' ) echo 'selected="selected"';
?>>جمعه</option>
</select>
</div>
<div class="fields fields4">
<div class="title">عنوان سریال :</div>
<input type="text" name="name_series[]" value="
<?php echo $week['name_series'][$i];
?>" placeholder="نام سریال">
</div>
<?php }
?>
</div>
<button type="button" class="add_row_weekly" onclick="add_row_weekly();
">افزودن زمان پخش سریال جدید</button>
<script>
function add_row_weekly(){ var rand_num = Math.floor((Math.random() * 1000000000) + 1000);
$("#add_row").append('<div class="items"><div class="fields fields4"> <div class="title">در روز :</div><select id="week_day[]" name="week_day[]"><option value="0"
<?php if ( $week['week_day'][$i] == '0' ) echo 'selected="selected"';
?>>شنبه</option><option value="1"
<?php if ( $week['week_day'][$i] == '1' ) echo 'selected="selected"';
?>>یکشنبه</option><option value="2"
<?php if ( $week['week_day'][$i] == '2' ) echo 'selected="selected"';
?>>دوشنبه</option><option value="3"
<?php if ( $week['week_day'][$i] == '3' ) echo 'selected="selected"';
?>>سشنبه</option><option value="4"
<?php if ( $week['week_day'][$i] == '4' ) echo 'selected="selected"';
?>>چهارشنبه</option><option value="5"
<?php if ( $week['week_day'][$i] == '5' ) echo 'selected="selected"';
?>>پنجشنبه</option><option value="6"
<?php if ( $week['week_day'][$i] == '6' ) echo 'selected="selected"';
?>>جمعه</option></select></div><div class="fields fields4"><div class="title">عنوان سریال :</div><input type="text" name="name_series[]" placeholder="نام سریال"></div><div class="fields fields4"><label class="title">آدرس سریال :</label><input dir="ltr" type="text" id="url_series[]" name="url_series[]" placeholder="
<?php bloginfo('url');
?>"></div><div class="fields fields4"><label class="title">شبکه پخش کننده :</label><input dir="ltr" type="text" id="network_series[]" name="network_series[]" placeholder="مثال : HBO"></div><div class="fields fields4"><label class="title">قسمت :</label><input dir="ltr" type="text" id="part_series[]" name="part_series[]" placeholder="مثال : اول"></div><div class="fields fields4"><label class="title">فصل :</label><input dir="ltr" type="text" id="season_series[]" name="season_series[]" placeholder="مثال : دوم"></div><div class="fields fields4"><div class="des">لینک یک تصویر ۲۵۰px × ۴۵px قرار دهید</div><label class="title">لینک کاور :</label><input dir="ltr" type="text" id="cover_series[]" name="cover_series[]" placeholder="
<?php bloginfo('url');
?>/cover_series.png"></div><span class="close" onclick="$(this).parent().slideUp(function(){$(this).remove();
});
"><i class="icon-remove"></i></span></div>');
$('div[id=hide]').slideDown(function(){ $(this).removeAttr('id');
$(this).removeAttr('style');
});
}
</script>
</div>
</div>
The errors tell you that the functions your code tries to use do not exist at the point when they are attempted to be invoked. Let me show you an illustrative example of what your problem is:
Incorrect
foo();
function foo() {}
The reason this is incorrect is that it tries to call foo before it has been created.
Correct
function foo() {}
foo();
This is correct because foo has been called when it already exists.
Your functions
Are called before they are defined. I advise you to move your script tags into the head tag to fix this. If you need more help, then you need to point out the exact lines where the issue happens and show your generated markup taken from browser page source, because we do not have your project and your PHP code will not work for us, unless you provide extra information about your project's PHP side. The error tells you where it happens in the console, you need to find the exact location of the error and point it out to us.
EDIT
Fixed code:
<div id="add_link_menu">
</div>
<div class="content">
<button type="button" class="add_link_menu" onclick="add_link_menu();">Add link button</button>
<script>
function add_link_menu() {
var rand_num = Math.floor((Math.random() * 1000000000) + 1000);
$("#add_link_menu").append(`
<div class="item">
<div class="fields fields3">
<div class="title">عنوان لینک :</div>
<input type="text" name="menu_link_name[]" placeholder="باکس آفیس"></div>
<div class="fields fields3">
<label class="title">آدرس لینک :</label>
<input dir="ltr" type="text" id="menu_link_url[]" name="menu_link_url[]" placeholder="someurl">
</div>
<div class="fields fields3">
<label class="title">عنوان انگلیسی :</label>
<input dir="ltr" type="text" id="menu_link_name_en[]" name="menu_link_name_en[]" placeholder="مثال : Menu Link Name">
</div>
<div class="fields fields3">
<label class="title">کلاس آیکن :</label>
<input dir="ltr" type="text" id="menu_link_icon[]" name="menu_link_icon[]" placeholder="مثال : icon-Movie">
</div>
<span class="close" onclick="$(this).parent().slideUp(function(){$(this).remove();});">
<i class="icon-remove"></i>
</span>
</div>
</div>`);
$('div[id=hide]').slideDown(function(){
$(this).removeAttr('id');
$(this).removeAttr('style');
});
}
</script>
</div>
Fiddle: http://jsfiddle.net/z7jhkxo8/
Am not very expert in jquery and stepformwizard. Am working on code written by someone else. The issue is the multistepformwizard fieldset is used and the next step should be loaded only when Next button is clicked. But currently both of the steps are visible on the screen with extra horizontal scrollbar. I dont want the step 2 form to be visible. Here is the html and jquery code.
<div class="ztab-sec ztab-desbrd bksrvsec bksecrv2">
<div class="row">
<div class="col-md-12">
<form action="<?php echo base_url()?>service/service_request" method="POST" id="wizard_example_6" autocomplete="off">
<input type="hidden" value="<?php echo $ownerArr[0]['email_id']; ?>" name="email_id">
<input type="hidden" value="<?php echo $ownerArr[0]['phone']; ?>" name="mobile">
<fieldset>
<legend>Basic information about your car</legend>
<div class="row bkmargin">
<div class="col-lg-4 col-sm-4">
<div class="form-group">
<label>Vehicle Brand</label>
<select id="u_car_id" class="form-control" name="car_id" required>
<option selected disabled value="">Select a Car</option>
<?php foreach ($cars as $car_details) : ?>
<option value="<?php echo $car_details['user_car_id']; ?>"><?php echo $car_details['makesTitle']; ?> (<?php echo $car_details['modelsTitle']; ?>) <?php echo $car_details['car_reg_no']; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="col-lg-4 col-sm-4">
<div class="form-group">
<label>Variants</label>
<input type="text" class="form-control" name="variant" placeholder="Variants" required data-parsley-group="block0" id="variant" readonly pattern="^[a-zA-Z\s]*$">
</div>
</div>
<div class="col-lg-4 col-sm-4">
<div class="form-group">
<label>Transmission</label>
<input type="text" class="form-control" placeholder="Transmission" name="transmission" readonly required data-parsley-group="block0" id="transmission" pattern="^[a-zA-Z\s]*$">
</div>
</div>
</div>
<legend class="clearfix">Select service for your car? <a href="#" class="car-link pull-right" data-toggle="modal" data-target="#myModal3" >Add New Car</a></legend>
<div class="row bkmargin">
<div class="col-md-4 col-sm-4">
<div class="form-group besrvx">
<input id="option-one" name="service_type" value="1" class="styled-radio " type="radio" data-parsley-group="block0" required data-required-message="Please check one service">
<label class="srvlabel" id="1" for="option-one"><strong>Basic Service: </strong>
<span class="srvspa">3 months or 5000 kms (whichever is earlier)</span>
</label>
</div>
</div>
</fieldset>
<fieldset>
<legend>Vehicle picked up address </legend>
<div class="row bkmargin">
<div class="col-lg-4 col-sm-4">
<div class="form-group">
<label>Select Address</label>
<select class="form-control" id="user_add_id" name="pick_id">
<option selected disabled>Select Address</option>
<?php $i=1; foreach ($address as $user_details) : ?>
<option value="<?php echo $user_details['add_id'] ?>"> Address <?php echo $i; ?></option>
<?php $i++; endforeach; ?>
<option id="neww" class="neww" value="neww">Select new Address</option>
</select>
</div>
</div>
<div class="col-lg-4 col-sm-4">
<div class="form-group">
<label>Address Type</label>
<select class="form-control" id="add_type" name="add_type" disabled>
<option value="" selected disabled>Select Address Type</option>
<option value="Home">Home Address</option>
<option value="Office">Office Address</option>
<option value="Other">Other Address</option>
</select>
</div>
<div class="row bkmargin">
<noscript>
<input class="nocsript-finish-btn sf-right nocsript-sf-btn" type="submit" value="submit"/>
</noscript>
</div>
</fieldset>
</form>
I have skipped some part as its a long form. I want the second fieldset should only be visible when user clicks on next button
here is the jquery
w6 = $("#wizard_example_6").stepFormWizard({
onNext: function(b, a) {
return $("#wizard_example_6").parsley().validate("block" + b)
},
onFinish: function(b) {
return $("#wizard_example_6").parsley().validate()
}
});
var d = window.location.hash.match(/^#step-(\d+)/),
c = 0;
null !== d && (c = d[1] - 1);
w7 = $("#wizard_example_7").stepFormWizard({
startStep: c,
onNext: function(b, a) {
window.location.hash = "#step-" + (b + 2)
},
onPrev: function(b, a) {
window.location.hash = "#step-" + b
},
onFinish: function(b) {
window.location.hash = "#form-sended"
}
})
};
$(document).ready(function() {
prepare_example();
$("pre code").each(function(c, b) {
hljs.highlightBlock(b)
});
var d = $(location).attr("search").match(/t=([a-z]+)/);
"undefined" != typeof d && null != d ? ($(".sf-wizard").parent().removeClass("sf-sea").addClass("sf-" + d[1]), $(".bt-" + d[1]).removeClass("btn-default").addClass("btn-info")) : $(".bt-sea").removeClass("btn-default").addClass("btn-info")
});
Please help me with this. I have tried using but no luck also changed css display:none inplace of block but couldnt resolve the issue.
My main problem is that my modal is showing but alert is showing [object Object]. I have four tables like stud,country_master_academic, master_city and master_state.When i click on edit, modal appears but data fetched from database is not showing in it.
jQuery in home.php page
$(document).ready(function(){
$(document).on('click', '.edit_data', function(event){
var stud_no = $(this).attr("id");
$.ajax({
url:"update.php",
method:"POST",
data:{stud_no:stud_no},
dataType:"json",
success:function(data){
console.log(data);
$('#name').val(data.name);
$('#mob_no').val(data.mob_no);
$('#dob').val(data.dob);
$('#add').val(data.add);
$('#photo').val(data.photo);
$('#gender').val(data.gender);
$('#country').val(data.country);
$('#state').val(data.state);
$('#city').val(data.city);
$('#stud_no').val(data.stud_no);
$('#update_data_modal').modal('show');
},
});
});
});
Modal for update in home.php page
<div class="container">
<div class="modal fade" id="update_data_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-heading" style="margin-top:30px;text-align:center">
<button class="close" data-dismiss="modal" style="margin-right:20px;font-weight:bold;">x</button>
<h4 class="modal-title"><span class="glyphicon glyphicon-edit"></span>Update Student</h4>
</div>
<div class="modal-body">
<?php
$img = "images/".trim($vrow["photo"]);
echo '<img src='.$img.' class="image" style="margin-left:75%;margin-top:5%;width:120px;height:120px;border:2px solid #bbbbbb;border-radius:10px;">';
?>
<br/>
<input type="file" name="photo" style="margin-left:70%;">
<div class="form-group">
<form class="form-horizontal" name="form" id="form" method="post" action="<?php $_PHP_SELF?>" enctype="multipart/form-data">
<label for="name" id="name"><span class="glyphicon glyphicon-user"></span><b> Student Name: </b></label>
<input type="text" class="form-control" name="name" id="name" pattern="[a-zA-Z]{3,}" title="Name should only contain letters and atleast 3 letters" required />
</div>
<div class="form-group">
<label for="no"><span class="glyphicon glyphicon-phone"></span><b> Mobile No: </b></label>
<input type="text" class="form-control" name="mob_no" id="mob_no" pattern="[0-9]{10}" title="Mobile number should be of 10 digits" required />
</div>
<div class="form-group">
<label for="dob"><span class="glyphicon glyphicon-calendar"></span><b> Birth Date: </b></label>
<input type="date" class="form-control" name="dob" id="dob" required />
</div>
<div class="form-group">
<label for="add"><span class="glyphicon glyphicon-map-marker"></span><b> Address: </b></label>
<textarea rows="4" cols="33" class="form-control" name="add" id="add" required></textarea>
</div>
<div class="form-group">
<label for="photo"><span class="glyphicon glyphicon-camera"></span><b> Photo: </b></label>
<input type="file" name="photo" id="photo" required />
</div>
<div class="form-group">
<label for="gen"><b> Gender: </b></label>
<input type="radio" name="gender" id="gender" value="M" required="required">Male
<input type="radio" name="gender" id="gender" value="F" required="required">Female
</div>
<div class="form-group">
<label for="cntry"><span class="glyphicon glyphicon-map-marker"></span><b> Country: </b></label>
<select name="country" id="country" class="form-control">
<option value="0">Select</option>
<?php
$country="SELECT * from country_master_academic";
$res= $conn->query($country);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["country_name"]==$vcountry or $vrow['country'] == $row["country_code"] )
{
echo '<option value='.$row["country_code"].' selected>'.$row["country_name"].'</option>';
}
else
{
echo '<option value='.$row["country_code"].'>'.$row["country_name"].'</option>';
}
}
}
?>
</select>
</div>
<div class="form-group">
<label for="state"><span class="glyphicon glyphicon-map-marker"></span><b> State: </b></label>
<select name="state" id="state" class="form-control">
<option value="0">Select</option>
<?php
$state="SELECT * from master_state";
$res= $conn->query($state);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["state_name"]==$vstate or $vrow['state'] == $row["state_code"] )
{
echo '<option value='.$row["state_code"].' selected>'.$row["state_name"].'</option>';
}
else
{
echo '<option value='.$row["state_code"].'>'.$row["state_name"].'</option>';
}
}
}
?>
</select>
</div>
<div class="form-group">
<label for="city"><span class="glyphicon glyphicon-map-marker"></span><b> City: </b></label>
<select name="city" id="city" class="form-control">
<option value="0">Select</option>
<?php
$city="SELECT * from master_city";
$res= $conn->query($city);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["city_name"]==$vcity or $vrow['city'] == $row["city_code"] )
{
echo '<option value='.$row["city_code"].' selected>'.$row["city_name"].'</option>';
}
else
{
echo '<option value='.$row["city_code"].'>'.$row["city_name"].'</option>';
}
}
}
?>
</select>
</div>
<div class="form-group">
<input type="hidden" name="stud_no" id="stud_no" />
<button type="submit" name="update" id="update" class="btn btn-info">Update</button>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-danger" type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
update.php page but still i am trying to fetch data, so only wrote code for selecting records from database.
My edit button which is kept in a loop
echo '<td><button name="edit" style="font-weight:bold;" type="submit" id='.$row["stud_no"].' class="btn btn-warning edit_data" data-target="#update_data_modal" data-toggle="modal"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>';
Change in your html as below.
<div class="form-group">
<label for="gen"><b> Gender: </b></label>
<input type="radio" name="gender" id="genderMale" value="M" required="required">Male
<input type="radio" name="gender" id="genderFemale" value="F" required="required">Female
</div>
Add this code in your ajax success.
if(data[6] == 'M')
{
$("#genderMale").prop("checked", true);
} else if(data[6] == 'F') {
$("#genderFemale").prop("checked", true);
}
For image file you can use as below in your html
<div class="form-group">
<img id="resultedPhoto">
<label for="photo"><span class="glyphicon glyphicon-camera"></span><b> Photo: </b></label>
<input type="file" name="photo" id="photo" required />
</div>
Add below code in ajax success
$("#resultedPhoto").attr("src","/path-to-your-folder/" + data[5]);
As shown in the snapshot, your object has numeric indexes hence data.photo will not return anything as there is no index photo in your object.
You can access image name by using data[5].
Similarly you can get any index value.
Like for name instead of using data.name you need to use data[1]
So your code would be something like this:
$('#name').val(data[1]);
$('#mob_no').val(data[2]);
$('#dob').val(data[3]);
$('#add').val(data[4]);
$('#photo').val(data[5]);
Also you have given same ids for labels and inputs:
<label for="name" id="name">
You need to remove id from all labels:
<label for="name">
Now when i click on edit Modal is not opening and database value is getting empty
another jquery
$(document).ready(function(){
$("#form1").submit(function(event){
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url:"upresult.php",
type:"POST",
data:{formData:formData},
async:false,
success:function(data) {
alert(data);
location.reload();
},
cache:false,
contentType:false,
processData:false
});
});
});
upresult.php
<?php
include("connection.php");
if(!empty($_POST)){
$no=$_POST['stud_no'];
$name=trim($_POST['name']);
$mob=trim($_POST['mob_no']);
$dob=trim($_POST['dob']);
$add=trim($_POST['add']);
$photo=trim($_FILES['photo']['name']);
$gen=trim($_POST['gender']);
$cn=trim($_POST['country']);
$st=trim($_POST['state']);
$ct=trim($_POST['city']);
$qry="update stud set stud_name='$name',mobile='$mob',dob='$dob',address='$add',gender='$gen',country='$cn',state='$st',city='$ct' where stud_no='$no'";
$data=mysqli_query($conn,$qry);
if($data)
{
echo '<script language="javascript">';
echo 'alert("Updated Successfully")';
echo '</script>';
}
else {
echo '<script language="javascript">';
echo 'alert("Cannot update record")';
echo '</script>';
}
if(!empty($_FILES['photo']['name'])){
$qry1= "update stud set photo='$photo' where stud_no='$no'";
$data1=mysqli_query($conn,$qry1);
if($data1){
$target_dir="images/";
$target_file=$target_dir.basename($_FILES["photo"]["name"]);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["photo"]["tmp_name"],$target_file)){
echo '<script language="javascript">';
echo 'alert("Image upload successfully")';
echo '</script>';
} else {
echo '<script language="javascript">';
echo 'alert("Cannot Upload")';
echo '</script>';
}
}
}
}
Here's the complete form:
$(document).ready(function() {
$( "#datepicker" ).datepicker();
});
<div class="labelContainer">
<label for="Date">Date</label>
</div>
<div class="labelContainer">
<label for="Category">Category</label>
</div>
<div class="inputContainer">
<select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Category</option>
<?php
$sth = $conn->prepare('Select name From category');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
}
?>
</select></div><br>
<br>
<div class="labelContainer">
<label for="Item">Item</label>
</div>
<div class="inputContainer">
<select name="item_name" id="item_name" value="item_name" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Item</option>
<?php
$sth = $conn->prepare('Select item_name From item');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['item_name']!="")
echo " <option id=\"ItemName\" name=\"ItemName\" value=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></div><br>
<div class="labelContainer">
<label for="Vendor">Vendor</label>
</div>
<div class="inputContainer">
<select name="vendor_name" id="vendor_name" value="vendor_name" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Vendor</option>
<?php
$sth = $conn->prepare('Select name From vendor');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo " <option id=\"VendorName\" name=\"VendorName\" value=".$row['name'].">".$row['name']."</option>";
}
?>
</select></div><br>
<div class="labelContainer">
<label for="Unit">Unit</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Unit" id="Unit" name="Unit" ><br>
</div>
<div class="labelContainer">
<label for="Price_per_Unit">Price_per_Unit</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Price per Unit" name="Price_per_Unit" id="Price_per_Unit"><br>
</div>
<div class="labelContainer">
<label for="Quantity">Quantity</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Quantity" name="Quantity" id="Quantity"><br>
</div>
<div class="labelContainer">
<label for="VAT">VAT</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="VAT" name="VAT" id="VAT"><br>
</div>
<div class="labelContainer">
<label for="Freight_charges">Freight_charges</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Freight charges" id="Freight_charges" name="Freight_charges"><br>
</div>
<div class="labelContainer">
<label for="Other_Charges">Other_Charges</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Other Charges" id="Other_Charges" name="Other_Charges"><br>
</div>
<div class="labelContainer">
<label for="Total">Total</label>
</div>
<div class="inputContainer">
<input type="text" placeholder="Total" id="Total" name="Total"><br>
</div>
<input type="submit" name="submit" value="Update" style="width:150px;padding:10px;">
<?php
$date="";
$category_value="";
$item_value="";
$vendor_value="";
$unit_value="";
$price_rate_value=0;
$quantity_value=0;
$vat_value=0;
$freight_value=0;
$other_charges_value=0;
$total_value=0;
if(!empty($_POST['category']))
{
$category_value=$_POST['category'];
}
if(!empty($_POST['item_name']))
{
$item_value=$_POST['item_name'];
}
if(!empty($_POST['vendor_name']))
{
$vendor_value=$_POST['vendor_name'];
}
if(!empty($_POST['Unit']))
{
$unit_value=$_POST['Unit'];
}
if(!empty($_POST['Price_per_Unit']))
{
$price_rate_value=$_POST['Price_per_Unit'];
}
if(!empty($_POST['Quantity']))
{
$quantity_value=$_POST['Quantity'];
}
if(!empty($_POST['Freight_charges']))
{
$freight_value=$_POST['Freight_charges'];
}
if(!empty($_POST['Other_charges']))
{
$other_charges_value=$_POST['Other_charges'];
}
if(!empty($_POST['VAT']))
{
$vat_value=$_POST['VAT'];
}
$total_value= ($price_rate_value * $quantity_value)+$freight_value+$other_charges_value+$vat_value;
$sql='INSERT INTO bill(date, category, item, vendor, unit, price_per_unit, quantity, vat, freight_charges, other_charges, total) '
. 'values (:date,:category, :item,:vendor, :unit, :price_per_unit, :quantity, :vat, :freight_charges, :other_charges, :total)';
$sth=$conn->prepare($sql);
$sth->execute(array(':date'=>$date,':category'=>$category_value,':item'=>$item_value, ':vendor'=>$vendor_value,
':unit'=>$unit_value,':price_per_unit'=>$price_rate_value,':quantity'=>$quantity_value,':vat'=>$vat_value,
':freight_charges'=>$freight_value,':other_charges'=>$other_charges_value,':total'=>$total_value));
?>
</form>
How can I add the date? Have already tried what the answers currently say. Is it because there's no submit associated with date? Could it be because of that?
MySQL accept that value in its dateformat syntax.
The date format in MySQL is YYYY-MM-DD
Try this
if(!empty($_POST['datepicker']))
{
$date = date("Y-m-d",strtotime($_POST['datepicker'])); //mysql date format
}
Rewrite your script code like this and try:
<script>
$(document).ready(function() {
$( "#datepicker" ).datepicker();
});
$("#datepicker").datepicker('setDate', new Date());
</script>
Before saving date to database change its date format to date format of mysql database field:
if(!empty($_POST['datepicker']))
{
$date = date("Y-m-d",strtotime($_POST['datepicker'])); // change date format like this
}
$sth->execute(array(':date'=>$date, ':category'=>$category, ....));
and now save this date to database. because you date picker date format may be different like d-m-y, d/m/y etc... so you need to change this date format to database date format then only it will be sucessfully saved to database.
Okay, it is working now. The problem was that I had written the code for calendar outside the form. Have placed it inside and now it works.
I have a page *book_order*,which is used to add orders into a table *order_management*, where order_id is auto incremented in that table. once after submit this page i want the order_id to be passed to other page *book_order2* to add products under same order_id. For that i have created seperete *order_management2* table in which order_id is not auto-incremented.
My requirement is that i want to pass order_id from book_order page to book_order2 page and that variable is to be remembered till i do keep on adding....if i want to add new order, i will go to book_order page, else i will use book_order2 page.
book_order.php
<div class="grid_4">
<div class="da-panel">
<div class="da-panel-header">
<span class="da-panel-title">
<img src="images/icons/color/wand.png" alt="" />
<font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
</span>
</div>
<div class="da-panel-toolbar top">
<ul>
<li><div class="da-button blue large">View all Orders</div></li>
</ul>
</div>
<div class="da-panel-content">
<?php
if(isset($_POST['submit']))
{
extract($_POST);
$order_date=date("Y-m-d");
$sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
$array=mysql_fetch_array($sql);
$sku = $array[0];
$qis = $array[1];
$sold_quan = $array[2];
$crdate = $array[3];
$sql2=mysql_query("INSERT INTO order_management(order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
VALUES
('$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");
if($sql2)
{
echo "<div class='da-message success'>Successfully Booked Your Order</div>";
?>
<script>
var r = confirm("want to add more products?");
if (r == true)
{
//x="You pressed OK!";
window.location = "main.php?page=book_order2";
}
else
{
//x="You pressed Cancel!";
window.location = "main.php";
}
</script>
<?php
}
else
{
die(mysql_error());
}
$quantity_left = $qis - $quantity;
$sold_quan = $sold_quan + $quantity;
$diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
$expctd_stock=round((7*$quantity_left)/$diff_in_days);
//echo $expctd_stock;
$sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
/*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/
$sql4 =mysql_query("select order_id from order_management where sku='$sku'");
$idarray=mysql_fetch_array($sql4);
$id = $idarray[0];
//$_SESSION['id'] = $id;
}
?>
<form id="da-ex-validate1" class="da-form" method="post" action="">
<div class="da-form-row">
<label>Brand<span class="required">*</span></label>
<div class="da-form-item small">
<!--<input type="text" name="brand" id="brand" class="required" value=""/>-->
<select name="brand" id="brand" onChange="retrievedata(this.value)">
<option value="">--- select brand ---</option>
<?php
$ord=mysql_query("select * from brand_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-form-row">
<label>Product<span class="required">*</span></label>
<div class="da-form-item small">
<select name="prod" id="prod" onChange="retrievequantity(this.value)">
<option value="">--- select product ---</option>
</select>
</div>
</div>
<div class="da-form-row">
<label>Customer name<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="customername" id="customername" class="required char" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Phone Number<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="phonenumber" id="phonenumber" class="required number" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Email<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="email" id="email" class="required email" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Address<span class="required">*</span></label>
<div class="da-form-item small">
<textarea name="address" id="address" class="required"></textarea>
</div>
</div>
<div class="da-form-row">
<label>Quantity<span class="required">*</span></label>
<div class="da-form-item small">
<select name="quantity" id="quantity">
<option value=""> --- select Quantity--- </option>
</select>
</div>
</div>
<div class="da-form-row">
<label>Total Price<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="pri" id="pri" class="required number" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Courier<span class="required"></span></label>
<div class="da-form-item small">
<!--<input type="text" name="courier" id="courier" class="required" value=""/>-->
<select name="courier" id="courier">
<option value=""> ---select courier --- </option>
<?php
$ord=mysql_query("select courier_name from courier_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-form-row">
<label>Channel<span class="required"></span></label>
<div class="da-form-item small">
<!--<input type="text" name="channel" id="channel" class="required" value=""/>-->
<select name="channel" id="channel">
<option value=""> --- select channel ---</option>
<?php
$ord=mysql_query("select channel from channel_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-button-row">
<input type="submit" name="submit" value="submit" class="da-button grey" />
</div>
</fieldset>
</form>
</div>
<!-- End of .grid_4 --> </div>
</div>
<script>
function retrievedata(data)
{
var option_html = "";
<?php
$sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
while($ord1=mysql_fetch_array($sql))
{
?>
if(data == '<?php echo $ord1['brand']; ?>')
{
option_html += "<option><?php echo $ord1['product_name']; ?></option>";
/*alert(option_html);*/
}
<?php
}
?>
var par = document.getElementById("prod");
par.innerHTML = "<option>--- select product ---</option>"+option_html;
}
function retrievequantity(product)
{
var option_quantity_html = "";
<?php
$sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
while($ord2=mysql_fetch_array($sql))
{
$i=1;
?>
if(product == '<?php echo $ord2['product_name']; ?>')
{
<?php
while($i<=intval($ord2['quantity_in_stock'])){?>
option_quantity_html += "<option><?php echo $i++; ?></option>";
<?php }?>
}
<?php
}
?>
var par = document.getElementById("quantity");
par.innerHTML = option_quantity_html;
}
</script>
book_order2.php
<?php /*?><?php
$id = $_SESSION['id'];
echo $id;
?><?php */?>
<?php
include("includes/db.php");
?>
<div class="grid_4">
<div class="da-panel">
<div class="da-panel-header">
<span class="da-panel-title">
<img src="images/icons/color/wand.png" alt="" />
<font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
</span>
</div>
<div class="da-panel-toolbar top">
<ul>
<li><div class="da-button blue large">View all Orders</div></li>
</ul>
</div>
<div class="da-panel-content">
<?php
if(isset($_POST['submit']))
{
extract($_POST);
$order_date=date("Y-m-d");
$sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
$array=mysql_fetch_array($sql);
$sku = $array[0];
$qis = $array[1];
$sold_quan = $array[2];
$crdate = $array[3];
$sql2=mysql_query("INSERT INTO order_management2(order_id,order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
VALUES
('$id','$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");
if($sql2)
{
echo "<div class='da-message success'>Successfully Booked Your Order</div>";
?>
<script>
var r = confirm("want to add more products?");
if (r == true)
{
//x="You pressed OK!";
window.location = "main.php?page=book_order2";
}
else
{
//x="You pressed Cancel!";
window.location = "main.php";
}
</script>
<?php
}
else
{
die(mysql_error());
}
$quantity_left = $qis - $quantity;
$sold_quan = $sold_quan + $quantity;
$diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
$expctd_stock=round((7*$quantity_left)/$diff_in_days);
//echo $expctd_stock;
$sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
/*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/
//$sql4 =mysql_query("select order_id from order_management where sku='$sku'");
//$idarray=mysql_fetch_array($sql4);
}
?>
<form id="da-ex-validate1" class="da-form" method="post" action="">
<div class="da-form-row">
<label>Brand<span class="required">*</span></label>
<div class="da-form-item small">
<!--<input type="text" name="brand" id="brand" class="required" value=""/>-->
<select name="brand" id="brand" onChange="retrievedata(this.value)">
<option value="">--- select brand ---</option>
<?php
$ord=mysql_query("select * from brand_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-form-row">
<label>Product<span class="required">*</span></label>
<div class="da-form-item small">
<select name="prod" id="prod" onChange="retrievequantity(this.value)">
<option value="">--- select product ---</option>
</select>
</div>
</div>
<div class="da-form-row">
<label>Customer name<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="customername" id="customername" class="required char" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Phone Number<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="phonenumber" id="phonenumber" class="required number" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Email<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="email" id="email" class="required email" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Address<span class="required">*</span></label>
<div class="da-form-item small">
<textarea name="address" id="address" class="required"></textarea>
</div>
</div>
<div class="da-form-row">
<label>Quantity<span class="required">*</span></label>
<div class="da-form-item small">
<select name="quantity" id="quantity">
<option value=""> --- select Quantity--- </option>
</select>
</div>
</div>
<div class="da-form-row">
<label>Total Price<span class="required">*</span></label>
<div class="da-form-item small">
<input type="text" name="pri" id="pri" class="required number" value=""/>
</div>
</div>
<div class="da-form-row">
<label>Courier<span class="required"></span></label>
<div class="da-form-item small">
<!--<input type="text" name="courier" id="courier" class="required" value=""/>-->
<select name="courier" id="courier">
<option value=""> ---select courier --- </option>
<?php
$ord=mysql_query("select courier_name from courier_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-form-row">
<label>Channel<span class="required"></span></label>
<div class="da-form-item small">
<!--<input type="text" name="channel" id="channel" class="required" value=""/>-->
<select name="channel" id="channel">
<option value=""> --- select channel ---</option>
<?php
$ord=mysql_query("select channel from channel_info");
while($ord1=mysql_fetch_array($ord))
{
?>
<option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="da-button-row">
<input type="submit" name="submit" value="submit" class="da-button grey" />
<?php /*?><a href="book_order2.php?&id=<?php echo $id; ?>" name="submit" value="submit" class="da-button grey">Book Order</a<?php */?>
</div>
</fieldset>
</form>
</div>
<!-- End of .grid_4 --> </div>
</div>
<script>
function retrievedata(data)
{
var option_html = "";
<?php
$sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
while($ord1=mysql_fetch_array($sql))
{
?>
if(data == '<?php echo $ord1['brand']; ?>')
{
option_html += "<option><?php echo $ord1['product_name']; ?></option>";
/*alert(option_html);*/
}
<?php
}
?>
var par = document.getElementById("prod");
par.innerHTML = "<option>--- select product ---</option>"+option_html;
}
function retrievequantity(product)
{
var option_quantity_html = "";
<?php
$sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
while($ord2=mysql_fetch_array($sql))
{
$i=1;
?>
if(product == '<?php echo $ord2['product_name']; ?>')
{
<?php
while($i<=intval($ord2['quantity_in_stock'])){?>
option_quantity_html += "<option><?php echo $i++; ?></option>";
<?php }?>
}
<?php
}
?>
var par = document.getElementById("quantity");
par.innerHTML = option_quantity_html;
}
</script>
You can store the order number in the session. This way, it will be protected and persisted across pages.
When you insert the order in book_order.php, store it in the session:
$sql2=mysql_query(....); // inserting
if($sql2){
$_SESSION['order_id'] = mysql_insert_id();
}
Now, in book_order2.php you can retrieve the order ID before you do the insert of the product:
$id = $_SESSION['order_id'];
// insert product with order_id = $id
In order to use PHP sessions, you must calll session_start() at the beginning of any script that makes use of the session. If you have a global/header include then you can do it there.
Side notes:
mysql_* is deprecated. Consider upgrading to PDO or MySQLi. This is a good PDO tutorial, especially if you're upgrading from mysql_*.
Use a Prepared Statement with bound parameters instead of concatenating variables into SQL.
I would use ajax. For example:
$(document).ready(function()
{
$("form").on('submit',function(event)
{
event.preventDefault();
data = "var=data";
$.ajax
({
type: "GET",
url: "parser.php",
data: data
}).done(function(msg)
{
alert(msg);
});
});
});
It will send GET into parser.php. And the data field would be data in $_GET['var']