How to pass a PHP variable to a .JS file using JQUERY? - php

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()})
});
});

Related

Alert id of contents fetched from php while loop using jquery

I am new to jquery and ajax. I have a PHP while loop which display contents from database. What I need is simply alert the "content_id" on click of each contents.
My script
<script type="text/javascript">
$(function() {
$(".show_id").click(function() {
var test = $(".contentid").val();
alert(test);
});
</script>
PHP Code
//code to fetch contents from database
foreach($stmt as $row)
{
echo $row['content'];
?>
<form action="" method="post">
<input type="hidden" class="contentid" value="<?php echo $row['content_id']; ?>">
<div class="show_id" ></div>
</form>
<?php
}
?>
It Alerts the id for each content, But only the first id always .
How to alert id for each contents respectively ?.
Thanks for any help...
This would work:
$(function() {
$(".show_id").click(function() {
var cur = $(".show_id").index($(this)); // get the index of the clicked button within the collection
var test = $(".contentid").eq(cur).val(); // find the input with the contentid class at the same index and get its value
alert(test);
});
});
Note that this assumes that the two classes are always paired together like you show and not used elsewhere in your html, which seems a reasonable assumption here
<script type="text/javascript">
$(function() {
$(".show_id").click(function() {
var test = $(this).prev(".contentid").val();
alert(test);
});
})
</script>

how to php variable value assign as java script

<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>

Php vars within javascript

I have a bit of code to record a certain action
$('#record').click(function(){
$.get("../confirm.php", { "id": '<?php echo $record; ?>' }, function(data){});
});
Which works fine, but i don't want the page to be full of javascript and such as i have other things like this on it too, so i am trying to move it to a js file.
In the php file
<script type="text/javascript">var record = "<?= $record ?>";</script>
<script type="text/javascript" src="jsfile.js"></script>
In the js file
$('#record').click(function(){
$.get("../confirm.php", { id: record}, function(data){});
});
But it doesnt want to play ball, any ideas how to do this?
Make 'record' as class of div and assign php variable $record as its id.Use event.target.id to get variable $record from div.
<div class="record" id="<?php echo $record;?>">
Your code here
</div>
And Use this
$('.record').click(function(event){
var record=event.target.id;
//your code here
});
If I may a suggestion to you. Set in the view on the element the ID. Then when the action is clicked, get the id for the data.
Example:
<input type="text" name="inputName" id="<?php echo $record; ?>" class="btnToRecord" />
$('.btnToRecord').click(function(){
var clickedElement = $(this);
$.get("../confirm.php", { id: clickedElement.attr("id") }, function(data){});
});
Edit: Correction for $(this) was pointing to the get instead to the element, putting in a var will fix the problem

Javascript variables to php variables

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.

How to pass PHP value to external JavaScript file

I am trying to pass my variables from my main page to external JS file. Please see my code.
Main page:
<script type="text/javaScript">
$(document).ready(function(){
var add_project_link="<?= base_url().'add_project/show_form'; ?>";
var add_agent_link="<?= base_url().'agent/add_agent';?>";
});
</script>
External JS
$(document).ready(function(){
alert(add_project_link);
alert(add_agent_link);
});
I got:
uncaught referenceError add_project_link is not defined.
I thought the external JS would catch the variable declared in main page. Any thoughts? Thanks a lot!
Before you call your external JavaScript file, define your variables using PHP:
<script type="text/javascript">
var site_url = '<?php echo site_url(); ?>';
var current_url = '<?php echo current_url(); ?>';
</script>
<script type="text/javascript" src="file.js"></script>
In your external file.js you can now use site_url as a variable.
Take your php generated variables out of document ready. They are not accessible anyhwere else in your code due to closure of the ready event function they are in
What I would do is to store those variables to something easily called by JS later.
<input type="hidden" id="hidden_var" value="<?= base_url().'add_project/show_form'; ?>" />
var add_project_link = $('#hidden_var').val();
You just write in the head tag
<script type="text/javaScript">
var add_project_link="<?= base_url().'add_project/show_form'; ?>";
var add_agent_link="<?= base_url().'agent/add_agent';?>";
</script>
Then load your External JS after the script above
best way is to to store those path in some tags like
<span id="site_url" style="display:none;"><?php echo site_url(); ?></span>
<span id="current_url" style="display:none;"><?php echo current_url(); ?></span>
then you can access this this using jquery like
$('#site_url').html();
$('#current_url').html()
the simplest thing to do is to bring the script into a script tag on the php page
you can go down the URL string variable route, but it's unnecessary I think

Categories