I'm newbie at Jquery. I stacked to passing php $_GET['myvalue'] to my jquery file.
I struggled very much but I cannot find solution.
I have different one php file and one jquery file. I call my php file like
myphpfile.php?myvalue=testvalue
Then I want to receive myvalue's value to my jquery file. I tried document.getElementById but It doesnt work.
For any helping Thanks.
If you want to access the $_GET['myvalue'] value in your Javascript you can echo it out straight into it.
<script type="text/javascript">
// Your code
// The var you want to assign it to
var value = '<?php echo $_GET['myvalue'];?>'
</script>
Try this in your jQuery File:
First create a function to parse the parameters in the URL:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
vars[key] = value;
});
return vars;
}
Then you call the function:
var myUrlParameters = getUrlVars();
Then you can access them with:
var myParameter = myUrlParameters['myParameter'];
You need to read more about jQuery and Javascript...
You might want to add the variable you want in an HTML element, for example:
PHP
<?php
$myValue = $_GET["myvalue"];
echo '<input type="hidden" name="_method" value="'.$myValue.'" id="myElement" />';
?>
At jQuery:
$(document).ready(function(){
alert($('#myElement'));
});
If you want to access the $_GET['myvalue'] value in your script but using only Javascript.
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
Related
Here i want to pass div class value to the variable so far i had done like this.
Here is my div <div class="modal_content_payment"></div> and here i have value getting 20 now i want to store that value in to a variable and use it in a PHP function.
my PHP code looks like this...
<?php $p_amount=$this->db->get_where('tbl_abc',array('abc_id'=>`here i want that value`))->row();?>
i got value in div by using jquery see my code
<script>
function modal_payment(id,title)
{
var measurement_id=id;
$("#measmnt_id").val(measurement_id);
var url='<?php echo base_url();?>admin_control/view_particular_amount_modal';
$.post(url,{measurement_id:measurement_id}, function(result)
{
$(".modal_content_payment").html(result);
jQuery('#modal_payment').modal('show', {backdrop: 'static',keyboard :false});
});
}
</script>
first i want to store that div value in to a variable so i can use it in PHP please help me to solve.
You can get data through JavaScript.
Function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
document.body.innerHTML = originalContents;
}
Or you can use AJAX functionality.
currently, I am trying to set the URL of my website base on what the user click. The website is coding in html and php and this is the code I am trying to use to set the Url. However it seems that windows.location doesn't recognize php code. is there any other way to do this?
<script>
$(document).ready(function(){
$("#HELP").click(function(){
window.location='hotels.php?Category=<?php $cat ?>&Pic=<?php $img ?>';
});
});
</script>
You forgot to echo the variables to the output:
window.location='hotels.php?Category=<?php echo $cat ?>&Pic=<?php echo $img ?>';
Variables by themselves don't emit anything to the page. They simply represent a value.
Try this:
<script>
$(document).ready(function(){
$("#HELP").click(function(){
var url = 'hotels.php?Category=<?=$cat?>&Pic=<?=$img?>';
window.location= url;
});
});
</script>
What change:
Echo php veriables in url
But its not a good practice to use php code in client side you can create global veriables in java script.
Or if your requirement is like that than you can also use a hidden value for url in html and than get value in your jQuery code as like that:
<input type="hidden" id="url" value="<?=hotels.php?Category=<?=$cat?>&Pic=<?=$img?>" name="url">
<script>
$(document).ready(function(){ $("#HELP").click(function(){
var url = $('#url').val();
window.location= url;
});
});
</script>
I am implementing tags feature in my project.
In my demo tags i found they are passing names in a javascript function for the autocomple.
This is a function in my demo project,
<script>
$(function()
{
var sampleTags = ['c++', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'];
..................
.................
So , i want pass values from my php controller to this function ,inorder to get the autocomplete value from my database table
For example i am getting tags values from my db in my Controller like this:
` $data["query"] = $this->ordermodel->fetch_orderlist();`
$this->load->view('tagpage', $data); //loading my page tag page where above function exists
Now how can i pass that $data["query"] values into the above javascript function?
Please help
You could echo the variable onto the page using PHP's json_encode. This function will convert a PHP array or object into a JSON object, which JavaScript can work with:
<script>
$(function() {
var sampleTags = <?php echo json_encode($query); ?>;
})();
</script>
But a better way would be to request these values via Ajax. Say you have a PHP script named values.php:
<?php
#...
#assign $query here
#...
echo json_encode($query);
Then, in JavaScript (on the page where you want to use the sampleTags variable), you can use jQuery's .ajax function to make an easy Ajax request:
<script>
var sampleTags;
$.ajax({
url: 'values.php'
}).done(function(data) {
if (data) {
sampleTags = data;
}
});
</script>
I haven't tested this example. Obviously you'll want to tweak it to fit your environment.
You will have to write an ajax function for this
$.ajax(
url : "<?php echo site_url('controller/method')?>",
type : 'POST',
data : 'para=1',
success : function(data)
{
if(data){
var sampleTags = data;
}
}
);
Just echo your php variable
$(function()
{
var sampleTags = '<?php echo $data["query"]; ?>'
Let's say I have this PHP variables :
$SelectedCountry = "USA";
$SelectedState = "Texas";
on the other hand, I have this javascript function to display all available countries and states :
function print_country(country_id){
// given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(country_id);
option_str.length=0;
option_str.options[0] = new Option('Where do you live now?','');
option_str.selectedIndex = 0;
for (var i=0; i<country_arr.length; i++) {
option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
}
}
function print_state(state_id, state_index){
var option_str = document.getElementById(state_id);
option_str.length=0; // Fixed by Julian Woods
option_str.options[0] = new Option('Select state','');
option_str.selectedIndex = 0;
var state_arr = s_a[state_index].split("|");
for (var i=0; i<state_arr.length; i++) {
option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
}
}
my question is... how to make 'USA' and 'Texas' becomes selected <option> which generated by those two javascript functions? thanks.
NOTE #1 : you can see the complete code of javascript here : http://sourceforge.net/projects/countries/files/
NOTE #2 : those function called by adding this line on my PHP :
<script type="text/javascript" src="scripts/countries.js"></script>
<script language="javascript">print_country("country");</script>
so basically I need your help how to pass that PHP variables so that it can be 'received' by javascript function INSIDE that countries.js file.
One way is to just echo out some JavaScript statements:
<script>
<?php
echo "
var SelectedCountry = '$SelectedCountry';
var SelectedState = '$SelectedState';
";
?>
</script>
Then just use them in your loops to check if the option needs to be selected or not.
If you're going to be doing a lot of this sort of thing, though, embedding PHP into JavaScript isn't really the best approach. Read up on AJAX and PHP's json_encode() function.
There are two answers:
1 Use AJAX cal and pass back JSON
$.ajax({
url: '/myScript.php',
success: function(data) {
//Do something
}
});
myScript.php
return json_encode($myVar);
2 Embed PHP into the JavaScript
<script>
var myPHPVariable = <?php echo $myVar; ?>
</script>
Okay, so in my <head> section i have the following:
<script>
var userDefaultInfo = '<?=$userInfo;?>';
var jGets = new Array ();
<?
if(isset($_GET)) {
foreach($_GET as $key => $val)
echo "jGets[\"$key\"]=\"$val\";\n";
}
?>
</script>
Now In my external .JS file, In the $(document).ready() section I can access userDefaultInfo fine, however, I am trying to access jGets, but not directly from there.
in the external .JS file, outside of $(document).ready(); I have the following function:
var sendGET = function () {
var data = $(this).val();
var elementName = $(this).attr("name");
var url = "zephi.php?p=home/support/admin_support.php&"+elementName+"="+data;
jQuery.each(jGets, function(i, val) {
alert(val);
});
alert(url);
window.location = url;
}
When a user changes a box, this function fires and changes the window location using the data. However, I want to add the data in the variable jGets, but I do not seem to be able to reference it at all in there.
Why is this?
You're "mis"-using an Array as an Object.
var jGets = {};
Perhaps doing either one of the following might work:
echo 'var jGets = '.(isset($_GET) ? preg_replace('/^"|"$/','',json_encode($_GET)) : '{}').';'
//or
echo 'var jGets = JSON.parse('.isset($_GET) ? json_encode($_GET); : '"{}"'.');'
This will produce an object literal, assigning everything you need to jGets, without having to mess about with loops. Since JSON stands for JavaScript Object Notation, it seems only logical/natural to use that to set the variable accordingly. Read more on json_encode here, more on JSON in PHP can be found here