how to pass a php var through jquery? - php

i have this script on JsFiddle .
when i click on the links i get another view.
What i am trying to do is replace
<a id="mine_click" href="#">test</a>
with:
$test = 'here we go'
<a id="mine_click" href="?var=$test">test</a>
and pass the $test var to the tab3view when i click on the test link,
so that the result will be 2222 here we go
any ideas on how to pass a php var through a link going through jquery?
thanks

I'm not sure that it is exactly what you need.
You have a php variable from the server side. In your javascript code you can do
var foo = '<?php echo $test; ?>';
and then you can use foo as a javascript variable
$('#mine_click').attr('href','?var='+foo);

You could do:
var test = '<?php echo($test) ?>';
$('#mine_click').attr('href', '?var='+test);

?var=<?php echo $test; ?>
will do the stuff
$('#mine_click').attr('href', '?var=<?php echo $test; ?>');

Related

Getting PHP variables intact with jQuery/json

So, a quick breakdown.
I've got a function on my page loads three sections, currently it loads them separately (which is kind of slow). So I've recently combined them all.
I now have all of the information stored in PHP variables. I'd like to get those PHP variables and load them into separate divs.
here is an example of what I'd like to do.
HTML
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
PHP
$var1 = "<div><p>data 1</p></div>";
$var2 = "<div><p>data 2</p></div>";
$var3 = "<div><p>data 3</p></div>";
jQuery
//`get var1,var2,var3`
// load var1 into div.one
// load var2 into div.two
// load var3 into div.three
The current problem I'm having is json_encode is changing some information into this "\n\t\t\t\t\t\t" and it's printing
"<div><p>data 1</p></div>"
instead of "data 1" in a p inside of a div.
Try:
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<script>
var var1 = "<?php echo json_encode($var1); ?>";
var var2 = "<?php echo json_encode($var2); ?>";
var var3 = "<?php echo json_encode($var3); ?>";
$(".one").html(var1);
$(".two").html(var2);
$(".three").html(var3);
</script>
You can pass PHP to JS in this way, but not the other way around.
var var1 = "<?php echo json_encode($var1); ?>";
var var2 = "<?php echo json_encode($var2); ?>";
var var3 = "<?php echo json_encode($var3); ?>";

how to store session when clicking LINK <a>

Is it possible to store session when clicking <a> in php??
If yes, how can I accomplished that? then that SESSION variable will be concatenate to JQUERY like this:
<script>
$(document).ready(function(){
serviceName = '<?php $_SESSION['sessname'] . ".php";?>';
if(serviceName!=""){
$('.main_content').load(serviceName);
}
</script>
is possible what I' trying to do?
Thank you in advance.!
Try to concatenate your value properly:
serviceName = '<?php echo $_SESSION["sessname"]' . '.php ;?>';

Pass PHP variable to Javascript at a specific point

I am passing a PHP variable to Javascript, but only at a specific point in the code. As the PHP code continues, the variable I need changes. I could easily just run the Javascript and grab the PHP variable when the page is done loading, but that won't pass the information I want. I want the Javascript to grab the PHP variable at that point, and then ignore subsequent changes to it.
The actual program I'm working with is several thousand lines of code, so I created a simple page to mess around with this issue. The code below shows what I'm trying to do.
<?php
$php_var = 'something';
if(true) {
$php_var = 'new';
echo '
<script type="text/javascript">
js_var="<?php echo $php_var; ?>";
alert(js_var);
</script>
';
}
$php_var = 'later changes';
?>
<script type="text/javascript">
var js_var;
</script>
This doesn't work, however. js_var is set when the inline script is run, but it is just set to a string that says <?php echo $php_var; ?> rather than actually evaluating it.
PHP runs first, and whatever JS is sent to the browser is run. I think you just want this:
echo '<script type="text/javascript">
js_var = '.json_encode($php_var).';
alert(js_var);
</script>';
$php_var = "foo";
echo '
<script>
var js_var = '. json_encode($php_var) .';
alert(js_var);
</script>
';
Produces:
<script>
var js_var = "foo";
alert(js_var);
</script>

Printing PHP variables into JavaScript variables [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 9 years ago.
Yes I know this question gets asked a lot, but I'm fairly new to JS and I need to use a php variable in some JS. I'm more then aware that PHP is executed server side and JS is client side however other people claim that this works.
I've got a PHP variable called "test1" that I want to log to the JS console (for instance):
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>';
?>
What this does is print " " to the JS console. Not exactly what I was hoping for.
Now this may not even be doable and I may have to pass the variable off the page and back in again with AJAX, but I'd rather have a quick and easy solution if there is one available!
Any help is appreciated.
You could do this.
<script>
var JSvar = "<?= $phpVar ?>";
</script>
The PHP will be parsed and the value of $phpVar will become the value of JSvar whatever.
Make sure you encode phpVar properly. For example, if phpVar contains a double quote, you'll end up with a broken JS
Use this no need to give "" => change to '.$test1.'..
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "'.$test1.'"
console.log(carnr);
</script>';
?>
try
<?php $test1 = '1'; ?>
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>
Generally, it is better to not print static stuff with php, but to have static (that is unchanging) stuff directly in HTML and only use PHP on the parts that really need it.
You made a mistake do it so:
<?php
$test1 = '1';
echo '<script type="text/javascript"> var carnr; carnr = "'.$test1.'" console.log(carnr)</script>';
?>
Since you're writing your JS with your PHP, you can simply do:
$test1 = "blah";
echo "<script type=\"text/javascript\">console.log($test1);</script>";
You are already in open php tag. When printing the line just append the variable to the output by using a dot.
Example:
print 'variable1 '.$variable1.' is now printed';

Javascript: dynamic var names with php?

Probably a dead simple and idiotic question (I'm totally new to javascript):
I have this code that loads a new post by clicking on a "next" or "back"-link. The clicks variable is used to scroll up and down in the sql-limit-statement (using the swapContent function), means you move backward or forward in the database by clicking the links. It works easy and perfectly:
<script type="text/javascript">
var clicks = -1;
function increase()
{
clicks++;
return false;
}
function decrease()
{
clicks--;
return false;
}
</script>
<div id="<?php echo $post['id'].'-multipost'; ?>">
<?php include('views/posts/_postmultipost.php'); ?>
</div>
<div id="<?php echo $post['id']; ?>-next" class="rightbutton" style="display:block;">
next
</div>
<div id="<?php echo $post['id']; ?>-back" class="leftbutton" style="display:none;">
back
</div>
The only problem: As you see I have several posts (post-IDs). But the javascript var "clicks" is always the same. How can I add the post-id into the javascript variable name "clicks", well, something like this :
var <?php echo $post['id']; ?>-clicks = -1;
Of course it doesn't work this way, but I have no clue how to manage it. Any advice? Sorry for this stupid question...
Thanks for your help!
UPDATE
Ok, got the solution: Bryan was right!!!
Changed the code to:
<script type="text/javascript">
var clicks = {};
clicks['<?php echo $post['id']; ?>'] = -1;
function increase()
{
clicks['<?php echo $post['id']; ?>']++;
return false;
}
</script>
The javascript in html stays as it is:
>
Clicks is now an object and will output the following in the swapContent-Function:
count: Array
(
[80] => 0
)
In php you would access the value like this:
foreach($count as $key=>$value) { $count = $value }
In javascript it seems to work a bit different like this:
for(x in clicks)
{
var clicks = clicks[x];
}
Seems to work perfectly now, thanks for your help!!
I'm not incredibly familiar with PHP, so I don't know about php echo. However, would using an object work?
var postClicks = {};
postClicks['<?php echo $post['id']; ?>'] = -1;
As far as I understand you are trying to get this:
var something-clicks = -1;
But in JS something-clicks is an expression - substraction of two variables.
Name tokens in JS cannot contain '-' in contrary with CSS.
You have a syntax error:
onmousedown="increase(); javascript:swapContent('next', clicks, '<?php echo $post['id']; ?>', '<?php echo $post['title']; ?>', '<?php echo $_SESSION['user']['id']; ?>');"
that javascript: is the problem. That property is expected to contain raw JS, and that token is invalid. the javascript used as a protocol is for use on the href property of an a tag.
Other than that, it looks alright. Just type clicks in the JS console of your browser to get the current value returned. Or add console.log('clicks:', clicks); to your function so that the result is logged out on each click.

Categories