Deleting a row within SQL database webpage [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
My aim is for a user to click a button within a row to delete that row within the database.
The button is declared as so:
<td><button class="btn btn-default" onclick="removeStudent()" button id= <?php echo $productDetails["studentID"]; ?> type="button">Delete</button></td>
The remove function is as follows:
<script>
function removeStudent() {
$id = window.event.target.id.toString();
$sql = "DELETE FROM tableStudent WHERE studentID= '$id'" ;
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error deleting";
}
}
</script>
The webpage is connected to the database, and variable names are correct and working within other functions. Just delete function not working. Any help would be much appreciated.

It looks like you are mixing javascript and PHP here. I'm not sure you understand how PHP works. Any PHP code on your page is run before the page is ever sent to the user, so you can't put PHP stuff inside javascript function (well you can, but it will be run and turned into plaintext before the javascript is ever even read by the user's browser.) So your line where you go $id = window.event.target.id.toString(); won't work, since you are trying to set a PHP variable using javascript. Actually none of that code will do anything, since its not inside a <?php block. It will just cause errors in the browser console because it is invalid javascript code.
You need to make your javascript function make an ajax call to another PHP page, which actually does the delete

Related

how to use a php variable in javascript value in mysql query? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
Actually, I have a javascript variable I pass this to PHP variable and use this variable as a MySQL query when I submit the query the page gets reloaded and value of the javascript variable is finished and therefore query doesn't work
I just want a modification in my code or there is another relevant solution regarding my problem then kindly please help me.
Everything works fine when echo the PHP variable it shows me the variable only problem is in the query of my SQL that in the query the PHP variable which has javascript variable is not working.
<script>
var buttontext="esteem";
<?php
$ff ="<script>document.write(buttontext);</script>";
?>
</script>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="beelist";
$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'beelist');
if($_POST['sub'])
{
echo $ff;
$code=$_POST['Bid'];
if($code!=""){
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '$ff'";
$data = mysqli_query($conn,$query);
$res1=mysqli_fetch_array($data);
if ($res1) {
echo '<script> alert("Beacon found")</script>';
echo '<script> showp();</script>';
}
else {
echo '<script> alert("Beacon ID is wrong!")</script>';}
}else{
echo '<script> alert("Beacon ID is required")</script>';
}
}
?>
As I said in the comments
Where do I start, Client and Server (JS and PHP) are separate. One runs on the server one runs on the clients computer. PHP goes first and as such only PHP can affect JS (as that is part of the output of the PHP) the JS's state cannot be known in PHP as it's on a entirely different computer. Basically you are left with making a request, either from a page reload (such as form submission) or AJAX where you can pass that data back to the server so it can work on it.
Basically what you have now is $ff literally is this text:
$ff ="<script>document.write(buttontext);</script>";
And because you don't echo it, it's actually never passed to the Client as part of the source.
Your query is like this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '<script>document.write(buttontext);</script>'";
It's too broad of a topic for me to really give you a working answer, and there are plenty of tutorials out there that can do it better justice them me. But hopefully, you understand what is going on now.
PS. you can test this easily by doing echo $query; right after the query. Also be aware you should not put PHP variables directly in SQL, or you risk SQLInjection type attacks against your site. For example if $_POST['Bid']="' OR 1 -- your query would be this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '' OR 1 --' && name = '<script>document.write(buttontext);</script>'";
Where -- is the start of a comment in MySQL. So now I just selected your entire table by injecting SQL commands into your Query, which is a very bad thing.
Cheers!

Passing PHP into jQuery [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have an idea how to pass PHP value into a jQuery variable, I have done it in the past and it worked. But I tried the below code but for some reason it not working. I dont know where the problem lies.
I am trying to hide a custom virtuemart drop down cart I designed when the cart is empty.
jQuery(document).ready(function ($) {
var cart = "<?php if ($data->totalProduct) echo $data->cart_show; ?>"
jQuery('.btn-cart1').hover(if (cart === "") {
jQuery(this).find('.dropdown').hide();
}
else {
jQuery(this).find('.dropdown').hide();
jQuery('.btn-cart1').hover(
function() { jQuery(this).find('.dropdown').stop().show(500)},
function() { jQuery(this).find('.dropdown').stop().hide(500)})
}
});
PHP is server side scripting language and is executed well before your javascript executes on web browser.
The correct execution of your code depends on the values of $data->totalProduct & $data->cart_show after the PHP has been executed on your server side and the values are places in your javascript.
It is not a good programming practice and I would strongly suggest you to use AJAX instead to access the values of PHP variables.

How to receive data in php sent from javascript [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I am sending some data from my pain page to app.php using javascript
$(document).on('click', '.app' , function(){
var data=$(this).attr("id");
window.location="application/app.php?data="+data; //data send in this statement
});
my php code is this :
<?php
if($_GET['data'])
{
echo $app_id=$_GET["data"]; receiving data
}
?>
It works sometime but now it is giving Notice in an alert box: Undefined index data in app.php. But it is showing the echo.
I want to know how I can receive the data I need using $_POST or $_GET. like we receive by making an Ajax call. But I haven't specified something like this in Javascript. is there any way to this?
I want to clarify that the data exists and it is non empty because echo is successfully printing that on the page
Try this:
<?php
if(!empty($_GET['data']))
{
$app_id = $_GET['data']; // receiving data
}
?>
Please be aware that passing data without validation is not secure. Reccomended reading: https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project
use isset() to first check if the key exists. data won't exist in $_GET when it's not passed via a parameter in the url
you probably want some logic that looks like this:
if (isset($_GET['data'])) {
if (!empty($_GET['data'])) {
$appId = $_GET['data'];
}
else {
// data exists, but it's empty
}
}
else {
// data does not exist
}
and maybe you even want to check if it's a valid id, like being numeric or something

How to call php codes inside javascript [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to check the inputs of a form if they are empty and then i want to use php codes for user registeration.
<script>
$('#submitbutton').click(function(){
if($('#usernameinput').val() == ''){
alert('Input is blank');
}
else {
//here i want to use php codes for example;
$bgl = new mysqli("localhost", "root", "", "users");
if ($bgl->connect_errno) {
echo "Failed to connect to MySQL: (" . $bgl->connect_errno . ") " . $bgl->connect_error;
}
$username = mysql_real_escape_string($_POST['username']);
.....
.....
..... /and so on..
}
});
</script>
In this case you most likely want to use $.ajax() to call a PHP page that supplies this information.
jQuery.ajax()
There is no such thing as php and javascript working together like that. Javascript in run on client side and php is run on server side. The best you can to is to pass and retrieve values to and from a php page and then use those values as intended.
You have a few options here. The first is to post the form to a PHP page after the JavaScript validations (just use the form action) or use Ajax and call the PHP page, as Robin says.
That's not posible, you can embed javascript into php code but not vice versa.
The best way to implement that is developing a REST service and retrieving data with AJAX.

Echo variable from javascript prompt [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am struggling to get the following code to work. I want to take input from a javascript prompt, and output it using a php echo. When I try this I get an echo with the random numbers "12313" for no apparent reason. This is my code:
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>'
, '<script type="text/javascript">'
, 'var code = prompt("Enter verification code", "");'
, 'var getcode = code;'
, '$.post("wp-members-register.php", { code: getcode }); </script>';
$buffer_data['code'] = $_POST['code'];
echo $buffer_data['code'];
I am very new to php so please bear with me. Perhaps I am not correctly posting the 'code' variable?
EDIT: Maybe somebody can show me a better way of inputting a text string and getting it in the php code to follow? Note I am working with the wp-members wordpress plugin in the wp-members-register.php file.
Outputting javascript using PHP doesn't make any sense as javascript is a client side scripting which is executed after loading the DOM elements. PHP code is triggered when the request is sent from the browser to the server.
PHP is called before the JavaScript and cannot be called after.
You have to do everything you have to do in PHP before you use any JavaScript.
On thing you can do is use AJAX.
<?php
//php code here before the javascript
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var code = prompt("Enter verification code", "");
$.get( "wp-members-register.php?code=code", function( data ) {
document.write("Data Loaded: " + data);
});
</script>

Categories

Resources