Change SQL Statement Upon Page Navigation - javascript

I'm fairly new to all of this, so correct me if this isn't possible.
I am trying to display data with this SQL statement:
SELECT * FROM trail101 WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id DESC LIMIT 0, 9
It only shows the first 9 pieces of data I have, but I'd like to be able to navigate through to the next 9, in which clicking the pagination would update the SQL statement to ORDER BY id DESC LIMIT 9, 18 for the next page and so forth. How would I go about scripting this? I have the following code for the navigation:
<div class="w3-center w3-padding-32">
<div class="w3-bar">
«
1
2
3
4
»
</div>

Got a simple code sample with your query for the pagination, check this out
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'password';
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test_db');
/* Get total number of records */
$sql = "SELECT * FROM trail101 WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM trail101 ".
"WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "ID :{$row['id']} <br> ".
"ACTIVITY NAME : {$row['activity_name']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($conn);
?>
</body>
</html>

Related

Cant get data from sql server to php page?

i have a php website linked with Sql Server database, im trying to get data from the database and show it on the page in a table but when it create the table it show me the message "No results found" , what is the code to get the info from the database and show it in the website?
ps:im using jquery.bootgrid
<?php
include 'dbh.inc.php';
$query='';
$data=array();
$records_per_page= 10;
$start_from= 0;
$current_page_number= 0;
if(isset($_POST["rowCount"]))
{
$records_per_page= $_POST["rowCount"];
}
else
{
$records_per_page= 10;
}
if(isset($_POST["current"]))
{
$records_per_page= $_POST["current"];
}
else
{
$records_per_page= 1;
}
$star_from= ($current_page_number - 1)* $records_per_page;
$query .="SELECT * FROM Employee";
if(!empty($_POST["searchPhrase"]))
{
$query .= 'where (Id like "%'.$_POST["searchPhrase"].'%")';
$query .= 'or First_Name like "%'.$_POST["searchPhrase"].'%" ';
$query .= 'or Last_Name like "%'.$_POST["searchPhrase"].'%" ';
$query .= 'or username like "%'.$_POST["searchPhrase"].'%" ';
}
$order_by = '';
if(isset($_POST["sort"]) && is_array($_POST["sort"]))
{
foreach($_POST["sort"] as $key =>$value )
{
$order_by = '$key $value,';
}
}
else
{
$query .= 'order by Id Desc';
}
if($order_by != '')
{
$query .= ' order by ' . substr($order_by,0,-2);
}
if($records_per_page != -1)
{
$query .= "LIMIT" .$star_from. "," .$records_per_page;
}
$result = sqlsrv_query($conn, $query);
while($row=sqlsrv_fetch_array($result))
{
$data[]=$row;
}
$query1 .="SELECT * FROM Employee";
$result1 = sqlsrv_query($conn, $query1);
$total_records=sqlsrv_has_rows($result1);
$output=array('current' => intval($_POST["current"]),
'rowCount' => 10,
'total' => intval($total_records),
'rows' => $data );
echo json_encode($output);
?>

Want to get the user info from database , and want to show on calendar

I have php code from this I got complete dates of room that are booked by one user like 12-15.
And 17-18, for second user 20-21, I got marked these dates on calendar.
Now I want to show user info on mouse hover:
<?php
include 'dbconfig.php';
$query_getaway = "SELECT * FROM getaway_table; ";
$result_getaway = $db->query($query_getaway);
$category = mysqli_real_escape_string($db, filter_input(INPUT_POST, 'category')) .'<br>';
$resort = mysqli_real_escape_string($db, filter_input(INPUT_POST, 'resorts')) .'<br>';
if( $category != 0 ){
$query = "SELECT * FROM booking_request where room_id = '$resort' '<br>' ";
$result = $db->query($query);
if( $result -> num_rows > 0 ) {
while( $row = $result-> fetch_assoc() )
{
$name = $row['getaway_name']." <br>";
$start = $row['check_in_date']." <br>";
$end = $row['check_out_date']." <br>";
$guest_n[]= $row['guest_name'];
$guest_number[]= $row['guest_phone'];
$arr1 = range(strtotime($row['check_in_date']),strtotime($row['check_out_date']), "86400");
array_walk_recursive($arr1, function(&$element) { $element = date("j-n-Y", $element); });
foreach ($arr1 as $value)
{
$arr2[]= $value;
}
$arrt= (array_merge($guest_n, $guest_number));
//print_r($arrt);
foreach($arrt as $val)
{
echo $arr_val[]=$val;
}
$arr[]= $value;
}
}
else { echo "select" ;}
?>
Then in js part I showed unavailable dates, bot on hover I'm unable to show user complete info:
<script type="text/javascript">
// Add New Dates and Block Out Booked Days Below
/*var unavailableDates1 = ["20-5-2016", "21-5-2016", "22-5-2016", "23-5-2016", "5-6-2016", "7-6-2016", "8-6-2016",
"15-6-2016", "16-6-2016", "25-6-2016", "26-6-2016", "27-6-2016", "28-6-2016", "14-2-2016",
"15-7-2016", "16-7-2016", "17-7-2016", "18-7-2016", "19-7-2016","20-7-2016","21-7-2016",
]; */
var unavailableDates = <?php echo json_encode($arr2); ?>;
document.write(unavailableDates);
function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1 ) + "-" + date.getFullYear();
if($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
}else {
this line is showing unavlaible on hover, i want from database user info
return [false, "","unavalible"];
<?php echo $val; ?>
}
}
$(function() {
$("#Datepicker1").datepicker({
numberOfMonths:3,
beforeShowDay: unavailable
});
});
</script>

Retrieve next row data every 5 mins

I have rows of data in my database table,
How to display
First; row ID 1,
after 5 mins row ID 2,
after 5 mins row ID 3
and so on, Automatically.
The webpage will auto retrieve next row every 5 mins and only display that row.
kinda like refreshing every 5 mins but displaying next row not the latest one.
below is my code :
<html>
<head>
<style>
#main {
margin:5% 15% 10% 15%;
background:url(viewBG.png);
width:70%;
height:80%;
}
p {
padding:20% 20% 20% 20%;
color:white;
font-family: Honey Script;
font-size:30pt;
}
body {
background-color: skyblue;
}
</style>
</head>
<body>
<?php
//connection to
require 'config.php';
$conn = mysqli_connect( $db_host, $db_username, $db_password, $db_name);
// Create connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql = "SELECT * FROM `demo` ORDER BY `ID` DESC ";
$sql = "SELECT Comment FROM demo ORDER BY ID DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div id="main">';
echo ' <p>"'.$row["Comment"].'"</p>';
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
You will have to combine javascript in your solution. Make your php script to echo a form with a parameter with the next row to retrieve, lets say in php this :
<?php
if (isset($_REQUEST["next_row"])) {
$myrow = $_REQUEST["next_row"];
} else {
$myrow = 0;
}
echo '<form id=myform> <input type=hidden name="next_row" value="'.($myrow * 1 + 1).'"> </form>';
//connection to
require 'config.php';
$conn = mysqli_connect( $db_host, $db_username, $db_password, $db_name);
// Create connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql = "SELECT * FROM `demo` ORDER BY `ID` DESC ";
$sql = "SELECT Comment FROM demo ORDER BY ID DESC LIMIT 1 OFFSET ". $myrow;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div id="main">';
echo ' <p>"'.$row["Comment"].'"</p>';
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Ok now on Javascript at the end of the page the following code (be sure to include jquery.js):
setTimeout(function(){ $('#myform').submit(); }, 300000);

How to change quantity with php and plausible js?

Okay so I have this problem on a site im working om.. some merchandise that have been selected are POST'ed from a form to this page. Then the prices are gathered from the db and it all gets added to get the total sum price.. However I need to get a function where you can change the quantity of the items selected.
The code I have so far is this - For displaying the selected products.
$t_id=implode(',',$t_id);//impload the value with the ',' and used IN operate to get all the value relate this id (11, 19 and 20)
$query = "SELECT * FROM tilbehor WHERE t_id IN ($t_id)";
$result = $db->query($query);
$num_rows = $result->num_rows;
if($num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
echo "
∙ <input type='text' style='width: 50px;' name='quantity' id='quantity' class='txt' value='1'> - <b>".$row['tilbehor_navn']."</b><br>
";
}
} else {
echo "Error";
}
And here is the code to display and add up alle the prices.
$test = $_POST['tilbehor'];
$test=implode(',',$test);//impload the value with the ',' and used IN operate to get all the value relate this id (11, 19 and 20)
$query = "SELECT * FROM tilbehor WHERE t_id IN ($test)";
$result = $db->query($query);
$num_rows = $result->num_rows;
if($num_rows > 0) {
$number = $p_pris + $o_tilbehor_pris;
while ($row = mysqli_fetch_array($result)) {
$number += $row['kunde_pris'];
}
$output = $number;
$followers = number_format( $output , 0 , '.' , '.' );
echo "
<br>
<input type='checkbox' name='price' value='$followers'>Kunde pris $followers<br>
";
} else {
$number = $p_pris + $o_tilbehor_pris;
$output = $number;
$followers = number_format( $output , 0 , '.' , '.' );
echo $followers;
echo ",-";
}
Anyone have any idea on how to code it so you can change the quantity of an item and so that the price is affected as well? :)

Why php code inside the javascript cannot change the value of innerHTML? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
This page include two web page, insert6.php is using iframe.
This full code of the web site of the page.
form6.php
<!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=big5" />
<title>CCC</title>
<style>
#tlist tr:last-child td {
background-color:#CCCCCC;
}
table{
table-layout: fixed;
}
th, td {
overflow: hidden;
}
#container
{
margin-left:auto;
margin-right:auto;
width:600px;
}
</style>
</head>
<body>
<h3>New Record</h3>
<script language="JavaScript" type="text/javascript">
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var che = document.createElement('input');
che.type = 'checkbox';
che.id = 'op'+ iteration;
che.name= 'checkbox';
cellLeft.appendChild(che);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow[]';
el.id = 'txtRow' + iteration;
el.size = 40;
el.onkeypress = keyPressTest;
cellRight.appendChild(el);
// select cell
var cellRightSel = row.insertCell(2);
var sel = document.createElement('select');
//sel.type = 'text';
sel.name = 'selRow[]';
sel.id = 'selRow' + iteration;
sel.options[0]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="FF")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
sel.options[1]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="DD")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
sel.options[2]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="TT")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
sel.options[3]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="GG")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
sel.options[4]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="RR")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
sel.options[5]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="AA")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
cellRightSel.appendChild(sel);
}
function keyPressTest(e, obj)
{
var validateChkb = document.getElementById('chkValidateOnKeyPress');
if (validateChkb.checked) {
var displayObj = document.getElementById('spanOutput');
var key;
if(window.event) {
key = window.event.keyCode;
}
else if(e.which) {
key = e.which;
}
var objId;
if (obj != null) {
objId = obj.id;
} else {
objId = this.id;
}
displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
}
}
function openInNewWindow(frm)
{
// open a blank window
var aWindow = window.open('', 'TableAddRowNewWindow',
'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
// set the target to the blank window
frm.target = 'TableAddRowNewWindow';
// submit
frm.submit();
}
function validateRow(frm)
{
var chkb = document.getElementById('chkValidate');
if (chkb.checked) {
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length - 1;
var i;
for (i=1; i<=lastRow; i++) {
var aRow = document.getElementById('txtRow' + i);
if (aRow.value.length <= 0) {
alert('Row ' + i + ' is empty');
return;
}
}
}
openInNewWindow(frm);
}
function deleteAll(obj){
var checked = document.getElementsByName(obj);
debugger
for(var i = 0; i < checked.length; i ++){
if(checked[i].checked){
var tr=checked[i].parentNode.parentNode;
var tbody=tr.parentNode;
tbody.removeChild(tr);
i--;
}
}
}
</script>
<!--Input Data-->
<form action="insert6.php" method="post" target="myframe">
Series:
<?php
function series(){
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT Series_NAME FROM series;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
echo("<option>".$row[$i]."</option>");
}
}
mysql_free_result($result);
}
?>
<select name="choose series">
<?php series(); ?>
</select><br>
<?php
function height(){
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT height FROM width_height GROUP BY height;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
echo("<option>".$row[$i]."</option>");
}
}
mysql_free_result($result);
}
?>
<p>Height(MM):
<select name="height" id="height0">
<?php height(); ?>
</select></p>
<?php
function width(){
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT width FROM width_height GROUP BY width;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
echo("<option>".$row[$i]."</option>");
}
}
mysql_free_result($result);
}
?>
<p>Width (MM):
<select name="width" id="width0">
<option><?php width(); ?></option>
</select><br>
<table border="1" id="tblSample">
<tr>
<td><input type="checkbox" id="op0" name="checkbox">
</td>
<td><input type="text" name="txtRow[]"
id="txtRow0" size="40"/></td>
<td>
<select name="selRow[]" id="selRow0">
<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
echo("<option>".$row[$i]."</option>");
}
}
mysql_free_result($result);
?>
</select>
</td>
</tr>
</table>
<input type="button" value="Add" onClick="addRowToTable();" />
<input type="button" name="delete_button" value="Delete" onClick="deleteAll('checkbox');" />
<input type="submit" name="submit" value="Submit" onclick="test();"/>
</form>
<script src="https://dl.dropboxusercontent.com/u/19096175/blog/selectDate/selectDate.js" type="text/javascript"></script>
Choose Date:<input onfocus="HS_setDate(this)" readonly="" type="text" value="Check" />
<br />
<hr/>
<iframe src="insert6.php" name="myframe" id="myframe" width="650" height="400" scrolling="no" frameborder="0">
</iframe>
</body>
</html>
This full code of the web site of the page.
insert6.php
<!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>
<title>DDD</title>
<style>
#tlist tr:last-child td {
background-color:#CCCCCC;
}
table{
table-layout: fixed;
}
th, td {
overflow: hidden;
}
#container
{
margin-left:auto;
margin-right:auto;
width:600px;
}
</style>
</head>
<body>
<?php
/*
$name = $_POST['selRow'];
$qty = $_POST['txtRow'];
foreach( $qty as $v ) {
print $v."<br>";
}
foreach( $name as $v ) {
print $v."<br>";
}
*/
?>
<script>
function delrecord(obj)
{
obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
var delbutton=document.getElementsByName("del");
var newzum=0;
for(var j=1;j <document.getElementsByName("del").length+1;j++)
{
newzum+=parseFloat(document.getElementById('total'+j).innerHTML);
}
znum.innerHTML =newzum;
}
function caltotal(e){
var fqty=document.getElementById('qty' +iteration);
var fprice=document.getElementById('price' + iteration);
var ftotal=document.getElementById('total'+ iteration);
var delbutton=document.getElementsByName("del");
ftotal.innerHTML= fqty.innerHTML * fprice.innerHTML;
newzum =0;
for(var j=1;j <=document.getElementsByName("del").length;j++)
{
newzum += parseFloat(document.getElementById('total'+j).innerHTML);
}
znum.innerHTML = newzum;
}
</script>
<div class="container">
<table width="450" border="0" cellspacing="1" cellpadding="0" class="tb" id="tlist">
<tr class="tit2">
<td>QTY </td>
<td>TILE </td>
<td>HEIGHT </td>
<td>WIDTH </td>
<td>PRICE </td>
<td>TOTAL </td>
<td>Action </td>
</tr>
<tr class="tit3">
<?php
if(isset($_POST["submit"])){
$t=count($_POST['selRow']);
$w=count($_POST['txtRow']);
for($i=0;$i<$t;$i++)
{
$qty[$i]= $_POST['txtRow'][$i];
$tile[$i]= $_POST['selRow'][$i];
$height = $_POST['height'];
$width = $_POST['width'];
echo "<tr class='tit3'><td>";
echo "".$qty[$i]."<br>";
echo "</td>";
echo "<td>";
echo "".$tile[$i]."<br>";
echo "</td>";
echo "<td>";
echo "".$height."<br>";
echo "</td>";
echo "<td>";
echo "".$width."<br>";
echo "</td>";
echo "<td>";
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
//foreach($_POST['selRow'] as $tile){
$query = "SELECT `Price` FROM `actualpanelmaterialsize` WHERE `Material ID` IN (SELECT `Material_ID` FROM `material` WHERE `Tile` = '".$tile[$i]."') AND `Width_Height ID` IN (SELECT `Width_Height ID` FROM `width_height` WHERE `Width` =".$_POST['width']." AND `Height` =".$_POST['height'].");";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
$price = $row[0];
echo $price."<br>";
}
//}
echo "</td>";
echo "<td id='total[$i]'>";
echo $total[$i]=$price*$qty[$i];
echo "</td>";
echo "<td>";
echo "<input type='button' value='Delete' name='del' onclick='delrecord(this);'>";
echo "</td>";
echo "</tr>";
}
echo "<script>";
echo "newzum =0;";
echo "for(var j=0;j <=document.getElementsByName('del').length+1;j++) {" ;
echo "for (var i=0;i<=document.getElementById('total['+i+']').innerHTML.length;i++){";
echo "newzum += parseFloat(document.getElementById('total['+i+']').innerHTML);";
echo "}";
echo "}";
echo "document.getElementById('znum').innerHTML = newzum;";
echo "</script>";
}
?>
</tr>
<tr class="tit3"> <td>Total </td> <td colspan=3> </td> <td colspan=2 align='right'> <b id="znum">0</b> </td> <td colspan=1> </td> </tr>
</table>
<script>
/*
for (var i=0;i<=document.getElementById("total["+i+"]").innerHTML.length;i++){
window.alert(document.getElementById("total["+i+"]").innerHTML);
}
*/
function Check(){
for (var i=0;i<=document.getElementById("total["+i+"]").innerHTML.length;i++){
window.alert(newzum);
}
}
</script>
<input type="button" value="Check" onClick="Check();"/>
</div>
</body>
</html>
Last, I was ask you guys about why znum.innerHTML cannot change the value.
Now, I put the javascript code inside the php language, the result still wrong...
My expected result:
Now I see the result:
After click the Check button to see newznum is correct but znum.innerHTML could not change this value
When pass the form to insert6.php, the total should be change the value.
Now, I cannot do it.
Anyone see the problem here?
Please you help me, I dun know where i get a mistake.
Thank you for reading my question!
I hope i will received the answer to help me to solve the problem
At first, Separation of logic is very important and what i see frmo your code is you are making DB calls from your view itself. Which shows poor way of coding. It will make it difficult to manager your code.
sel.options[0]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="FF")
echo($row[$i]);
}
}
mysql_free_result($result);
?>");
Now answer to your question:
echo "newzum =0;";
echo "for(var j=0;j <=document.getElementsByName('del').length+1;j++) {" ;
echo "for (var i=0;i<=document.getElementById('total['+i+']').innerHTML.length;i++){";
echo "newzum += parseFloat(document.getElementById('total['+i+']').innerHTML);";
echo "}";
echo "}";
echo "document.getElementById('znum').innerHTML = newzum;";
echo "</script>";
This lines of code will be executed before the page is loaded as its php.
as a result, when the page is rendered the script will not find document.getElementsByName('del') so it will throw undefined error.
To overcome this, Do those actions in onload function.
window.onload = function() {
// Inside this method put those lines. It will work.
}
Your code is too long, but I will try to answer from your title.
You must understand how php and javascript work. php is a server-side language, while javascript is a client side language. You can NOT execute php inside javascript. php does the processing on the server and then returns the results to the client. After this, the server is not aware of the client state.
In order to change the value of innerHTML, you can just use javascript. If you need to somehow interact with php, you should send AJAX calls to the server, where the php script can process and return new data.
The PHP will just echo the Javascript code, which is fine, but needlessly complex. You can just enter the Javascript code in the page.
The actual problem right now is this line:
document.getElementById('znum').innerHTML = newzum;
It is a line of Javascript in the global scope, and it will be executed as soon as the browser encounters it during the loading of the page.
But the element is should modify ('znum') is a couple of lines lower on the page:
<b id="znum">0</b>
That basically means that the element is not ready yet when the script is executed, so it cannot be found.
The solution is relatively simple: Execute the Javascript later (at the bottom) of the page, so it will execute when all relevant elements are already loaded.
Script errors like this result in an error or a warning which should be visible in the Console tab in the developer tools (F12) of your browser. So make sure you always look at that when debugging Javascript. It's really a tool you should master as a web developer. :)

Categories

Resources