Set Session variable using javascript - php

<script type="text/javascript">
function checkQuery()
{
var val = form1.proDown.options[form1.proDown.options.selectedIndex].value;
var txt = form1.proDown.options[form1.proDown.options.selectedIndex].text;
//alert(val+' | '+txt);
<?php $_SESSION['value1']= ?> = txt; <?php ; ?>
}
</script>
I have this code and it does not Work?
Any One have solution for accessing javascript variable into $_SESSION[].

I think you should use xhr(Ajax) to store your data into php session. Following is a simple example to do this
jQuery.ajax({
url: 'storesession.php',
type: 'POST',
data: {
txt: txt,
},
dataType : 'json',
success: function(data, textStatus, xhr) {
console.log(data); // do with data e.g success message
},
error: function(xhr, textStatus, errorThrown) {
console.log(textStatus.reponseText);
}
});
storesession.php
$_SESSION['value1'] = $_POST['txt'];

Related

jQuery ajax posting data to php

I am trying to post some data via ajax to php. The issue i have is the original data is pulled in via some java, like this:
var taskID = jQuery(this).attr('data-id');
var taskTitle = jQuery(this).attr('data-title');
var taskContent = jQuery(this).attr('data-content');
jQuery('.task_title').val(taskTitle);
jQuery('.task_description').val(taskContent);
When i then try and update this content in the browser (via a form), only the original data is getting posted back, and not the updated data.
Here is my ajax to post the data:
$( ".saveTaskEdit" ).click(function(event) {
var ta = $('.task_title').val();
var da = $('.task_description').val();
var taskID = $('.editTaskPanel').attr('data-id');
$.ajax({
type: "post",
url: "task-edit.php?t="+ ta + "&d=" + da + "&id=" + taskID,
contentType: "application/x-www-form-urlencoded",
success: function(responseData, textStatus, jqXHR) {
jQuery('p.status').text('Task Saved');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
})
});
Is there any reason behind this?
You set type as "post" but send data as "get"; change your ajax , update "url" add "data" like this:
$( ".saveTaskEdit" ).click(function(event) {
var ta = $('.task_title').val();
var da = $('.task_description').val();
var taskID = $('.editTaskPanel').attr('data-id');
$.ajax({
type: "post",
data: { "t":ta,"d":da,"id":taskID},
url: "task-edit.php",
contentType: "application/x-www-form-urlencoded",
success: function(responseData, textStatus, jqXHR) {
jQuery('p.status').text('Task Saved');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
})
});

how i can send a confirmation response to my API , when user click on activation link

I am trying to send a confirmation message to my API after click on activation link , I am trying lot but failed ,
http://polestarllp.com/users/useractive.php?contranumberid=23215
<script type="text/javascript">
$(document).ready(function () {
var person = "<?php $data; ?> ";
$.ajax({
url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
//type: 'Post',
data:person,
success: function (data, xhr) {
alert(data.ErrorMessage);
if(data.Success)
{
document.location.reload();
}
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
</script>
<style>
So far from what you said I have understood that you're trying to send a POST request whenever a user accesses your URL. Is that correct?
In that case try something like this:
<script type="text/javascript">
$(window).ready(function(){
$.ajax({
type: 'post',
url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
data: {'id' : 23215},
success:(function(data){
alert('Success ' + data);
}),
error:(function(data){
console.error('Error ' + data);
alert('Error has occurred');
})
});
});
</script>
This will send a POST request to http://192.168.1.102:1512/qlikapi/RegisterUser straight after when the page loads, with id parameter and its value being 23215
If you want to get the contranumberid from the URL, you can embed the PHP into your JS script like so:
<script type="text/javascript">
var contranumberid = <?php echo intval($_GET['contranumberid']);?>;
$(window).ready(function(){
$.ajax({
type: 'post',
url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
data: {'id' : contranumberid },
success:(function(data){
alert('Success ' + data);
}),
error:(function(data){
console.error('Error ' + data);
alert('Error has occurred');
})
});
});
</script>
Note that I've added a new line:
var contranumberid = <?php echo intval($_GET['contranumberid']);?>;
This will assign a JS variable to the value of the GET contranumberid parameter and the POST that value to http://192.168.1.102:1512/qlikapi/RegisterUser.
Hope this helps :)
The script you have given in the question and the actual script in the page linked in question is slightly different. The success callback function was missing closing braces. Here's a better version of your JS code:
<script type="text/javascript">
function myfunc(id) {
id = 23215;
$.ajax({
url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
type: 'post',
data: {'id' : 23215},
success: function (data, textStatus, xhr) {
alert(data.ErrorMessage);
}
});
}
</script>
Also, I didn't see any code invoking myfunc() anywhere?

how to get the value of multidimensional array pass from jquery and ajax?

I send a multidimensional array from ajax.js to test1.php. But then I do not know how to retrieve the multidimensional array in my test1.php. I have found lots of examples but they did not work for me.
ajax.js
answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';
$.ajax({
type: "POST",
url: 'test1.php',
data: {answers: answers},
//dataType: "json",
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
alert('success!');
}
});
test1.php
$a=$_POST['answers'][0]['answer_id'];
echo "$a"; //display nothing, I wish to display '32' here
Appreciate for any helps. Thanks!
Update: I included library and Ajax.js does give me an alert 'success'. So this is not the library problem. Thanks again for any helps!
try this -
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';
$.ajax({
type: "POST",
url: 'test1.php',
data: {answers: answers},
//dataType: "json",
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
alert('success!');
}
});
</script>
test1.php
<?php
$a=$_POST['answers'][0]['answer_id'];
echo "$a hello"; //display nothing, I wish to display '32' here

Receive .ajax in PHP without form

I've got this script:
<script>
var request;
$("#foo").click(function(event){
request = $.ajax({
url: "/votesHandler.php",
type: "post",
data: "true"
});
request.done(function (response, textStatus, jqXHR){
alert("Voted!");
});
request.fail(function (jqXHR, textStatus, errorThrown){
alert(
"Oops, something went wrong"
);
});
request.always(function () {
alert("Done.");
});
</script>
And what I'm trying to do is send "true" to the server when the #foo element is clicked. I've used AJAX only a few times before and each time it was with forms. The problem is that I need to receive this POST request in PHP (with $_POST['foo']) but this time the input is not the name of a text input. How do I send data into $_POST without a form?
You can specify the key of data when you send to your url.
$.ajax({
data: {foo: 'true'}
});
To retrieve it use
$_POST['foo']
Send it as:
data: 'foo_clicked=1',
or
data: 'foo_clicked=' + somevarname,
On the PHP side:
$recd = $_POST['foo_clicked'];
if ($recd == 1) {
//do what you need to do
}
try this:
$("#form").submit(function (e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
console.log(formURL); //your form action
$.ajax({
url: formURL,
type: "POST",
data: postData,
beforeSend: function () {
//do
},
success: function (data, textStatus, jqXHR) {
console.log('success data');
//whatever
},
error: function (jqXHR, textStatus, errorThrown) {
//if fails
console.log('fail');
}
});
e.preventDefault(); //STOP default action
});

How to get JSON vars from PHP script

I have a problem:
I have a JS function which sending data to php script, then PHP script returning JSON data from database QUERY and I want to get values returned from PHP script.
<script type="text/javascript">
<!--
jQuery('#wysz2').submit(function() {
var myData = {
"rodzaj_konta": jQuery('#rodzaj_konta').val(),
"miejscowosc": jQuery('#miejscowosc').val()
};
jQuery.ajax({
url: 'http://somescript.php?action=results',
type: 'GET',
data: myData,
dataType: 'json',
beforeSend: function() {
jQuery('#loading').html('<p>loading...</p><img src="loading.gif" />'); //Loading image during the Ajax Request
},
error: function(xhr, textStatus, errorThrown) {
alert("Error: " + (errorThrown ? errorThrown : xhr.status));
},
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
}
});
return false;
});
//-->​
</script>
The PHP script returning data in proper format using:
header('Content-Type: application/json');
echo json_encode($data);
When I'm trying to alert(data), I get always a null.
How to get this returned JSON data ?
EDITED:
It's strange, because I have changed sending method to POST.
PHP returning JSON:
[{"nazwa":"Test","nazwa_firmy":"Testowa","ulica":null,"numer_domy":"2A","numer_mieszkania":"11","kod_pocztowy":"00-189","miejscowosc":"Warszawa","telefon":"213-123-132","nip":"112-312-31-31","regon":"231232133","adres_www":"http:\/\/www.gogl.epl","rodzaj_uzytkownika":"serwis"}]
But my JQUERY AJAX Script still returning null.
So my script now looks like this:
<script type="text/javascript">
<!--
jQuery('#wysz2').submit(function() {
var myData = {
rodzaj_konta: jQuery('#rodzaj_konta').val(),
miejscowosc: jQuery('#miejscowosc').val()
};
jQuery.ajax({
url: 'http://somedomain.com/skrypt.php?action=wyniki_wyszukiwania',
type: 'GET',
data: myData,
dataType: 'json',
contentType: "application/json; charset=utf-8",
jsonp: "jsoncallback",
beforeSend: function() {
jQuery('#loading').html('<p>ładowanie...</p><img src="loading.gif" />');//Loading image during the Ajax Request
},
error: function (xhr, textStatus, errorThrown) {
alert("Error: " + (errorThrown ? errorThrown : xhr.status));
},
success: function (data) {
alert(JSON.stringify(data));
console.log(data);
}
});
return false;
});
//-->
</script>
Any ideas ?
you are constructing your variables while sending in a wrong way semicoluns for object names is not there according to definitions
try this
var myData = {
rodzaj_konta: jQuery('#rodzaj_konta').val(),
miejscowosc: jQuery('#miejscowosc').val()
};
and while alerting your json data try
alert(JSON.stringify(your_json_obj));
Try to alert the object of the result...
Means if json in the format {"responseCode":"001","responseMsg":"success"}
Then alert data.responseCode
In success of your ajax function try something like this
var objParams1 = $.parseJSON(data);
console.log(objParams1);
alert(objParams1.Testowa)

Categories