I really dont know why this isnt working!
<script src="js/jquery.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
function updateVAL()
{
var f = document.getElementById("nu");
var val=f.value;
alert(val); // it displays the value properly
$.post("getDATA.php", {id: val}); // I sent the variable with jquery
}
</script>
getDATA.php
$value=$_POST['id'];
echo $value;
and when I access getDATA.php to see if it was sent I get this:
Notice: Undefined index: id in C:\Users\dan...
why the variable 'id' isnt set ? why is isnt passed to the server ?
Any help would be appreciated :)
Cheers,
dan.
Parameters are being sent via ajax - so this js script calls getDATA.php and $_POST['id'] is "seen" there only at that time.
And you are trying to access getDATA.php after and send no post or get parameters by your briwser - so you don't see this params there.
You have to catch echo by your js script. Look at this demo:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
Related
i have a page in php to execute query. when i need the parameter from another page with session using jquery. so, how to set session in php from session in jquery. please check my code and give me solution. thanks...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.ciphertrick.com/demo/jquerysession/js/jquerysession.js?d56ac9"></script>
<script>
$(document).ready(function(){
var sDecode = $.session.get("sesDecode");
alert(sDecode);
});
</script>
<?php
include('connection.php');
$content= ==> how to get " sDecode " ???
$upd = mysql_query("UPDATE t_modules set content='$content' where id_t_modules='3'") or die(mysql_error());
?>
You can use ajax to send parameter to a php file like this
$.ajax({
url: "connection.php",
data: "Session="+$.session.get("sesDecode"),
type: "POST",
dataType: 'json',
success: function (e) {
console.log(JSON.stringify(e));
},
error:function(e){
console.log(JSON.stringify(e));
}
});
Note that both solutions needs you to check the input since the user could potentially modify it.
Using Ajax
The best way to get sDecode is probably to send it via jQuery.post() (AJAX) to a PHP page that will register the session value (after doing the appropriate checks to it).
$.post('session.php', { d: sDecode }, function (response) {
alert(response);
});
and you get it in PHP via :
$sDecode = $_POST["d"]; // Please test the input here!
You might have to use .serialize() and then unserialize it in PHP depending on the content and how you treat it.
Using Redirection
You can simply redirect to a page and transmit it directly as a GET value:
window.location.href=”session.php?d="+sDecode;
Then on the session.php page you would read it using this code :
$sDecode = $_GET["d"]; // Please test the input here!
You can have PHP fill in the Javascript variable from the session variable.
<?php session_start(); ?>
<html>
...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
var sDecode = <?php echo json_encode($_SESSION['sDecode']); ?>;
alert(sDecode);
</script>
PHP execute before Jquery so use ajax after get the parameter from session to do your query
<form>
<input type="text" id="user"/>
<input type="button" value="Submit" onClick="post();" />
</form>
<div id="result"> </div>
<script type="text/javascript">
function post()
{
var username = $('#user').val();
$.post('battlephp.php',
{postuser:user}
)
}
</script>
Its a simple Ajax code.. It should take username and display the Php code!
But don't know why its not running?? Actually I am learning...so I cant rectify the error or fault??
I am running ii on localhost.. so is there any problem with using:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
display the Php code
No, it shouldn't.
First, you've changed your mind about the variable name you are using (user, username) half way through your script, so you are going to throw a reference error.
Second, you haven't provided a function (the third argument) to $.post, so you aren't doing anything (such as displaying it) with the returned data.
Third, the server should execute the PHP and return its output. You shouldn't get the actual PHP code.
function post() {
var username = $('#user').val();
$.post(
'battlephp.php',
{postuser:username}, // Be consistent about your variable names
function (data) {
alert(data);
}
);
}
Instead
document.ready()
you can use
jQuery(function($){...});
Try to do this:
<script>
$(document).ready(function() {
function post() {
var username = $('#user').val();
$.ajax({
type : 'post',
url : 'batttlephp.php',
data : {
postuser : user
},
success : function(data) {
alert(data);
},
error : function(data) {
alert(data);
}
});
});
});
</script>
if you're doing a ajax request then is good also handle success and error...
Also I suggest to you "to start the document".
Try the code above and let us know if worked
So the issue I'm currently having is that I'm trying to use an ajax call to send information to a php page to create an entry within a SQL database.
If I have the code within the tag. (So it's not within a function and it just calls on page load).
This is what is imported:
<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<link rel="stylesheet" href="jquery.mobile-1.3.0.css" />
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=visualization"></script>
<script type="text/javascript">
var pos = "(44.18974214877667,-79.63154019848633)";
var place = "test";
var check = "on";
var priv = "yes";
$.ajax({ type:"POST",
url:"backMap/locationCreation.php",
data: { name: place, loc: pos, check: check, privacy: priv },
success: function(data) { $("#awesomet").html(data);},
error: function(xhr, textStatus, thrownError, data) {
alert("Error: " + thrownError); }
});
</script>
When this is called upon page load it will run correctly and enter the information into the database. The issue is that I would like this to happen when a button is called so the information could be entered by the user.
<button onClick="sendCreationRequest()" />Submit</button>
<script type="text/javascript">
function sendCreationRequest() {
var place = $("#place").val();
var pos = targetted.getPosition();
var check = $("#checker").val();
var priv = $("#privOp").val();
$("#awesomet").html("GOT HERE.");
$.ajax({ type:"POST",
url:"backMap/locationCreation.php",
data: { name: place, loc: pos, check: check, privacy: priv },
success: function(data) { $("#awesomet").html(data);},
error: function(xhr, textStatus, thrownError, data) {
alert("Error: " + thrownError); }
});
}
</script>
So the Awesomet div will be changed to "GOT HERE." when a button is pressed that should activate the function so it is entering the function. I also had an alert going with all of the variables and they are correct.
When the button is clicked this is what comes up within the console:
CordovaLog - https://maps.gstatic.com/cat_js/intl/en_ca/mapfiles/api-3/13/4/%7Bmain,visualization%7D.js: Line 12 : Uncaught TypeError: Cannot call method 'lat' of undefined
Web Console - Uncaught TypeError: Cannot call method 'lat' of undefined:12.
I looked everywhere for some place that I was calling this but I couldn't find it.
I don't understand why it will work from a on page load call, and why it won't work from a function call.
EDIT: Targetted is a Google Maps Marker. When the call to get position is called. It returns just fine. I have put a alert(pos); within the code so when the button is pressed the alert does come up with the correct string.
EDIT: Okay, so I changed the:
var pos = targetted.getPosition();
to...
var pos = "(44.18974214877667,-79.63154019848633)";
The function call now works perfectly! However I don't understand why. They are getting the same numbers within the variable. Any ideas?
Just to answer my own question.
Thanks to bystwn22 and Mansimran Singh.
It appeared the issue was with the targetted.getPosition();
targetted was a Marker from Google Maps. It's getPosition() function returns something. When printing it out using an alert() it appeared to be the same.
It turns out it wasn't actually a String but some sort of json statement.
The fix turned out to be:
var pos = ""+targetted.getPosition();
Simple but it works.
Thanks everyone for the input!
Hello this is code snippet which i get from Jquery Ajax based search
I am done with everything, just the problem is the following script may not be sending the POST variable and its values or may be i am not properly fetching it.
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function() {
$("input[name='search_user_submit']").click(function() {
var cv = $('#newInput').val();
var cvtwo = $('input[name="search_option"]:checked').val();
var data = 'cv=' + cv + '&cvtwo=' + cvtwo; // sending two variables
$("#SearchResult").html('<img src="../../involve/images/elements/loading.gif"/>').show();
var url = "elements/search-user.php";
$.post(url, {
contentVar: data
}, function(data) {
$("#SearchResult").html(data).show();
});
});
});
});//]]>
</script>
In php file i have the following code:-
if (isset($_POST['cv']))
{
// My Conditions
}
else
{
// Show error
}
And its showing error, This means everything is correct just the post is not working properly, maybe.
Do the var data = 'cv=' + cv + '&cvtwo=' + cvtwo; // sending two variables will do the needful or we need to do any modifications. I know questions like this really annoy people, but what should i do i am stuck up.. #userD has really helped me a lot just, this part is left.
Since you're using $.post instead of $.ajax, your call should be:
$.post(url, data, function(response) {
/// ...
});
data must be a Javascript object, like this:
data = { "cv" : cv, "cvtwo" : cvtwo };
Check Jquery's documentation for more info:
http://docs.jquery.com/API/1.1/AJAX#.24.post.28_url.2C_params.2C_callback_.29
i got this script, which i found in the web:
<script type="text/javascript">
$(document).ready(
function () {
$('a.closeEl').bind('click', toggleContent);
$('div.groupWrapper').Sortable(
{
accept: 'groupItem',
helperclass: 'sortHelper',
activeclass : 'sortableactive',
hoverclass : 'sortablehover',
handle: 'div.itemHeader',
tolerance: 'pointer',
onChange : function(ser)
{
},
onStart : function()
{
$.iAutoscroller.start(this, document.getElementsByTagName('body'));
},
onStop : function()
{
$.iAutoscroller.stop();
}
}
);
}
);
var toggleContent = function(e)
{
var targetContent = $('div.itemContent', this.parentNode.parentNode);
if (targetContent.css('display') == 'none') {
targetContent.slideDown(300);
$(this).html('[-]');
} else {
targetContent.slideUp(300);
$(this).html('[+]');
}
return false;
};
function serialize(s)
{
serial = $.SortSerialize(s);
alert(serial.hash);
};
</script>
<div class="serializer">
<a href="#" onClick="serialize(); return false;" >serialize all lists</a>
</div>
<script language="JavaScript" type="text/javascript">var client_id = 1;</script>
at the bottom of the script is the "function serialize", which is called by clicking on the link below. Can someone tell me, how can i send the variable "serial.hash" to a php-file to save it in mysql-database?
many thanks, maschek
Yes as Yacoby says $.post() or jQuery.post
http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype
Use Ajax to send request to a PHP script which submits the data to a database. The JQuery documents contain a lot of examples. If you use a HTTP GET request, be warned there is a limit on the amount of data you can pass. This is browser dependant. I would recommend using jQuery.post as the method of sending data.
All the PHP script has to do is read the variables either from $_GET or $_POST (whichever method you use), sanitize them and submit them to the database.
Actually, it seems very simple now:
$.post("test.php",{'func':'serial'},function(data){
alert(data);
});