Passing a variable to PHP with jQuery .load - php

I have a page ("main.php") which loads content from an external PHP file ("rpc.php"). Using the below syntax on main.php successfully pulls in content from rpc.php:
$("#portfolioContent").load("rpc.php?o="+day+"");
On rpc.php I have an if statement (part of a long switch function), as follows:
if ( $pagename == "home" ) {
break;
}
This break is not occuring because the variable has not been set. rpc.php is used by various parent pages so the variables need to be set on those. On a parent page I have tried using the following code to attempt to set the variable and pass it to rpc.php but to no avail:
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
Can anybody point me in the right direction? Thank you.

It should be like this,
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");

Your syntax is wrong. Try this:
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Notice i substituted the $ with a &
Cheers

change this line
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
to this
$("#portfolioContent").load("rpc.php?o="+day+"pagename="home");
then access the variable with $_GET['pagename']

To access variables from the URL in PHP just do:
$_GET['variablename']
For example, with the URL http://www.example.com?hello=hellWorld
echo $_GET['hello'];
would print helloWorld
Also, you use & to separate variables, not $

Related

PHP dynamic variable as jQuery selector

After hours of frustration, I finally found the line of code that has been causing an error, but now I need to know why.
jQuery was throwing this error: Uncaught Error: Syntax error, unrecognized expression: .
I've researched it and found that this is a Sizzle error that occurs when jQuery cannot find the selector that is referenced. As was suggested in other questions on SO, this was not actually an error in my jQuery code, it was elsewhere.
I was defining a variable to use as a target element to load content, and was using PHP to dynamically define the variable, like so:
var $container = $(".<? echo $target ?>");
This did not work, as the . is flagged as an unrecognized expression. However, replacing the PHP variable with a static string works fine:
var $container = $(".target");
This was so hard for me to find because I couldn't pinpoint the line that was throwing the error, and in the source from the browser, the initial line above looks just like the second line.
Why does the first example not work? Does it have to do with order of execution? And, how can I use a dynamic variable as a selector?
you have to use
<?php echo $test; ?>
or the shortcut:
<?= $test ?>
You can try trim($target) before doing this. If it works you probably have some unwanted spaces in that variable of yours.
Also consider using json_encode to pass variables from php to javascript. Like so:
var selector = <?php echo json_encode($target); ?>;
var $container = $(selector);
This will allow you to pass not only simple strings but more complex variable structures as well (and with encoding safety).
Turns out the page I was loading wasn't having the variable $target passed to it. On the initial page, $target was initialized with a value, and thus the source output looked as specified in the question. However, the ajax call I was making to reload the page with new data was not passing the variable.

Can't pass a PHP variable to file_get_contents()

I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:
$body = file_get_contents('welcome/green2.html.php');
Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.
Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.
Any help would be much appreciated!
Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.
Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.
$bodytag = str_replace("#name#", $name, $myfile);
Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.
If you want it to be parsed by PHP, you would need to include or require it.
But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.
Hope that helps.
Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.

PHP Require and Include GET

I would like to require a file but also pass GET variables through the url, but when I write:
<?php
require_once("myfile.php?name=savagewood");
?>
I get a fatal error. How would I accomplish this functionality in a different way, such that I don't get a fatal error?
variables will be available as normal you do not have to pass like this.
$name='savagewood';
require_once("myfile.php");
$name will be available in myfile.php
<?php
$getVarsArray = $_GET;
$postVarsArray = $_POST;
/* n number of variables and lines of code*/
include('script-a.php');
?>
Now in script-a.php has access to $getVarsArray and $postVarsArray and if in any case you are in doubt you can use $GLOBALS to access any variable throughout the life cycle of a script. But using global variables is a sin. :)
It is not necessary to pass the variables to the new file, because by including the new file the variables are maintained.
Remember that $ _GET is an array, and it can be modified within the script.
<?php
$_GET['name'] = "savagewood";
require_once("myfile.php");
?>
On this case, $_GET['name'] is accesible from the "myfile.php"
I think I have got a perfect solution to your problem. You can use implode function of PHP. But I would strongly recommend doing Shakti Singh's code.
SOLUTION CODE
echo implode(file('http://path-to-your-site/your-dir/myfile.php?name=savagewood'));

Problem in assigning js value to php variable

How can i assign a javascript value to a php variable,
This is what i want to do:
<?php
$pag = echo "<script language ='javascript'>var pn = document.getElementById('t').value;
document.write(pn); </script>";
?>
But getting error as: Parse error: syntax error, unexpected T_ECHO
or is there any other way to do, but i want to assign that valur to php variable.
can somebody help here?
First, you can't give an "echo" to a variable. echo send a string or an information to the browser.
Then, you need to understand that Javascript is interpreted on the browser and PHP on the server.
It means, PHP can send variable to javascript (in an ugly and static way or with Ajax) but Javascript can't, unless you use it to change page sending the variable via GET or via AJAX.
Finally you should tell us what you need that for...
Javascript is client side, all PHP code is loaded by the server, before sending to the client. Thus, the only way to access JS variables in PHP is by setting a cookie in js, or by using AJAX
If you want to assign the value to a variable and then echo it out, use this code instead:
<?php
$pag="<script language ='javascript'>var pn = document.getElementById('t').value; document.write(pn); </script>";
echo($pag);
?>
Edit
Looking back over your question it would be good if you could explain exactly what you're trying to achieve.
Remove the echo.. You're assigning to a variable, not outputting anything.

Print all variables available in a Smarty template

How do you print all variables available in the context of a Smarty template? Something like the Django debug trace that lists everything being passed.
Thanks
Use {debug} From the manual:
{debug} dumps the debug console to the
page. This works regardless of the
debug settings in the php script.
Since this gets executed at runtime,
this is only able to show the assigned
variables; not the templates that are
in use. However, you can see all the
currently available variables within
the scope of a template.
$debugging = true must be enabled in your settings or class, and site popups must be unblocked to see the window
var_dump($Smarty->_tpl_vars);
From the Smarty code :)
Updated answer for Smarty 3: getTemplateVars
// If no parameter is given, an array of all assigned variables are returned.
$all_tpl_vars = $smarty->getTemplateVars();
$all_tpl_vars = $smarty->getTemplateVars();
var_dump($all_tpl_vars);
//before pushing to the template
exit;

Categories