JQuery is not working properly in my wordpress - javascript

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

Related

Display data from MYSQL into text field using AJAX and PHP

I want to display the array data that I have called in getword.php into the text field in index.php using AJAX. but how ?
this is the index.php
<body>
<div class="container" >
<h2>View data</h2>
<h4>Word List : </h4>
<div class="form-group">
<input id="wordlist" type="text" class="form-control" name="wordlist">
</div><br>
<button id="display" title="Generate Word">Generate</button>
<div class="input-single">
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#display").click(function() {
$.ajax({
type: "POST",
url: "getword.php",
dataType: "json",
success: function($result){
$('#wordlist').val('');
}
});
});
})
</script>
and this is the sql query to get data from database into array (getword.php)
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "wordlist";
$con = mysqli_connect($host, $user, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "select kata from word";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
$result[]= $row[0];
}
echo json_encode(array('kata'=>$result));
mysqli_close($con);
?>
I would suggest you write PHP code as below it will be lot more simple
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "wordlist";
$con = mysqli_connect($host, $user, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT kata FROM word";
$res = $con->query($sql);
while($row = mysqli_fetch_array($res)){
$dataToSend = $row[0];
}
mysqli_close($con);
echo json_encode($dataToSend);
?>
Now the script be like this to place the result in the text box
$.ajax({
type: "POST",
url: "getword.php",
dataType: "json",
success: function($result) {
var val = JSON.parse($result);
console.log(val); // see console in browser to check what is sent back
var txtBox = docuent.getElementById('wordlist');
txtBox.value = val;
}
});
Here I guess the data from the PHP file will be a single value since you are using a single text box. if multiple values are there then use an array to store data in PHP and send it in json_encode() itself.
hope that this becomes helpful to you.

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.

PHP/JS: cannot get

All I want to do is print 'win!' if they log in with their details in the Database (working correctly) and 'loss' if for some reason their info was not found in the DB.
So my issue is that for some reason my line of code 'echo $email;' doesn't work. It seems be set to NULL.
At the moment it only ever prints 'loss' regardless what i enter, but, if I add a row in the database that has a blank email and password (email = "", password="") then the php script returns 'win!'.
PHP CODE:
<?php
// echo "php test";
//server info
$servername = "localhost";
$username = "root";
$dbpassword = "root";
$dbname = "personal_data";
//Establish server connection
$conn = new mysqli($servername, $username, $dbpassword, $dbname);
//Check connection for failure
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//Read in email & password
echo "reading in email & password...";
$email = mysqli_real_escape_string($conn, $_POST['email1']);
$password = mysqli_real_escape_string($conn, $_POST['password1']);
echo $email; //this prints blank
echo $password; //this also prints blank
$sql = "SELECT Name FROM personal_data WHERE Email='$email' AND Password='$password' LIMIT 1";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
echo "win!!";
} else {
echo "loss";
}
mysqli_close($conn);
?>
JS CODE:
$(document).ready(function(){
// alert("js working");
$('#login_button').click(function(){
var email = $('#email').val(); //prints the correct value
var password = $('#password').val(); //prints the correct value
var dataString = 'email1=' + email
+ '&password1=' + password;
$.ajax({
type: "POST",
url: "http://localhost:8888/php/login.php",
data: dataString, //posts to PHP script
success: success()
});
});//eo login_button
function success(){
alert("success");
}
});//eof
Apart from the fact that that is completely, insanely useless and with no security whatsoever, you can just exchange $.ajax() for $.post() and do like this:
var loginEmail = $('#email').val();
var loginPassword = $('#password').val();
$.post('login.php',{email:loginEmail,password1:loginPassword},function(data) {
console.log(data);
})

jquery calling select box, how to select that and use it for later

I have this function which shows options dropdown box. The box works and it whows users but when i select a user and then want to use it for further i dont know how. Any help here would be much apritiated.
After this code i have some more entries with user adds via keybord and then a submit button. All this data is then saved to database.
At this moment all data is corectlly saved exept this "Ime in priimek" which i am reading from php
Ime in priimek: <select id="ime_priimek" name="ime_priimek" onchange="myFunction() >
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smartronik";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, "SELECT ime_priimek FROM smart_zaposleni");
while ($row = mysqli_fetch_array($result)){
echo "<option value=\"rezultat\">" . $row['ime_priimek'] . "</option>";
}
?>
</select>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("ime_priimek").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

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