I have string value in input box
<input name="to" value="hi.rand(1,10). my.rand(10,100).name is .rand(23,54).rand(1,4)" >
How to use $_GET['to'] if i want have
hi5 my54name is 335
Now i have got string "hi.rand(1,10). my.rand(10,100).name is .rand(23,54).rand(1,4)" but not execute like php function.
So how to format string in input box so PHP take it like variables.
You can create javascript using php. Something like this:
<script type="text/javascript">
<?php
$anyVar = "BONJOUR";
?>
var value = "<?php echo $anyVar; ?>" ;
alert(value);
</script>
In the example I'm passing the value of my var $anyVar in PHP to the value var in javascript. So after the page loads and the script is parsed, you'll end up with something like this:
<script type="text/javascript">
var value = "BONJOUR" ;
alert(value);
</script>
You can as well write js code like this:
<script type="text/javascript">
<?php
$anyVar = "BONJOUR";
?>
var value = "<?php echo $anyVar; ?>" ;
<?php echo 'alert(value);' ?>
</script>
Hope this helps.
Related
I'm a JQUERY begginner. I'm trying to pass a PHP variable to a js file so that i can use it in JQUERY! But found no resource to learn to do so. I'm able to pass a JQUERY variable to a different php file but I couldn't pass PHP variable to a Jquery file. Is really possible to do so?
function.php:
<?php $variable = $fetch['username']; ?>
app.js : How can use this variable in app.js file below?
$(document).ready(function(){
var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{flwusernameselected:selectflwusername})
});
});
Echo it to HTML temaplate. If it's some structure/array use for example json format.
<script>
<?php $variable = $fetch['username']; ?>
var js_variable = <?php echo json_encode($variable); ?>
</script>
or
echo some hidden element like
<input type="hidden" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">
and grab if with Jquery like
<script>
$( "#custId" ).value();
</script>
with your code
JS:
$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{$( "#custId" ).value()})
});
});
How I can set the JavaScript variable strUser from PHP?
I am using the following code:
<script>
function val()
{
var e = document.getElementById("ali");
var strUser = e.options[e.selectedIndex].text;
}
</script>
brand<select id="ali" onChange="val()">
<?php
$brand=modsearchkhodroHelper::retrieve();
foreach($brand as $item)
{
?>
<option value="<?php echo $item['brand']?>" selected="<?php $id=$item['brand']?>">
<?php echo $item['brand']?>
</option>
<?php
}
echo "</select>";
?>
If you want to set the variable when the page loads, you could use something like this in the PHP code:
<script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
Just make sure to remove the later variable declaration from the JavaScript.
If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.
Use Cookie
in your javascript
<script type="text/javascript">
document.cookie = "cookieName=cookieValue";
</script>
in your php
<?php
$phpVar = $_COOKIE['cookieName'];
echo $phpVar;
?>
<script>
var id=`$`php;
</script>
I've added php variable store as JavaScript.
but some thing wrong in my code. what wrong with there please help me
You need PHP tags around the variable name and you need to use echo to print out the value
<script>
var id = <?php echo $whatever; ?>;
</script>
Use quotes if it's a string
<script type="text/java script">
var id='<?php echo $id;?>';
</script>
Try in this way
<script>
var id=<?php echo $var;?>
</script>
Try this
<script>
var id= <?php echo $php;?>;
</script>
So I was wondering, if I have a simple php form that posts information to a certain variable, how can I pass that posted variable to javascript and be able to manipulate the data? The only way I've figured out is by using inline javascript with php but I feel there must be a cleaner and more elegant way to do it where the variables don't show up so blatantly in the source code and are handled externally.. Here's what I'm playing around with right now:
<form action="index2.php" method="post">
<input name="sugar" class="form" type="text" value="" />
<input name="fat" class="form" type="text" value="" />
<input name="protein" class="form" type="text" value="" />
<input type="submit" value="submit" />
</form>
Which is followed by:
<?php
$sugar = $_POST['sugar'];
$fat = $_POST['fat'];
$protein = $_POST['protein'];
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var sugar = "<?php echo $sugar ?>";
var fat = "<?php echo $fat ?>";
var protein = "<?php echo $protein ?>";
</script>
<script src="test.js" type="text/javascript"></script>
</head>
Does anyone have any suggestions for alternatives I could follow? I haven't been using javascript for too long so it would be nice to know my options when dealing with a situation such as this.
The easiest way to do this would be simply to send a Javascript object containing all the data you sent. This can easily be accomplished by JSON-encoding $_POST:
var data = <?php echo json_encode($_POST) ?>;
You can then access, for instance, data.fat if you sent a fat value in your POST request.
Without using _GET as well and storing the variables in the URL you have the best solution for your requirements currently.
These variables will have to be printed and visible somewhere if you want to get them to your JavaScript.
JSON-encoding them and printing them so that they're saved in a local JavaScript variable is what I've always been using:
<?php
function passToJavascript($variableName, $variable){
if($variable = json_encode($variable))
echo '<script>var '. $variableName .' = '. $variable .';</script>';
}
$foo = array(
'bar1' => 1,
'bar2' => 2,
'bar3' => 3
);
passToJavascript('foo', $foo);
?>
Will print in your HTML:
<script>var foo = {"bar1":1,"bar2":2,"bar3":3};</script>
And that way you can pass whatever variable you want, may that be a string, a number, an array or even an "associative array".
I feel like the best way to pass php variables over to javascript variable is to json_encode them first. If you are passing a php array over to javascript you can't simple say
var1 = <?php echo $var1; ?>
you should do it like the follwing.
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
?>
<script type="text/javascript">
var arr = "<?php echo json_encode($arr) ?>";
</script>
You could write your vars in inputs and then reach these inputs with javascript
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
//You already linked jquery so I use it, you could do without if necessary.
$(document).ready(function(){
var sugar = $('#sugar').val();
var fat = $('#fat').val();
var protein = $('#protein').val()
});
</script>
var sugar = "<?php echo $sugar ?>";
var fat = "<?php echo $fat ?>";
var protein = "<?php echo $protein ?>";
</script>
<script src="test.js" type="text/javascript"></script>
</head>
<body>
<input type="hidden" id="sugar" value="<?php echo $sugar ?>" />
<input type="hidden" id="fat" value="<?php echo $fat?>" />
<input type="hidden" id="protein" value="<?php echo $protein?>" />
</body>
</html>
That way your javascript can all reside in a static file.
you could try sending your variables with GET instead of POST, and then you can access them directly from javascript like this: http://javascript.about.com/library/blqs1.htm
There is no way to access POST variables from javascript, as far as i know. You would have to use something like you are doing
I have an php array that is generated by server-side code. I want to show the array value in my input field after user pick an option from my dropdown menu. Please see my code:
$(document).ready(function(){
$('select[name="job_number"]').change (function () {
var selectedVal = $(this).val();
var test="<?php echo $JNarray['selectedVal'];?>"; //where the problem is
$('input[name="project"]').val(test);
//I want show value1 to value 3 in my input field when user picks
//an option from my dropdown menu.
});
<?php
$JNarray['job1']=value1;
$JNarray['job2']=value2;
$JNarray['job3']=value3;
?>
<form action='project_manager' method='post'>
<input type='text' name='project' value='show value1 to value3 when user picks an option' />
<select name='job_number'>
<option value='job1'>job1</option>
<option value='job2'>job2</option>
<option value='job3'>job3</option>
</select>
</form>
Any thoughts? Thanks for the help!
Here's a cleaner way to do it without having to set the array to a variable. Use a data attribute that jQuery reads with $.data()
HTML:
<option value='job1' data-job-val="<? echo $arrayvalue ?>">job1</option>
JS:
$('select[name="job_number"]').change (function () {
var test=$(this).find('option:selected').data('job-val');
$('input[name="project"]').val(test);
});
Though, I don't recommened echoing php in js...
var test=JSON.parse(<?php echo json_encode($JNarray['selectedVal']);?>); //where the problem is
<select name='job_number'>
<option value='<?php echo $JNarray['job1'] ?>'>job1</option>
...
...
</select>
<script>
$(document).ready(function(){
$('select[name="job_number"]').change (function () {
$('input[name="project"]').val($(this).val());
});
</script>
something like that.
You have to declare and populate your array before you use it on the jquery code. Move your <?php ?> code to the top.
Then pass the whole php array to javascript, using JSON as Trevor suggested:
$(document).ready(function(){
var options = JSON.parse(<?php echo json_encode($JNarray);?>);
$('select[name="job_number"]').change (function () {
var selectedVal = $(this).val();
var test=options[selectedVal]; //where the problem was
$('input[name="project"]').val(test);
});
});
I agree with Trevor, however try
"<?php echo $JNarray['"+selectedVal+"'];?>"
Echoing PHP means your calling your JS from a PHP file, not a good approach dude!, The only thing I would pass is maybe a BASE_URL or FILE_UPLOAD_TYPE/SIZE, even those are ewwwwwwwy