How to store javascript value to php variable in jqgrid? - javascript

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;
?>

Related

Can't call a jQuery Class in PHP [duplicate]

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>

How would I implement pagination through php get requests

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.

JQuery is not working properly in my wordpress

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 />";
}

Post and get in the same script (HTTP request)

I am using this code right here:
HTML code (jsonexamplecss.html):
<!DOCTYPE html>
<html>
<body>
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "adomaniname/phpselectdatawhere.php";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var s = 256;
var hr = new XMLHttpRequest();
hr.open("POST", "adomainname/phpselectdatawhere.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.send("limit="+s);
var arr = JSON.parse(response);
var i;
var i = 0;
for(i = 0; i < arr.length; i++) {
out = arr[i].name
if (out = "0 results") {
alert("0 results");
}
}
document.getElementById("id01").innerHTML = out;
alert(out);
}
</script>
</body>
</html>
PHP code(phpselectdatawhere.php):
<?php
if(isset($_POST['limit'])){
$limit = preg_replace('#[^0-9]#', '', $_POST['limit']);
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name FROM testget WHERE id='$limit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["name"];
}
} else {
echo "0 results";
}
$conn->close();
?>
I have a table named testget with id, name fields both VARCHAR. I wrote a script in order to do these things:
The HTML script sends data to the php script via http request and in this example it sends limit=256.
The PHP script gets the limit=256, puts away the number from the limit and stores $limit =256.
After that the PHP script searches if there is a name in the table whose id='$limit' and prints the name if it exists, else it echoes 0 results. The table testget has a row where id=256 and name=JohnDoe so it can't echo 0 results.
After all this procedure the HTML scripts gets the echoed stuff and alerts them with JavaScript.
So in this case I want to have JohnDoe alerted in my HTML page but unfortunately nothing shows up. Could you please help me?
Note that the url and the database connection info are correct but as you can understand I don't want to show them.

JavaScript print and AJAX save/delete from database

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.

Categories

Resources