How to assign value of jQuery to PHP SESSION in javascript [duplicate] - javascript

This question already has answers here:
How to assign JavaScript variable to PHP session variable?
(2 answers)
Closed 4 years ago.
This is my code I am Assigning a value to PHP but Not able to get in PHP SESSION. Tried all possible way. Also, I tried to set SESSION from javascript not get values.
var designation = $("#Designation").val();
<?php $_SESSION['lclDesignation'] = "<script type='text/javascrip'>alert(desi);</script>"?>

It's not possible to directly assign session value. using AJAX to assign the session value. Please review below code
var designation = $("#Designation").val();
$.ajax({
url: "session.php",
type: "POST",
data : {'designation' : designation },
success: function(html){
}
});
Then create the session.php file paste the below code
session.php
session_start();
$_SESSION['lclDesignation']=$_POST['designation'];

Related

Not able to transfer variables from javascript to php using ajax [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I tried sending data stored in an array(emp) using ajax and other string variables like 'gtotal' in code but data has not been transferred to the PHP file. It doesn't show any error but when I removed the isset() function it says unidentified index. I tried with different variables but no luck so far. Console function is working in HTML file and returning the values passed but data has not been collected in PHP file-like return value of $_POST shows empty array.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script >
$('#btn').click(function(){
var grandtotal=document.getElementById("gt");
var x =grandtotal.textContent;
var gtotal= String(x);;
var emp={};
emp.name=gtotal;
if(x=== '₹0'){
alert("Add to your cart")}
else{ $.ajax({
method: "POST",
url: "connect2.php",
data: emp,
success: function(data){
console.log(data);
}
});
// location.href='connect2.php'
}});
</script>
<?php
print_r($_POST);
if(isset($_POST['gtotal']))
{
$gtotal = $_POST['gtotal'];
echo $gtotal;
}
?>
The problem is that no item called gtotal is being sent in the $_POST.
Do a var_dump($_POST) to check exactly what is being sent.
You asked for an object to be sent which has an item name. If you see that in the dump then you need to use $_POST['name'] not $_POST['gtotal']

How to securely send POST data between JQuery and PHP? [duplicate]

This question already has answers here:
How to send secure AJAX requests with PHP and jQuery
(3 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
I would like to send data from a Jquery script to a php page using POST.
My code is as follows:
$.ajax({
type: "POST",
url: 'http://mywebsite.com/add_data.php',
data: {value1: some_value,
value2: 123,
value3: ABC
},
My php script captures the data and records them in MySQL database.
$myvalue1 = $_POST['value1'];
$myvalue2 = $_POST['value2'];
$myvalue3 = $_POST['value3'];
The problem is that since JS code is visible in the source code, anyone can submit anything to my database...
Is there an easy way to make it more secure and prevent this from happening?
Any advice appreciated.
All data from the client always MUST be validated and sanitized, because all data from the client can be modificated/falsificated.

How to pass a value from JavaScript in php?

Translate translator from Google. So that did not swear if something is not clear. Itself from Russia.
The question arose. How to pass the value of the alert in the javascript in the variable $ value in php, and write it in the case file. And another question: how to hide the alert? or use instead to visually it was not visible, but the value was passed?
//a lot of code
{
console.log(data);
alert(data['value']);
}
});
So. Also there is a PHP script that writes logs (current page and the previous one) to a file. According to this principle here:
//a lot of code
$home = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$referer = $_SERVER['HTTP_REFERER'];
$value = how the value of the java script to convey here?;
$lines = file($file);
while(count($lines) > $sum) array_shift($lines);
$lines[] = $home."|".$referer."|".$value."|\r\n";
file_put_contents($file, $lines);
It is necessary that the value of js is transferred to the php-script and write to the file. How to do it? Prompt please. I am a novice in all of this.
PHP scripts run before your javascript, which means that you can pass your php variables into javascript, but not the other way around. However, you can make an AJAX POST request from JavaScript to your PHP script, and grab the POST data in PHP through the global $_POST variable.
Assuming you use jQuery, your JavaScript would look something like:
// assign data object:
var data = { value: "test" };
// send it to your PHP script via AJAX POST request:
$.ajax({
type: "POST",
url: "http://your-site-url/script.php",
data: data
});
and your PHP script would look like:
// if the value was received, assign it:
if(isset($_POST['value']))
$value = $_POST['value'];
else
// do something else;

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

Pass jquery variable to php within same jquery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am beginner on coding I need help on passing jquery variable to php inside the same jquery
<script language="javascript">
$(document).ready(function(){
$("#option1).change(function(test));
$("#option2).change(function(test));
function(test){
var select1 = $("select1").val();
var select2 = $("select2").val();
var phpselect1 = '<?php $select1 = '+select1+'?>';
var phpselect2 = '<?php $select2 = '+select2+'?>';
}
});
</script>
I want to pass the the jquery variable to php in this way but I can't pass it anyway. Is there any way to pass the variable like this to php? please help me. ...
Here is an example
AJAX:
var select1 = $("select1").val();
var select2 = $("select2").val();
$.ajax({
url: 'yourphpfile.php',
data: {data1 : select1, data2 : select2},
type: 'POST',
success: function(data){
//do something with the returned data
}
});
Server-side PHP (yourphpfile.php). To assign a value passed from AJAX, do the following;
$phpselect1 = $_POST['data1']; //should be value of select1 from JS
$phpselect2 = $_POST['data2']; //should be value of select2 from JS
PHP is handled server side (step 1). Javascript is done on the client-side (step 2). You're trying to tell the page to do something on the step that has already been done.
Also, you're trying to assign PHP variables using JS? If you need data to get to PHP, use GET/POST variables and pass relevant data to the PHP upon a page load/refresh.
You are doing it wrong. You need to use Ajax to send a Get or Post request to the server. In that request you can put your variable.

Categories

Resources