I have a database, golf_add.php and ajax_price.php Files.
So I can not do this:
After I choose values from form like > From | To | Transfer Type | Golf Date option box fields, I want these fields' values are compared and select the appropriate data from database (for golf price value) into Golf Price textBox.
For example:
From: XXX Otel (id=15)To: YYY Place(id=25)Transfer Type: Shuttle (id=4)Golf Date: 2015/08/05
After I choose these values from form I want to click refresh button OR after I selected the Golf Date last step its automatically must get only price from correct row according to my selection as above..
You can preview my form here :
golf_add.php code:
<!-- OTHER CODES ... -->
<script>
function refreshforprice() {
jQuery.ajax({
type: 'GET',
url: 'ajax_price.php',
data: 'tid='+jQuery('#op_transfer_type_id').val()+'&p='+jQuery('#operation_pax').val()+'&p2='+jQuery('#operation_pax2').val()+'&v=1',
cache : false,
success: function(sonuc){
sonucx = sonuc.split(';');
jQuery('#sold_tour_price_auto').val(sonucx[0]+'.00');
jQuery('#xprice1').html('* '+sonucx[1]+'.00 €');
jQuery('#xprice2').html('* '+sonucx[2]+'.00 €');
}
});
}
</script>
<form method="POST" action="golf_add.php" name="adminForm" id="adminForm">
<table class="adminlist">
<thead>
<tr>
<th> GOLF DETAILS </th>
</tr>
</thead>
</table>
<table class="adminlist">
<tbody>
<?PHP $i = 0; ?>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td align="right"><b>From</b></td>
<td id="fromarea">
<select size="1" name="operation_pickup_from" id="operation_pickup_from">
<option value="-1" disabled selected> --- Select Pick Up Point --- </option>
<?php
$strSQL2 = "SELECT * FROM pickup_points_From WHERE region_id='".$regionidx."' soldtours='1' ORDER BY pickup_point_name ASC";
$resultSQL2 = mysql_query($strSQL2);
$ks = mysql_num_rows($resultSQL2);
for ($z=0; $z<$ks; $z++) {
$rowSQL2 = mysql_fetch_array($resultSQL2);
?><option value="<?=$rowSQL2['pickup_point_id']?>"><?=$rowSQL2['pickup_point_name']?></option><?
}
?>
</select>
</td>
</tr>
<tr class="<?php echo "row1";?>">
<td align="right"><b>To</b></td>
<td id="fromarea">
<select size="1" name="operation_pickup_to" id="operation_pickup_to">
<option value="-1" disabled selected> --- Select Drop Point --- </option>
<?php
$strSQL3 = "SELECT * FROM pickup_points_To WHERE region_id='".$regionidx."' AND soldtours='1' ORDER BY drop_point_name";
$resultSQL3 = mysql_query($strSQL3);
while($rowSQL3 = mysql_fetch_array($resultSQL3)) {
?>
<option value="<?=$rowSQL3['drop_point_id']?>"><?=$rowSQL3['drop_point_name']?></option>
<? } ?>
</select>
</td>
</tr>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td align="right"><b>Transfer Type</b></td>
<td id="tourtypes">
<select size="1" name="op_transfer_type_id" id="op_transfer_type_id">
<option value="-1" disabled selected> --- Select Transfer Type --- </option>
<?php
$strSQL2 = "SELECT * FROM operation_transfer_types WHERE op_transfer_type_id IN (4,10) ORDER BY op_transfer_type_name";
$resultSQL2 = mysql_query($strSQL2);
while($rowSQL2 = mysql_fetch_array($resultSQL2)) {
?><option value="<?=$rowSQL2['op_transfer_type_id']?>"><?=$rowSQL2['op_transfer_type_name']?></option><?
}
?>
</select>
</td>
</tr>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td width="45%" align="right"><b>Golf Date</b>»</td>
<td width="55%"><input type="text" name="operation_date" id="operation_date" value="<?=$_GET['selected_day']?>" size="10" class="datepicker" autocomplete="off"> <input type="text" name="operation_time" id="operation_time" size="5" class="timepicker" autocomplete="off">«<b>Golf Time</b></td>
</tr>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td align="right"><b>Adult</b></td>
<td><input type="text" name="operation_pax" id="operation_pax" size="5" value="1" onkeypress="return OnlyDigits(event);"
onblur="OnlyDigits(event,false)" autocomplete="off"> <span id="xprice1" style="color: green;">* 0.00 €. </span>
</tr>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td align="right" id="agex1"><b>06-12 Age</b></td>
<td><input type="text" name="operation_pax2" id="operation_pax2" size="5" value="0" onkeypress="return OnlyDigits(event);"
onblur="OnlyDigits(event,false)" autocomplete="off"> <span id="xprice2" style="color: green;">* 0.00 €. </span>
</td>
</tr>
<tr class="<?php $i++; $m=$i%2; if($m=='1') echo "row0"; else echo "row1";?>">
<td align="right"><b>Golf Price</b></td>
<td id="pricediv"><input type="text" name="sold_tour_price_auto" id="sold_tour_price_auto" size="8" class="bestupper"
value="0.00" onkeypress="return OnlyDigits(event,'.');" onblur="OnlyDigits(event,false)"> EUR
</td>
</tr>
</tbody>
</table>
</form>
<!-- OTHER CODES ... -->
ajax_price.php Code:
<?php
$tid = intval(#$_GET['tid']);
$p = intval(#$_GET['p']);
$p2 = intval(#$_GET['p2']);
$v = intval(#$_GET['v']);
$sql = "SELECT * FROM transfer_type WHERE transfer_id='".$tid."'";
$s = mysql_query($sql);
$ks = mysql_num_rows($s);
if ($ks > 0) {
$k = mysql_fetch_assoc($s);
$pprice = floatval($k['golf_price']);
if ($tid == 4) {
$price_pax = $p + $p2;
$price = ($price_pax * $pprice) + ($price_pax * 7);
}
else {
$price = ($pprice * $p) + ($pprice * $p2);
}
if ($v == 1) { echo $price.';'.$pprice.';'.$pprice; }
else { echo $price; }
?><?PHP
}
else {
if ($v == 1) { ?>0;0;0<?PHP }
else { ?>0<?PHP }
}
?>
I'm not too sure but I can't see where you are using the function refreshforprice() in your form, I have done similar using the function in a onchange in a <select> box.
eg <select id="something" name="something" onchange"function();">
Related
I have wasted a day almost to solve this but not able to find solution. I am stuck in MSSQL procedure with php. i have a form of 5 fields which return a "Amount" when calculate. The Procedure is working perfectly on SQl Managemant 2008 but it display an error while execute from php code. Please help me please below my code and screenshot.
<div id="middle_pane">
<div>
<div id="inner_pane">
<div id="center_pane">
<div id="center_inner_pane">
<div class="txt_content">
<table width="385" height="200" style=" border:2px solid silver" cellpadding="6px" cellspacing="0px" align="center" border="0">
<form method="POST" action="<?php bloginfo('url');?>/fedex-tariff">
<tr>
<td colspan="2" style="background:#0066FF; color:#FFFFFF; fontsize:50px">Fedex Rate Card</td></tr>
<tr>
<td>Origin</td>
<td>
<select name="origin" id="origin" onChange="">
<option value="pakistan">Pakistan</option>
</select>
</td>
</tr>
<tr>
<td>Select Origin city </td>
<td>
<select name="city_id" id="city_id">
<option value="">--SELECT CITY--</option>
<?php
$city = "select * from branches";
$city_query = mssql_query($city);
while ($row_city = mssql_fetch_array($city_query)){
?>
<option value="<?php echo $row_city['zoneCode'];?>"> <?php echo $row_city['name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Select Destination </td>
<td>
<select name="country_id" id="country_id">
<option value="">--SELECT Destination--</option>
<?php
$service= "select * from country";
$service_query= mssql_query($service);
while ($row_service = mssql_fetch_array($service_query)){
?>
<option value="<?php echo $row_service['Code'];?>"> <?php echo $row_service['Name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> Selection of Weight </td>
<td>
<div id="wgt">
<input type="text" id="weight" name="weight" value="" placeholder="weight...." required="">
</div>
</td>
</tr>
<tr>
<td>Select Your Service</td>
<td>
<select name="service_id" id="service_id">
<option value="">--SELECT--</option>
<?php
$service_type = "SELECT distinct ServiceTypeId from dbo.IntlZoneCountry where ServiceTypeId IN ('FedEx Cash Tariff Docs','FedEx Box','FedEx Student Package')";
$res_type = mssql_query($service_type);
$cnt_type = mssql_num_rows($res_type);
while($row_type = mssql_fetch_array($res_type)){
?>
<option value="<?php echo $row_type['ServiceTypeId'];?>"><?php echo $row_type['ServiceTypeId'];?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> <input type="submit" value="Calculate" name="submit" class="btn"></td>
</tr>
</form>
<table width="385" style=" border:2px solid silver" cellpadding="6px" cellspacing="0px" align="center" border="0">
<tr>
<td>
<?php
if(isset($_POST['submit']))
{
$city_id = $_POST['city_id'];
$country_id = $_POST['country_id'];
$service_id = $_POST['service_id'];
$weight_id = $_POST['weight'];
//This is PHP validation
if(empty($city_id) || empty($country_id) || empty($service_id) || empty($weight_id)){
?><center><?php echo "Please filled all the fields first.";?></center>
<?php
}
else{
$tozoneCode = "Select ZoneCode from dbo.IntlZoneCountry where CountryCode= '$country_id' AND ServiceTypeId= '$service_id'";
$result_tozoneCode = mssql_query($tozoneCode,$conn);
while($row = mssql_fetch_assoc($result_tozoneCode)){
$toZone = $row['ZoneCode'];
}
$result = mssql_query('set ANSI_NULL_DFLT_ON ON',$conn);
$stmt = mssql_init('SP_UpdateCNPrice_Int_Tariff',$conn);
echo "<pre>";
print_r($stmt);
//$weight_id = '5';$city_id = '3';$toZone = 'D';$service_id = 'fedex';$gstPercent='12.2';
mssql_bind($stmt, '#weight', $weight_id, SQLVARCHAR);
mssql_bind($stmt, '#FromZone', $city_id, SQLVARCHAR);
mssql_bind($stmt, '#ToZone', $toZone, SQLVARCHAR);
mssql_bind($stmt, '#serviceType', $service_id, SQLVARCHAR);
// $outVar1 = '';
//$outVar2 = '';
// mssql_bind($stmt, '#amount', $outVar1, SQLVARCHAR);
//mssql_bind($stmt, '#gst', $outVar2, SQLVARCHAR);
//$proc_result = mssql_execute($stmt, true);
if ($proc_result = mssql_execute($stmt)){
while ($row = mssql_fetch_assoc($proc_result)){
echo $row["amount"];
}
}
die("<BR>".mssql_get_last_message());
//print($outVar1);
//print($outVar2);
mssql_close($conn);
// $proc = mssql_init("[SP_UpdateCNPrice_Int_Tariff] '$weight_id','$city_id','$toZone','$service_id'", $conn);
//$result = mssql_execute($proc);
//print_r($result);
/*while($row = mssql_fetch_assoc($result)){
//print_r($row);
?>
</td>
</tr>
<tr style="background:#00CFFF; color:blue;">
<td>
<center>
<font size="3">
<?php
$val = $row['amount'];
$gst = $row['gst'];
echo "<strong>"."Rate : "."$".""." ".$val.$gst;
?>
</font>
</center>
</td>
</tr>
<tr style="background:#00CFFF; color:blue;">
<td><center> All Rates are Exclusive of 16% GST + 23% fuel Charges (FSC).</center> </td>
</tr>
<?php
}*/
}
}
// mssql_close();
?>
</table>
</div>
</br>
<font size="2"><center>For more details <strong>Contact Us</strong> at any time.</center></font>
<div style="float:right;"></div>
</div>
</div>
</div>
</div>
</div>
Rate Card Front end display
MSSQL MANAGEMENT SCREENSHOT
I was just having an identical problem to this.
My PHP -> MSSQL stored procs had been working properly previously, and then, something changed in the environment, and all of a sudden the code stopped working.
I switched from using:
$proc = mssql_init('storedProceedureName', $connection );
mssql_execute($proc);
to using
mssql_query( 'EXEC storedProceedureName', $connection );
And all of a sudden it started working again. Not really sure why that's the case, but I'm not about to look a gift horse in the mouth.
I have some combobox with onchange event, and they're reset each other when selected the orther one of them, does any suggest how to retain the value on the page? this my script :
<form method="POST" name="form1" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>
<select name="select_petugas1" style="width:18px;" onchange="this.form.submit('select_petugas1');"> //first combobox
<option></option>
<?php include 'dbconn.php';
$sql_peg1="SELECT * FROM users"; $result_peg1=$conn->query($sql_peg1);
while( $row_peg1=$result_peg1->fetch_assoc() ){
echo "<option>".$row_peg1['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if(isset($_POST['select_petugas1'])){
$select_petugas1=$_POST['select_petugas1'];
echo "<input type='text' name='select_petugas1' value='".$select_petugas1."'>"; // Throw 1st result into the text box
$sql_NIP1="SELECT NIP FROM users WHERE nama='$select_petugas1'";
$result_NIP1=$conn->query($sql_NIP1);
$row_NIP1=$result_NIP1->fetch_assoc();
$NIP1=$row_NIP1['NIP'];
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type="text" name='NIP1' value="<?php echo $NIP1; ?>"></td>
</tr> <!-- child of first result -->
<tr>
<td colspan="5" bgcolor="blue"></td>
</tr>
<tr>
<td>
<select name="peg_2" style="width:18px;" onchange="submit(this)"><!--2nd combobox-->
<option></option>
<?php
$sql_peg2="SELECT nama FROM users";
$result_peg2=$conn->query( $sql_peg2 );
while ($row_peg2=$result_peg2->fetch_assoc()){
echo "<option value='".$row_peg2['nama']."'>".$row_peg2['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if( isset($_POST['peg_2']) ){
$peg_2=$_POST['peg_2'];
echo "<input type='text' name='peg2' value='".$peg_2."'>"; // 2nd result throw into 2nd texbox
$sql_NIP2="SELECT NIP FROM users WHERE nama='$peg_2'";
$result_NIP2=$conn->query($sql_NIP2);
$row_NIP2=$result_NIP2->fetch_assoc();
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type='text' name='NIP2' value="<?php echo $row_NIP2['NIP'];?>"> <!--2nd child of result-->
<?php
}
}
if(isset($_POST['NIP2'])){
$NIP2=$_POST['NIP2'];
echo "<br /> NIP2 :".$NIP2."<br />";
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
<form method="POST" name="wilayah" id="wilayah" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="1">
<tr>
<td>
<select name="select_provinsi" onchange="submit(this)" style="width:18;">
<option selected>PROVINSI</option>
<?php
include 'dbconn.php';
$sql_prov="SELECT * FROM wilayah GROUP BY provinsi";
$result_prov=$conn->query($sql_prov);
echo "";
while($row_prov=$result_prov->fetch_assoc()){
$provinsi=$row_prov['provinsi'];
echo "<option value='".$provinsi."'>".$provinsi."</option>";
}
?>
</select>
<?php
if(isset($_POST['select_provinsi'])){
$select_provinsi=$_POST['select_provinsi'];
echo "
<input type='text' name='select_provinsi' value='".$select_provinsi."' placeholder='PROVINSI'>
</td>
</tr>";
$sql_kabkota="SELECT * FROM wilayah WHERE provinsi='$select_provinsi' GROUP BY kab_kota";
$result_kabkota=$conn->query($sql_kabkota);
?>
<tr>
<td>
<select name="select_kabkota" style="width:18px;" onchange="submit(this)"><option>KAB/KOTA</option>
<?php
while($row_kabkota=$result_kabkota->fetch_assoc()){
echo "<option>".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
<?php
}
if(isset($_POST['select_kabkota'])){
$select_kabkota=$_POST['select_kabkota'];
?>
<input type="text" name="kab_kota" value="<?php echo $select_kabkota;?>">
<?php
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
hope any suggestion for resolved of my problem with them,,
onchange="submit(this)" means that you want to submit the form when the value of the combobox changes. So, when the form is sent, the page reloads and you get the default value of your form.
To restore the chosen value, I would do something like :
<select name="select_kabkota" style="width:18px;" onchange="submit(this)">
<option>KAB/KOTA</option>
<?php
if(isset($_POST['select_kabkota']))
$select_kabkota=$_POST['select_kabkota'];
while($row_kabkota=$result_kabkota->fetch_assoc())
{
$selected = $select_kabkota == $row_kabkota['kab_kota'] ? 'selected="selected"' : '';
echo "<option ".$selected." >".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>
When a user select the name using drop down list,the stock and price text field should automatically be filled.But it wont.
This is the code
<form action="insertout.php" method="post">
<center>
<table>
<tr>
<td width="116">Medicine name</td>
<td width="221">
<center>:
<select name="name" id="name">
<option>--- Choose Medicine ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("arie");
$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_assoc($sql)){
echo '<option value="'.$row['price'].' && '.$row['price'].'">'.$row['name'].'</option>';
}
}
?>
</select >
</center>
</p>
</td>
</tr>
<tr>
<td>
<p>Price</p>
</td>
<td>
<p align="center">:<input type="text" name="price" id="price"value="<?php echo ('priceperunit'); ?>" onClick="checkprice()">
</p>
</td>
</tr>
<tr>
<td>
<p>Stock</p>
</td>
<td>
<p align="center">:<input type="text" name="stock" id="stock"value="<?php echo ('stock'); ?>" onClick="checkprice()">
</p>
</td>
</tr>
<script>
var select = document.getElementById('name');
var input = document.getElementById('price');
var input = document.getElementById('stock');
select.onchange = function(){
input.value = select.value;
}
</script>
i hope u guys can help.i've spent a week just to figured out the code.
Try this below code, below are some notes
Avoid using the Keywords like "name" in select box
Price and Stock Input element attribute are does not have spaced properly.
Avoid using Mysql_connect and start use PDO / MySQLi
Avoid presentational tags and start use css to acheive it like "text-align:center"
Currently its working as you expected without Ajax and its a quick workaround, I have added the value in a comma separated the both value by split function.
<form action="insertout.php" method="post">
<center>
<table>
<tr>
<td width="116">Medicine name</td>
<td width="221">
<center>:
<select name="name" id="name">
<option>--- Choose Medicine ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("arie");
$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_assoc($sql)){
// echo '<option value="'.$row['price'].' && '.$row['price'].'">'.$row['name'].'</option>';
$option_value = $row['price'] . ',' . $row['stock'];
echo '<option value="'.$option_value.'">'.$row['name'].'</option>';
}
}
?>
</select >
</center>
</p>
</td>
</tr>
<tr>
<td>
<p>Price</p>
</td>
<td>
<p align="center">:<input type="text" name="price" id="price"value="<?php echo ('priceperunit'); ?>" onClick="checkprice()">
</p>
</td>
</tr>
<tr>
<td>
<p>Stock</p>
</td>
<td>
<p align="center">:<input type="text" name="stock" id="stock"value="<?php echo ('stock'); ?>" onClick="checkprice()">
</p>
</td>
</tr>
<script>
var select = document.getElementById('name');
//var input = document.getElementById('price');
//var input = document.getElementById('stock');
var price = document.getElementById('price');
var stock = document.getElementById('stock');
select.onchange = function(){
// input.value = select.value;
var price_stock = select.value.split(',');
price.value = price_stock[0];
stock.value = price_stock[1];
}
</script>
How about this.......
Main File
<?php
$medOptions = "";
mysql_connect( "localhost", "root", "");
mysql_select_db( "arie");
$sql = mysql_query( "SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) !=0 ) {
while($row = mysql_fetch_assoc($sql)) {
$medOptions .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>\n';
}
}
?>
<form>
<table>
<tr>
<td class="label" width="55%">Medicine name</td>
<td width="45%">
<select name="med" id="med">
<option disabled="true" selected="true">--- Choose Medicine ---</option>
<?=$medOptions;?>
</select>
</td>
</tr>
<tr>
<td class="label">Price</td>
<td><input type="text" id="price" readonly="true" /></td>
</tr>
<tr>
<td class="label">Stock</td>
<td><input type="text" id="stock" readonly="true" /></td>
</tr>
</table>
CSS
table {
margin: 0 auto;
}
td {
text-align:center;
}
.label {
font-weight:bold;
}
Javascript
var select = document.getElementById('med');
// INSERT AJAX ROUTINE TO RETRIEVE PRICE AND STOCK LEVEL
var medPrice = document.getElementById('price');
var medStock = document.getElementById('stock');
select.onchange = function () {
medPrice.value = response.price;
medStock.value = response.stock;
};
I need to fill the code and initials of a employment with the same javascript functions.
The java script function
<script type="text/javascript">
function Expedisi(t) {
var y = document.getElementById("code");
y.value = t.value;
}
</script>
<tr>
<td width="200px" align="right"><label for=""><font color="0099FF"size="3px">Code</font><span></span></label></td>
<td align="left"><input type="text" id="code" name="code" value=" <?php echo ($addflag == 0) ? $get['loan'] : ""; ?>" class="form-input-code" readonly />
</tr>
<tr>
<td width="200px" align="right"><label for=""><font color="0099FF" size="3px">Employee Name </font><span></span></label>
</td>
<td align="left"><select data-placeholder="Employee Name" style="width:332px;" id="cmbemployee" name="cmbemployee" class="chzn-select-deselect" onchange="Expedisi(this)""/>
<option value="<?php echo ($addflag == 0) ? $gete['accode'] : ""; ?>"><?php echo ($addflag == 0) ? $gete['name'] : ""; ?></option>
<?php
$mown = mysql_query("SELECT * FROM accmast WHERE grpcode='044'") or die(mysql_error());
while($trow = mysql_fetch_array($mown))
{
echo "<option value=$trow[accode]>$trow[name]</option>";
}
?>
</select>
</td>
</tr>
you can no get value of select element in js!
<script type="text/javascript">
function Expedisi(t) {
var y = document.getElementById("code");
y.value = t.options[t.selectedIndex].innerHTML
}
</script>
Place single quotes ('')around the value.
while($trow = mysql_fetch_array($mown))
{
echo "<option value='{$trow[accode]}'>{$trow[name]}</option>";
}