Sending more than one parameter in jquery onchange event - php

I am just getting used to jquery and have a drop down box that prompts an onchange event:
<select name="department_list" id="department_list" onchange="checkTeacherList(this.value);" >
This works fine and the AJAX command called is:
function checkTeacherList(str)
{
...
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?departmentName="+str,true);
...
}
However I also want to use the $user_login variable such that:
<select name="department_list" id="department_list" onchange="checkTeacherList(this.value,$user_login );" >
and the AJAX command is:
function checkTeacherList(str, schoolName)
{
...
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+str,true); ...
}
but this does not seem to work. What am I doing wrong?
EDIT:
The problem is that as soon as I add a second parameter to the function call from:
onchange="checkTeacherList(var1);"
to
onchange="checkTeacherList(var1, var2);"
the function is not called. Is the syntax wrong using a comma as a separator?

when using more than 1 variable passed with GET you have to add a &:
url?var1=value1&var2=value2&var3=value3...
Also you say you are using jquery, but to me it seems you are using plain javascript, if you want to make a AJAX call using jquery and GET:
$.get("url", {var1: value1, var2: value2}, function(data){
//do something when the AJAX call finishes,
//the var data contains anything echoed by the script called.
});
EDIT: to follow your question.
onchange="checkTeacherList(this.value, '<?php echo $user_login;?>');"
If you are using a PHP variable in a HTML script you have to echo it, also you have to use ' or it will cause a javascript error.

try this
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+str,true);

Try like this
checkTeacherList(this.value,<?php echo $user_login;?> );
You need to echo the php variable.and try to put '&' between the two variables you are sending like
xmlhttp.open("GET",
"http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+str,true); ...

You must include & between the parameters, like
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+str,true);
Then in the calling section edit like this,
checkTeacherList(this.value,<?php echo $user_login;?> );

Related

Passing jQuery Value to PHP

I am calculating the distance between two places using jQuery and I want to pass this value (pickup_distance) for use in PHP.
I am wanting to send this value to tariff.fare.controller.php for use in the following function:
private static function getFare($int_terminate) {
// Function
if pickup_distance(<-- jQuery Value) > 5 {
// Do Something
}
}
How could I go about doing this? I'm aware I can do this via AJAX but being quite new to programming I'm not quite sure how I can do this.
Any help would be much appreciated!
using Jquery it's quite easy:
var your_var_value=1200;
$.post("your_php_script.php", {var_value: your_var_value}, function(data){
alert("data sent and received: "+data);
});
then in your PHP script you get the variable like this:
$distance=$_POST['var_value'];
And what you echo in "your_php_script.php" is returned as the data variable.
As jQuery is client side code and PHP is Server side code, the variables must somehow be passed to the server.
There are a few decent ways of doing this, by far the most common is GET and POST variables.
then you can pick them up in php and do whatever you wish with it.
A very simple example is to have an iframe/php image whatever and just load the src with JavaScript or jquery file.php?EEEE=YYY then fetch that variable $_GET['EEEE']
Best way to do it is to use jQueries built in Ajax functionality. Either use .ajax or .post and send in your required parameters to your PHP script.

Javascript get output from separate php script

I want javascript to be able to call a php script (which just echos a string) using jQuery.
I think $.get is the right way, but not too sure.
I then want to use the returned string as a javascript variable.
$.get() is the way to go, indeed.
First of all, you will need a page / url that outputs the result of the function you want to use (for example, *www.yoursite.com/test_output.php* ). You should create that page and call the function you want to use there.
Also keep in mind that I said output, not return, because .get will fetch the output of the http response, not the returned value of the php function.
So if you have the following function defined in your site (you can also define it in test_output.php, of course):
<?php
function say_hello() {
return 'hello world';
}
?>
In test_output.php, you will need something like this:
<?php
echo say_hello();
?>
Then on the client side, you need some JavaScript / jQuery:
var data_from_ajax;
$.get('http://www.yoursite.com/test_output.php', function(data) {
data_from_ajax = data;
});
Now you have the output of the ajax .get() function stored in data_from_ajax and you can use it as you please.
$.get() is the right way.
$.get('ajax/test.php', function(data) {
// use the result
alert(data);
});

How to send data to a php script page during jQuery load and accept the data

I have a php function that builds a list of items for me. Im not sure but i read that you cant call a php function explicitly though jQuery/js.
So i saw that you can still call php pages like this:
$("#name").click(function(){
$("#div").load("script.php");
});
If i can call a php page like that, is there also a way to send it a URL when that page is loaded like this?
$("#name").click(function(){
$("#div").load("script.php", 'http://gdata.youtube.com/feeds/');
});
also another problem comes up that how do i make the script accept that string through from jQuery?
normally when you call a function you pass parameter with the call like so:
<?php makeList( 'http://gdata.youtube.com/feeds/' ); ?>
//on the function-side
<?php
function makeList( $feedURL )
{
//...stuff that uses $feedURL...
}
?>
Since i will make the function a script that runs upon being called how would i pass it a parameter?
I have no idea if this is possible or not and i would understand if this creates tons of security issues which makes it not acceptable.
You have the $.get and $.post methods in jQuery.
$.post('script.php', { url: 'http://gdata.youtube.com/feeds/' }, function(data) {
//data will hold the output of your script.php
});
The url is posted to your PHP script and you can access it through $_POST['url'].
See jQuery.ajax(), the 'sending data to the server' example.

having problems passing javascript variable to php file in the url

I have this code right now :
$(document).ready(function() {
var ktitle = $('.hiddentwo').text();
$('div#tab2').load('morefour.php?title=ktitle');
});
but the php file doesn't seem to be getting this variable...am I doing something wrong?
I echo something in the php file and I see the text. Also when I echo the $ktitle in the php file I get 'kitle'...that should not be happening :p. This is a stupid question, but I just want to know how to store that variable in the url.
$('div#tab2').load( 'morefour.php?title=' + encodeURIComponent(ktitle) );
try using
$('div#tab2').load("morefour.php", {
title:ktitle
});
or
$('div#tab2').load('morefour.php?title='+ktitle);
UPDATE
In the first case, the data are passed to the script via POST, in the second via GET.
Because you're hardcoding the ?title to "ktitle". If you want to replace this with a variable, you need to concatenate the variable with 'morefour.php?title=' + ktitle.

Using a php function in javascript

I need to use a php function inside this script which converts the ID into a required format.
function onlineStatus() {
$.getJSON('updateusers.php', function(data) {
$('#users').html('');
$.each(data, function(i, item) {
$('#users').append('[<span>'+ item.userid +'</span>]');
});
});
setTimeout("onlineStatus()",30000);
}
I have tried the following in various ways but I either get nothing or it fails with a missing ) after the argument list.
$('#users').append('[<span>'<?php nameFormat('+ item.userid +') ?>'</span>]');
I would appreciate your assistance.
PHP functions can only be executed on the server-side. You should probably translate the nameFormat function into javascript so you can use it on the client-side.
If you are trying to run this command on the server side before sending the script to the user, you have forgotten to echo the statement. Either type
$('#users').append('[<span>'<?php echo nameFormat('+ item.userid +'); ?>'</span>]');
or
$('#users').append('[<span>'<?=nameFormat('+ item.userid +')?>'</span>]');
... unless ofcourse the function nameFormat allready does an echo.
It's one side connection. You can use javascript in php, but you can't use php in javascript.
For example:
You can assign variable in php, and than use it in JS.
var a = <?php echo $a ?>;
The same thing you can do with functions ;)

Categories