I want to pass the php variable to the next page.
I tried many things, but I did not succeed.
Here is the attached case I tried.
index code
<script>
<?php
$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");
$result = mysqli_query($con, "select * from StoreTable");
$n = 1;
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
?>
positions_1.push({ content:
'<div class="wrap">' +
' <div>next page</div>' +
'</div>'
});
<?
$n++;
}
?>
</script>
next code
<?php
echo $name;
?>
url has been changed to a variable, but a blank screen is displayed. (Variables were not passed to the next page.)
Try this --
<script>
<?php
$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");
$result = mysqli_query($con, "select * from StoreTable");
$n = 1;
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
?>
positions_1.push({ content:
'<div class="wrap">' +
' <div>next page</div>' +
'</div>'
});
<?
$n++;
}
?>
</script>
Related
I have a program that displays authors book code and book title using php and
AJAX technology, but for some reason the data is not appearing in the table. I know my SQL code is correct as our instructor gave us the code for that, but something is preventing the data from appearing in the table. Any tips or suggestions would be appreciated!
<body>
<?php
$authorid = 0;
$authorid = (int) $_GET['authorid'];
if ($authorid > 0) {
require_once('dbtest.php');
$query = "SELECT * FROM author";
$r = mysqli_query($dbc, $query);
if (mysqli_num_rows($r) > 0) {
$row = mysqli_fetch_array($r);
} else {
echo "Title Not Returned<br>";
}
echo "<table border='1'><caption>Titles for </caption>";
echo "<tr>";
echo "<th>Book Code</th>";
echo "<th>Book Title</th>";
echo "</tr>";
$q2 ="SELECT wrote.author_number As ANo, wrote.book_code As BookCd, book.book_title As Title ";
$q2 .= " FROM wrote, book ";
$q2 .= " WHERE wrote.book_code=book.book_code ";
$q2 .= " AND wrote.author_number = ' ' ";
$q2 .= " ORDER BY book.book_title";
$r2 = mysqli_query($dbc, $q2);
$row = mysqli_fetch_array($r2);
while ($row) {
echo "<tr>";
echo "<td>" .$row['BookCd']. "</td>";
echo "<td>" .$row['Title']. "</td>";
echo "</tr>";
$row = mysqli_fetch_array($r2);
}
echo "</table>";
} else {
echo "<p>No Author ID from prior page</p>";
}
?>
</form>
</body>
The suspicious line is: AND wrote.author_number = ' '
Why is it empty?
Put a check after the second query:
$r2 = mysqli_query($dbc, $q2);
if (mysqli_num_rows($r2) > 0) {
echo "rows are Returned<br>";
} else {
echo "rows are Not Returned<br>";
}
$row = mysqli_fetch_array($r2);
I want to use query and query2 two different select queries together.
How to use it and execute?
Here is my code
$query2 = "SELECT advance FROM transaction WHERE adv_date=now()";
$query = "SELECT SUM(s.total) AS totalamount FROM sale s JOIN product p ON ( s.pr_id = p.pr_id ) WHERE s.customer_name = '$customer'";
$result = mysqli_query($connect, $query);
$result2 = mysqli_query($connect, $query2);
$output = '';
if (mysqli_num_rows($result) > 0) {
$i = 0;
while ($row = mysqli_fetch_array($result)) {
$output .= '<h4 style="display: inline;" align="right"> ' . $row["totalamount"] . '</h4><br> ';
$output .= ' ';
$i++;
}
} else {
$output .= '<tr>
<td colspan="5">No Order Found</td>
</tr>';
}
$output .= '<h4 style="display:inline;" align="right"> ' . $row["total"] . '</h4><br> ';
echo $output; ?>
try this one it will help you
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";
// Execute multi query
if (mysqli_multi_query($con,$sql))
{
do
{
// Store first result set
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf("%s\n",$row[0]);
}
// Free result set
mysqli_free_result($result);
}
}
while (mysqli_next_result($con));
}
I'm not sure if i understood your question right, but i created a function to execute both of your queries, by passing them through the parameter of the function.
function commitQuery($query){
$result = mysqli_query($connect, $query);
$output = '';
if(mysqli_num_rows($result) > 0) {
$i=0;
while($row = mysqli_fetch_array($result)) {
$output .= '<h4 style="display:inline;" align="right"> '. $row["totalamount"] .'</h4><br>';
$output .= '';
$i++;
}
} else {
$output .= '
<tr>
<td colspan="5">No Order Found</td>
</tr>';
}
$output .= '<h4 style="display:inline;" align="right"> '. $row["total"] .'</h4><br>';
echo $output;
}
}
commitQuery("SELECT advance FROM transaction WHERE adv_date=now()");
commitQuery("SELECT SUM(s.total) AS totalamount FROM sale s JOIN product p ON ( s.pr_id = p.pr_id ) WHERE s.customer_name = '$customer'");
i have index.html with the following code
<html>
<head><title>Testing</title></head>
<body>
<script>
var javascriptVariable = "abc";
window.location.href = "test_selected.php?name=" + javascriptVariable;
</script>
</body>
</html>
and test_selected file containing the code
<?php
$host = 'localhost';
$port = '5432';
$database = 'postgis';
$user = 'postgres';
$password = 'postgres';
$reg_name=$_GET['name'];
$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database .
' user=' . $user . ' password=' . $password;
$link = pg_connect ($connectString);
if (!$link)
{
die('Error: Could not connect: ' . pg_last_error());
}
$query="select * from table where name='{$reg_name}'";
$result = pg_query($query);
$i = 0;
echo '<html><body><table border="1"><tr>';
while ($i < pg_num_fields($result))
{
$fieldName = pg_field_name($result, $i);
echo '<td>' . $fieldName . '</td>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
while ($row = pg_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
pg_free_result($result);
echo '</table></body></html>';
?>
The output is displayed in a new page because i have used windows.location. Is it possible to display the output in the same page in a div instead of another new page.
include jquery in page. and replace
window.location.href = "test_selected.php?name=" + javascriptVariable;
with
$.ajax({
type:'GET'
url: "test_selected.php?name=" + javascriptVariable,
success: function(result){
$("#div_id").html(result);
},error:(error){
console.log(error);
});
replace div_id with the id of div you want to load into.
EDIT: SOLVED.
The 'items' table has multiple tuples, and for each of them I used to have name,image,price,description and a bid system (a text field and a submit) to be printed on a webpage.
It worked wondefully until I decided I wanted each of them to have a countdown timer too.
This timer works this way: every time there's a new bid on an item, the item's timer resets to X minutes/seconds/whatever.
When I insert include.php into the original code, only the first instance is processed.
I know this is due to PHP and Javascript not 'running' at the same time, but I wonder how can I fix my Javascript to have the problem sorted out.
The classic echo solution doesn't work.
this is the while loop:
while ($row = # mysql_fetch_array($results))
{
print
"<div id='item'>".
"<p>".$row["name"]."</p>".
"<img src= uploads/".$row["image"].">".
"<p>€".$row["final_price"]."</p>".
"<p>".$row["description"]."</p>".
"<form method = 'get' action = 'sum.php'>".
"<input type = 'hidden' name = 'itid' value = ".$row["id"].">".
"<tr><td><input type = 'number' name = 'bid' required = 'required'></td></tr><br>".
"<tr><td><input type = 'submit' value = 'Bid'></td></tr>".
"</form>";
include('cntdwn.php');
print
"</div>";
}
SOLVED by setting cntdwn.php this way:
<?php
${"query1".$row['id']}= "SELECT time_up FROM items WHERE id = '" .$row["id"]."'";
${"results".$row['id']} = mysql_query (${"query1".$row['id']});
while (${"row".$row['id']} = # mysql_fetch_array(${"results".$row['id']})) {${"rez".$row['id']} = ${"row".$row['id']}['time_up'];}
//$date = 'January 22 2017 09:10:00 PM CET';
${"exp_date".$row['id']} = strtotime(${"rez".$row['id']});
${"now".$row['id']} = time();
if (${"now".$row['id']} < ${"exp_date".$row['id']}) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end<?php echo $row['id']; ?> = <?php echo ${"exp_date".$row['id']}; ?> * 1000;
var server_now<?php echo $row['id']; ?> = <?php echo time(); ?> * 1000;
var client_now<?php echo $row['id']; ?> = new Date().getTime();
var end<?php echo $row['id']; ?> = server_end<?php echo $row['id']; ?> - server_now<?php echo $row['id']; ?> + client_now<?php echo $row['id']; ?>; // this is the real end time
var _second<?php echo $row['id']; ?> = 1000;
var _minute<?php echo $row['id']; ?> = _second<?php echo $row['id']; ?> * 60;
var _hour<?php echo $row['id']; ?> = _minute<?php echo $row['id']; ?> * 60;
var _day<?php echo $row['id']; ?> = _hour<?php echo $row['id']; ?> *24;
var timer<?php echo $row['id']; ?>;
function showRemaining<?php echo $row['id']; ?>()
{
var now<?php echo $row['id']; ?> = new Date();
var distance<?php echo $row['id']; ?> = end<?php echo $row['id']; ?> - now<?php echo $row['id']; ?>;
if (distance<?php echo $row['id']; ?> < 0 ) {
clearInterval( timer<?php echo $row['id']; ?> );
location.reload();
return;
}
var days<?php echo $row['id']; ?> = Math.floor(distance<?php echo $row['id']; ?> / _day<?php echo $row['id']; ?>);
var hours<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _day<?php echo $row['id']; ?> ) / _hour<?php echo $row['id']; ?> );
var minutes<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _hour<?php echo $row['id']; ?>) / _minute<?php echo $row['id']; ?> );
var seconds<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _minute<?php echo $row['id']; ?>) / _second<?php echo $row['id']; ?> );
var countdown<?php echo $row['id']; ?> = document.getElementById('countdown<?php echo $row['id']; ?>');
countdown<?php echo $row['id']; ?>.innerHTML = '';
if (days<?php echo $row['id']; ?>) {
countdown<?php echo $row['id']; ?>.innerHTML += 'Days: ' + days<?php echo $row['id']; ?> + '<br />';
}
countdown<?php echo $row['id']; ?>.innerHTML += 'Hours: ' + hours<?php echo $row['id']; ?>+ '<br />';
countdown<?php echo $row['id']; ?>.innerHTML += 'Minutes: ' + minutes<?php echo $row['id']; ?>+ '<br />';
countdown<?php echo $row['id']; ?>.innerHTML += 'Seconds: ' + seconds<?php echo $row['id']; ?>+ '<br />';
}
timer<?php echo $row['id']; ?> = setInterval(showRemaining<?php echo $row['id']; ?>, 1000);
</script>
<?php
}
else {
${"winnerquery".$row['id']}= "SELECT u_mail FROM bids WHERE datestamp = (SELECT MAX(datestamp) FROM bids) AND i_id = '".$row['id']."' LIMIT 1";
${"rezul".$row['id']} = mysql_query(${"winnerquery".$row['id']});
while (${"rol".$row['id']} = mysql_fetch_array(${"rezul".$row['id']})) { ${"rel".$row['id']} = ${"rol".$row['id']}['u_mail'];}
${"relative".$row['id']} = mysql_query("UPDATE items SET sold = '1',sold_to = '".${"rel".$row['id']}."' WHERE id = '".$row['id']."'");
echo "Times Up";
}
?>
<div id="countdown<?php echo $row['id']; ?>"></div>
<?php
include_once("db.php");
$result=mysql_query("SELECT * FROM stu WHERE receiver='DM4'");
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ptype'] . "</td>";
echo "<td>" . $row['source'] . "</td>";
echo "<td>" . $row['letterno'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['descrip'] . "</td>";
echo "<td>" . $row['receiver'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td><a href='vex.php?uid={$row['letterno']}' id='id' onClick='addfavourite()'>.{$row['title']}.</a></td>";
//echo "<td><a href='update.php?id={$row['id']}'>Update</a></td>";
echo"<td><img style='width:100px;higth:150px;' src='upload/{$row[image]}'></td>";
addfavourite();
echo addfavourite();
function addfavourite() {
$ide=$_GET['uid'];
//echo $ide;
$sql = "SELECT * FROM stu WHERE letterno = '$ide'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if($row){
$newfav = "UPDATE stu SET open = 1 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);}
else{
$newfav = "UPDATE stu SET open = 0 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);}
}
}
echo $ide;
?>
This is my error code.In this code,there is a fault with mysql query statement.it is not supported with the databse. but onclick() function is working.
You cannot directly execute a PHP function from a JavaScript event. Simply, cannot mix server side and client side logic but you can make two of them interact with each other.
In this case, you can create a JavaScript function which sends an AJAX request to a PHP page on your server containing the code of addfavourite().
A Demo
I haven't tested the code below but assuming that your code logic and rest stuff is correct, this might just work
demo.php
<script>
function addfavourite(uid){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
var url = "addfavourite.php"; // URL of your PHP file
var vars = "uid="+uid;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
<?php
include_once("db.php");
$result=mysql_query("SELECT * FROM stu WHERE receiver='DM4'");
echo "<table>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ptype'] . "</td>";
echo "<td>" . $row['source'] . "</td>";
echo "<td>" . $row['letterno'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['descrip'] . "</td>";
echo "<td>" . $row['receiver'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td><a href='#' id='id' onClick='addfavourite({$row['letterno']})'>.{$row['title']}.</a></td>";
echo"<td><img style='width:100px;higth:150px;' src='upload/{$row[image]}'></td>";
}
echo "</table>";
?>
<div id='status'></div>
addfavourite.php
<?php
if(!isset($_POST['uid'])) {
$ide = $_POST['uid'];
$sql = "SELECT * FROM stu WHERE letterno = '$ide'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row) {
$newfav = "UPDATE stu SET open = 1 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);
} else {
$newfav = "UPDATE stu SET open = 0 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);
}
if ($createfav) {
echo "<script>alert('Added to favourite successfully...')";
} else {
echo "<script>alert('Something went wrong...')";
}
}else{
echo "POST variable not set!";
}
?>
Some notes
You should use mysqli insted of mysql functions as it is deprecated and no longer supported
This here is a good video tutorial on AJAX https://www.developphp.com/video/JavaScript/Ajax-Post-to-PHP-File-XMLHttpRequest-Object-Return-Data-Tutorial