This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 1 year ago.
I was trying to reload a div in a PHP function but it does not work.
Everything work but not the div reload:
function refresh() {
$servername = "localhost";
$username = "root";
$password = "";
$db = "wp_4lous";
$conn = new mysqli($servername, $username, $password, $db);
$checklog = mysqli_fetch_row(mysqli_query($conn, 'SELECT numero_voti FROM voti WHERE candidato="LOG"'));
if ($checklog[0] == '10') {
mysqli_query($conn, 'UPDATE voti SET numero_voti="0" WHERE candidato="LOG" AND numero_voti="10"');
echo '<script> updateDiv(); </script>';
}
};
?>
<script type="text/javascript">
function updateDiv()
{
$("#here").load(window.location.href + " #here" );
}
</script>
You can try a different approach, the way you are doing will not work. Use a php variable and assign something usefull to understand that your SQL update is done, and use that variable inside the script to do the work.
Sample code below - I have used a variable $recUpdated, and modified your code, take a look.
$recUpdated = "NO";
function refresh() {
$servername = "localhost";
$username = "root";
$password = "";
$db = "wp_4lous";
$conn = new mysqli($servername, $username, $password, $db);
$checklog = mysqli_fetch_row(mysqli_query($conn, 'SELECT numero_voti FROM voti WHERE candidato="LOG"'));
if ($checklog[0] == '10') {
mysqli_query($conn, 'UPDATE voti SET numero_voti="0" WHERE candidato="LOG" AND numero_voti="10"');
$recUpdated = "YES";
}
};
?>
<script type="text/javascript">
<?php if($recUpdated == "YES") { ?>
$("#here").load(window.location.href + " #here" );
<?php } ?>
</script>
Related
I have some code that supports pagination, but I can't make my buttons work. Can anyone help?
function setData() {
var flexContainer = document.getElementById("flex");
flexContainer.innerHTML = "<?php
foreach ($articlesarray as $seperated) {
$contentsContent = file_get_contents("../" . "$seperated[contentsname]");
echo "<div class='card'><img src='$seperated[img]'' alt='uh oh photo not found' style='width:100%''><div class='container'><h4><b>$seperated[title]</b></h4><p>$contentsContent</p></div></div>";
}
?>";
document.getElementById("back").disabled = "<?php
if ($_SERVER['REQUEST_URI'] == "/list/index.php?page=1") {
echo "true";
} else {
echo "false";
}
?>";
document.getElementById("back").style = "<?php
if ($_SERVER['REQUEST_URI'] == "/list/index.php?page=1") {
echo "display: none;";
} else {
echo "display: inline-block;";
}
?>";
}
and the php is:
$servername = "localhost";
$username = "root";
$password = "You can't have my server password";
$dbname = "myDB";
$badurl = "/list/index.php";
$newURL = "/list/index.php?page=1";
if ($_SERVER['REQUEST_URI']==$badurl) {
print "uh oh spaghettios";
header('Location: ' . $newURL);
die();
}
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$offsetAmount = $_GET["page"] * 9 - 9;
$sql = "SELECT id, title, contentsname, img FROM articles LIMIT 9 OFFSET $offsetAmount";
$result = $conn->query($sql);
$articlesarray = array();
while($row = mysqli_fetch_assoc($result)){
$articlesarray[] = $row;
}
//echo "<br><br><br> If you are reading this, you have found the debug screen. This website is under maintanence.";
mysqli_close($conn);
I can't work out how to add pagination using this system. Can anyone help? I have tried shifting the url but that only returned a 0 for some reason.
It's a GET request so in PHP I can just use
$_GET["page"] and then add or subtract 1 accordingly.
I have button which pressed update row in database based on variable:
<input type="submit" id="wyslij" name="przycisk" value="<?php echo $checkboxstatus;?>">
and I try to make ajax connection in wordpress but its not working, i tried diffrent ways but still the same results. It's even not responding while clicking the button and not receving error.
Ajax:
jQuery(document).ready( function() {
jQuery("#wyslij").click( function(e) {
e.preventDefault();
checkboxstatus = <?php echo $checkboxstatus; ?>
jQuery.ajax({
type : "post",
url : czekboks.php,
data : {checkboxstatus},
})
})
})
</script>
czekboks.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "obejrzaneodcinki";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$query = " UPDATE uzytkownik
SET status = '$checkboxstatus'
WHERE id = '2115' ";
if(mysqli_query($conn,$query))
{
echo "good job";
echo "<br />";
}
I got the row id value in my script but assign to php variable it shows object object..
How can do this?
I refer many answer but nothing will happen , I used document.write() function , its not working..
<script>
function edit()
{
// alert("hi");
var id = jQuery("#list_records").jqGrid('getGridParam','selrow');
if (id) {
var ret = jQuery("#list_records").jqGrid('getRowData',id);
var sampleedit = ret.id;
document.cookie = "myJavascriptVar = " + sampleedit
alert(sampleedit);
}
else {
alert("Please select row");
}
}
</script>
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "dbnew";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$myPhpVar = $_COOKIE['myJavascriptVar'];
echo $myPhpVar;
$sqlselect = "SELECT * FROM userregisterdetails ";
echo $sqlselect;
?>
I'm trying to create a JSON callback. I got two files, json.html and json.php. Also, I've a database with like this:
Type: MySQL
DB Name: user_table
Table name: customers
Fields: id, name, product, supplier
Codes of my json.html is:
<html>
<head>
</head>
<body>
<div id="demo" style="font-size: 20px;"></div>
<script>
obj = { "table":"customers", "limit":10 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
for (x in myObj) {
txt += myObj[x] + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
};
xmlhttp.open("POST", "json.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
</script>
</body>
</html>
And here is the codes of json.php:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";
header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_POST["x"], false);
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>
Here is the error log reports:
PHP Notice: Undefined variable: table in /home/user/public_html/json/json.php on line 7
PHP Fatal error: Cannot access empty property in /home/user/public_html/json/json.php on line 7
PHP Notice: Undefined index: x in /home/user/public_html/json/json.php on line 4
PHP Notice: Undefined variable: table in /home/user/public_html/json/json.php on line 7
PHP Notice: Trying to get property of non-object in /home/user/public_html/json/json.php on line 7
PHP Notice: Undefined variable: limit in /home/user/public_html/json/json.php on line 7
PHP Notice: Trying to get property of non-object in /home/user/public_html/json/json.php on line 7
PHP Fatal error: Call to a member function fetch_all() on a non-object in /home/user/public_html/json/json.php on line 9
How can I make it work?
As mentioned in a comment to David, the problem was with fetch_all(). I guess what was making the problem is the server resources because the page returned 500 on call.
In any case I retrieved the required array using this method instead:
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT * FROM customers LIMIT 10");
$outp = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$outp[] = $result->fetch_array(MYSQLI_ASSOC);
}
echo json_encode($outp);
And it worked.
Now, I'm going to make it work with my JSON callback.
Try replacing
$result = $conn->query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit);
with
$result = $conn->query("SELECT name FROM ".$obj->{'table'}." LIMIT ".$obj->{'limit'});
Try like this...
In your javascript send object..
xmlhttp.send(obj);
In PHP:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";
header("Content-Type: application/json; charset=UTF-8");
extract($_POST); //Extracting
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query("SELECT name FROM ".$table." LIMIT ".$limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
print_r($outp);
echo json_encode($outp);
?>
Looks like the JSON you are trying to send is not making it to the PHP script.
Some things I would do are:
Change the request header content type to application/json (see this https://stackoverflow.com/a/9880122/2315558)
Check that x has a value before trying to use it
I have created this page to get data from the database, with links to print the shown data and delete it afterwards.
One of the problems is that the JavaScript print function window.print(); wont work.
Another problem is that after printing the page, I would like to update the database, so people can see that it has been printed before.
Alternatively, the function could also print the page and then immediately deletes data, so people won't need to see if it has been printed before or not.
This is the code for getting the data from the database:
<html>
<header>
<script>
function print_table(id)
{
//print your document
window.print();
//send your data
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://localhost/Stage/printed_table.php" + id;
xmlhttp.send();
}
</script>
</header>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Query the database
$resultSet = $conn->query("SELECT * FROM orders");
// Count the returned rows
if($resultSet->num_rows != 0){
// Turn the results into an Array
while($rows = $resultSet->fetch_assoc())
{
$id = $rows['id'];
$naam = $rows['naam'];
$achternaam = $rows['achternaam'];
$email = $rows['email'];
$telefoon = $rows['telefoon'];
$bestelling = $rows['bestelling'];
echo "<p>Name: $naam $achternaam<br />Email: $email<br />Telefoon: $telefoon<br /> Bestelling: $bestelling<br /> <a href='delete.php?del=$id'>Delete</a> <input type='button' onclick='print_table($id)' value='Print Table' /> </p>";
}
// Display the results
}else{
echo "Geen bestellingen";
}
?>
</body>
</html>
and these are the pages for the two server-side functions:
delete.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";
// Get ID
$id = $_GET['del'];
$sql= "DELETE FROM orders WHERE id=" . $id . "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Query the database
$resultSet = $conn->query($sql) or die("Failed".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Stage%201/bestellingen.php'>";
?>
print_table.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Query the database
$id = $_GET['id'];
$resultSet = $conn->query("UPDATE `orders` SET `printed` = 1 WHERE `id` = `$id`");
?>
You should really check your browser console (F12) to see if there are any JavaScript errors.
One really glaring error I could spot is this line, where the brackets aren't closed. These type of errors could be easily fixed just by checking the console first.
Another error is the variable in the string, it should be sent as a ?key=value pair.
xmlhttp.open("GET","http://localhost/Stage/printed_table.php" + id;
should be:
xmlhttp.open("GET","http://localhost/Stage/printed_table.php?id=" + id, true);
Another problem would be the URL the above line is calling. I notice you mentioned that your PHP file name is called print_table.php instead of printed_table.php.