PHP dynamic variable as jQuery selector - php

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.

Related

JQuery prepend to crash

If I ajax any input into my page the following code works but if I print some specific code I get an exception, alert test3 never fires.
alert("test2");
$(html).hide().prependTo("#current table.vtable").fadeIn("slow");
alert("test3");
I get the jquery exception:
Uncaught TypeError: Cannot set property 'cur' of undefined
As I've said the CSS is working proper, I have tried printing other classes and it works perfectly the only difference is I use
print <<<END
...
END;
Instead of normal print and the tr is a different class
I just ran into this issue after upgrading from jquery 1.7 to 1.9. This is a bug in the implementation of fadeIn().
It has to do with the fact that the data() method now returns undefined rather than null when you query for a key that doesn't exist.
Anyhow, a workaround that I found is to prepend the data first, and then get a jquery handle to the new, prepended content, and fade that in.
Since you're inserting the new content into a table, it's a little annoying to find the thing you just inserted, since it's not actually the first child (it's wrapped in a "tbody" tag implicitly). I worked around this by setting a temporary attribute on the inserted thing, searching for it, and then removing it. This is a gigantic hack obviously, but it does work.
var html = "<tr><td>new thing1</td><td>new thing2</td>";
$("#clickme").click(function () {
var target = $("#current table.vtable");
$(html).attr("jquery_workaround","sigh").hide().prependTo(target);
var newstuff = target.find("[jquery_workaround=sigh]");
newstuff.removeAttr("jquery_workaround").fadeIn("slow");
});
For the fiddling: http://jsfiddle.net/BUVUj/

PHP variable passing

I'm working with drupal. I have this page something.com/node/13. Inside I have this script
<?php
$door = $_GET["variable"];
echo $door;
?>
When I open the URL like this something.com/node/13?variable=1231 I get the following error:
Error message
Notice: Undefined index: variable in eval() (line 2 of /var/www/html/modules/php/php.module(74) : eval()'d code).
and no output. Any ideas what I might be doing wrong?
The error, partcilarly in drupal 7 simply means that you aren't getting any value for $_GET['variable']. This would be because the url doesn't have a value (someurl?variable=foo). You can avoid the notice by prefixing an # like this:
$door = #$_GET['variable'];
but that won't make the variable return any data. It's pretty unclear from the code you've posted what you're trying to accomplish. You might consider starting there. There is no reason why you can't use $_GET variables in drupal. I do so all the time.
Use drupal_get_query_parameters
Because the rewrite rules with Drupal, you don't grab the $_GET values cleanly directly like that.

Passing a variable to PHP with jQuery .load

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 $

Undefinded $_GET[' ...'] index

After making a javascript variable accesable into a php file the $_GET['something'] keeps saying undefined index.
Although the proper result gets displayed and being written into the xml.
If i try to initialise it my score no longer shows up for some reason.
How can i initalise this without it showing anything at al on my screen?
Regards.
If you do this with javascript:
window.location.href="index.php?scoreresult="+score
you have to access the variable in PHP using
$_GET['scoreresult']
instead of
$_GET['score']
If you make use of XSL to transform the XML, XSLT::setParameter may be interesting to you. It allows you to register (PHP)-variables for use inside a XSL-stylesheet.

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.

Categories