This question already has answers here:
passing php variable from controller to javascript function in the head of a view
(3 answers)
Closed 3 years ago.
I have the following defined in my controller:
$data['is_amazon'] = $product->is_amazon;
$data['is_ebay'] = $product->is_ebay;
Directly below this in the controller I'm calling a .js file (jQuery).
$this->template->add_js('js/is_ebay_amazon_prouct_edit.js');
I'd like to access these php variables in my jQuery script, I've tried this approach:
var amazon = '<?php echo $is_amazon; ?>';
var ebay = '<?php echo $is_ebay; ?>';
jQuery(document).ready(function($) {
alert(amazon);
//alert(ebay);
});
Although this just displays my physical text in the alert, I.E:
<?php echo $is_ebay; ?>
What is the best approach here in terms of making these variables accessible in my jQuery?
Try this
do one thing.. you need to create two input hiddent field in view.
like this
<input type='hidden' id="amazon" value="<?php echo $is_amazon; ?>" />
<input type='hidden' id="ebay" value="<?php echo $is_ebay; ?>" />
and call that value in you jquery lik this
$(document).ready(function() {
//alert($('#amazon').val()+" "+$('#ebay').val());
console.log("amazon"+$('#amazon').val());
console.log("ebay"+$('#ebay').val());
});
now it will work for u.. try it.
There is no option to directly assign php variables to javascript. To do this you need to assign assign it to html elements like,
<input type="hidden" id ="amazon" value="<?php echo $is_amazon; ?>">
We can get the value in jquery from this html element by using this code,
var amazon = $('#amazon').val();
For security reasons the php variables cant assign to javascript variables.
Try this
<input type="hidden" id="fromAmazon" value="<?php echo $is_amazon ?>"/>
<input type="hidden" id="fromEbay" value="<?php echo $is_ebay ?>"/>
In jquery
After loading jquery.js file
jQuery(document).ready(function($) {
var amazon = $("#fromAmazon").val();
var ebay = $("#fromEbay").val();
console.log("amazon",amazon);
console.log("ebay",ebay);
});
Related
In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.
<input type="button" name="next" class="next btn btn-primary" value="Proceed To Checkout Page" />
Jquery
$(".next").click(function(){
<?php $var1="1";?>
}
While check the php value
<?php if(isset($var1)){
echo $var1;
}else{
echo "NULL";
}?>
Every time time i got the null value. Where i getting mistake.
Ps: I cant able to send the ajax call to get the value
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.
You can use cookies to achieve this.
In Javascript:
<script type="text/javascript">
document.cookie = "var1=1";
</script>
And in PHP
<?php
$phpVar = $_COOKIE['var1'];
echo $phpVar;
?>
If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.
Do some thing like this.
$(".next").click(function (){
var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.
//make ajax call with this as follows
$.post("a.php",{php_val:php_val},function(data){
//a.php is page name where u want to send php value.
})
});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>
I hope this answers your questions
It will help you ,
<script>
$(document).ready(function() {
$(".next").click(function() {
<?php echo $var = 1 ; ?>
});
});
</script>
<?php
if (!empty($var)) {
echo $var;
}
?>
I am getting a value with php $_GET[], and I want to pass it as the value of a simple html input element. I know I can do it like this:
<?
$value = $_GET['value'];
echo '<input type="text" name="value" value="'.$value.'" />';
?>
But is there any way to separate the php from html, giving the value to the textbox without echoing it?
I would like to create the textbox as regular html element, and only use php in the part where I set its value.
The answer of Iaroel was more practical for my purposes, but I liked the way that the accepted answer covered many concerns - I think it will be more valuable to other users.
You don't want an error when the $_GET['value'] becomes undefined.
<?php
$value = isset($_GET['value'] ? $_GET['value'] : '');
?>
<input type="text" name="value" value="<?php echo $value; ?>">
But be mindful with malicious data that can be inserted in $_GET['value'] so you've got to apply proper sanitation to $value
With regards to "Getting $_GET['value'] without PHP"
You can get it without PHP script by creating a small javascript function:
<script type="text/javascript">
function $_GET(q,s) {
s = s ? s : window.location.search;
var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
</script>
What the hell was that regular expression? It's just a matter of matching pattern from the URL.
Now, you can use this $_GET() method to assign the value to that textbox. In this example, I used jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('input[name="value"]').attr('value', $_GET('value'));
});
</script>
Now your HTML code will be as simple as this:
<input type="text" name="value">
Reference: http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/
If you don't wish to echo anything inside HTML, use Ajax call to get values.
$.ajax({
url: 'getValues.php',
type: 'post',
dataType: 'json',
success: function (json) {
if (json) {
$.each(json, function (k, v) {
$(k).val(v);
});
}
}
});
[PHP]
echo json_encode(array('#first_input_id' => 'val1', '#second_input_id' => 'val2', '.some_classes' => 'val5'));
Or if values are static, you can use local json file.
Do you mean like this?
<?php
$value = $_GET['value'];
?>
<input type="text" name="value" value="<?php echo $value;?>" />
Or this (not advised):
<?php
$value = $_GET['value'];
?>
<input type="text" name="value" value="<?= $someVar ?>" />
But i would suggest looking into a template engine for example http://www.smarty.net/
If your php configuration allows short tags you can do this:
<input type="text" name="value" value="<?=$_GET['value'];?>" />
you can try without echo
<?php $value = $_GET['value'];?>
<input type = "text" name = "value" value="<?= $value; ?>" />
If its really important for your project, ideally you want to separate your php code from your html tags. this can achieve by using some template engine.
There are some well known options available, best if you check and see which one suites your use-case best.
Mustache
Smarty
and of course Twig
You can use PHP Heardoc option. find the link here.
http://www.hackingwithphp.com/2/6/3/heredoc
I have a problem in passing 2 parameters via URL
I don't know how, so I wrote this:
<form method="get" >
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
</form>
<br><br>
<a href='./shopping-cart.php?part_id="<?php echo $_GET['part_id']; ?>"&quantity="<?php echo $_GET['quantity']; ?>"'>
<img src="add_to_shopping_cart.png">
</a>
$_GET['part_id'] this var from another URL and I want to pass it again and $_GET['quantity'] quantity from form.
You must use urlencode function for your parameters, and don't use the double quotes:
<a href='./shopping-cart.php?part_id=<?php echo urlencode($_GET['part_id']); ?>&quantity=<?php echo urlencode($_GET['quantity']); ?>'>
Even though, I suggest you to avoid long urls and use POST with hidden fields.
You get this notice Undefined index: quantity cause you are trying to access an undefined variable.
If your page url is something like page_name.php?quantity=5 then $_GET['quantity] is set, otherwise it isn't.
You should check if the $_GET variable exist.
If so, use #user4035 answer to print the url.
if(isset($_GET['quantity']))
{
//html code,the url
//echo $_GET['quantity']
}
There's a few errors that I can see.
You can't use $_GET['quantity'] if the user is entering it in without reloading the page (Remember, PHP is not client-side). Thus, I suggest using JavaScript for that.
Javascript:
function buildUrl(a) {
var qty = document.getElementById("qty").value;
a.href = "./shopping-cart.php?part_id="+<?php echo $_GET['part_id']; ?>
+"&quantity="+qty;
}
As you no longer need to get a PHP variable from the current page, the form is obsolete and a simple link will do.
HTML:
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
<a href='#' onclick="buildUrl(this);"> //onclick attribute triggers the JavaScript
<img src="add_to_shopping_cart.png">
</a>
with the code I have I can echo:<a id='test'>
and it displays what was typed, however I need to make<a id='test'> into a variable so I can use it for mysql query's etc. is this possible at all?
<script type="text/javascript">
function email(){
var input = document.getElementById('input').value;
document.getElementById('text').innerHTML = input;
}
</script>
<html>
<p><input type='text' id='input' value='' />
<input type='button' onclick='email()' value='submit'/></p>
</html>
<?php
$info="<a id='text'>"//make this a variable;
echo $info;
?>
using AJAX with jQuery will help you make a simple call to the server, without page reloading. Then you can send any variables to a PHP file that will store them in your database.
What I want to do is that only when I click on a div with id="$project['slug']", it will load the iframe inside that div.
So, i removed the src attribute from the iframe, and add it on onclick event.
I have this in my HTML/PHP:
<div class="box" id="<?=$project['slug']?>" onclick="load_frame();">
<iframe id="frame_<?=$project['slug']?>" frameborder="0"></iframe>
</div>
Js:
function load_frame() {
id = location.hash.replace('#', '');
var source = "<?php echo $project['video']; ?>";
$('#frame_'+id).attr("src", source);
}
Unfortunately, when I inspect the iframe it shows: src="<?=$project['video']?>" instead of the value that the variable holds.
Do you have any idea what i am doing wrong?
thank you!
jQuery is a client side language and have access only to the DOM elements once they have been rendered. So what you need to do is store $project['video'] variable in a hidden field and then using the id of that field get access to the rendered data.
Also, i noticed that you should use <?php instead of <?
You may try something like this.
<div class="box" id="<?php echo $project['slug']; ?>">
<iframe id="frame_<?php echo $project['slug']; ?>" frameborder="0"></iframe>
<input type="hidden" id="<?php echo $project['slug']; ?>" value="<?php echo $project['video']" />
</div>
Then in jQuery do:
$(document).ready(function(){
$('.box').click(function(){
var slug = $(this).attr('id');
var source = $('input#' + slug).val();
$('iframe#' + slug).attr("src", source);
});
});
add hidden input on html page
<input type="hidden" id="hidsource" value="<?php echo $project['video']" />
edit your function in js like this
function load_frame() {
id = location.hash.replace('#', '');
$('#frame_'+id).attr("src", $('input#hidsource').val());
}
hope this will work
You might not have the shorthand syntax enabled on the server. Try standard PHP instead:
<?php echo $project['slug']; ?>