How to fix “unsupported pseudo” error of jQuery AJAX? - php

I am trying to get response from a php page using jQuery Ajax. Everything works fine until I tried to explode an array and combined it elements to get a time 09:00.
Console says, Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: 00 and nothing displayed.
My code is,
$starttimeArr= explode(",",$comma_separated_starttime);// explodes 09,00,00
$endtimeArr= explode(",",$comma_separated_endtime);// explodes 17,00,00
echo $starttime= $starttimeArr[0].":".$starttimeArr[1];// combine to get 09:00. The line pop up the error
$endtime= $endtimeArr[0].":".$endtimeArr[1];// combines to get 17:00
How did I overcome this error? Any help will be appreciated.
My Ajax code is
jQuery("#_dob").change(function() {
jQuery.ajax({
url: "<?php echo $this->getUrl('deliverybydatepro/index/index') ?>",
data: "checkIn="+jQuery(this).val()+"&type=calendar",
type: "GET",
dataType: "html",
success: function(data) {
var $response=jQuery(data);
jQuery("#div1").html(data);
}
});
});
The response page has a dropdown having an option "09:00". jQuery-1.8.0 triggers an error on it.

Okay. Having seen the edited question with the JS code, the problem is clear:
var $response=jQuery(data);
This line is the cause of the error.
The data variable is the response string from the AJAX request. This contains the string "09:00", as expected.
This means that your code is equivalent of calling jQuery('09:00').
jQuery will try to interpret that as a CSS selector. It will see the 09 and try to find an element with that name. It won't find one of course, but it won't complain about that. However it will then see the :00 and assume that's a pseudo-selector (like :before or :first-child, etc). Of course :00 is not a valid pseudo-selector, and jQuery will complain about that. So that's where the error is coming from.
So what to do about it? Well the answer is quite simple, really.
You're using this line to set a variable called $response, but then you're never using that variable; you're continuing to use the data variable. So really the whole of the line that is throwing the error is completely unnecessary. You may need a line like that if your PHP is outputting JSON or XML data, but not if it's a plain string.
So the solution is to remove that line entirely.
Hope that helps.
Just as an aside, to help you out for next time, it would have been fairly easy to find out which line of JS code was causing the problem by using the debugger in the browser. Just open the Dev Tools or Firebug, and run the code, and it would stop and show you exactly where the error is. A little bit of further work with the debugger looking at variables, and it would probably have become clear what the problem was.

The php-answer is right, it sent the text '09:00' to javascript , but jQuery throws an error: "Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: 00", maybe ajax-answer is used as the determinant of element, maybe other . Show your js-code, because it throws an error

jQuery(data) was taken as the definition of an element with the pseudo-selector '09:00' and say that :00 - unsupported pseudo, becose there is the pseudo-selectors: first-child, hover, active and etc.
In this strings:
var $response=jQuery(data);
jQuery("#div1").html(data);
the string "var $response=jQuery(data);
is not needed, without this string script would work.

Related

trouble with double quotes first record of PHP array

php5, Microsoft SQL Server Management Studio, OS 6.3.15063. i get $row data via mssql_fetch_assoc and stuff a PHP array with the $row data just fine when the first record does not have double quotes. subsequent row data can have double quotes, just not the first row. Error: "SyntaxError: missing ) after argument list" in jquery.min - so i cannot peruse and get helpful info from that. code:
$History_SQL = "
DECLARE #AID int;
SET #AID = $Activity_ID;
SELECT activityid, lpid, abstract, changeid, changestamp FROM cpy_activity_history
WHERE activityid = #AID ORDER BY changestamp DESC;
";
$History_results = mssql_query($History_SQL);
while ($row = mssql_fetch_array($History_results)) {
$HistoryRecords[] = $row;
}
This works when record index[0] does not have double quotes, but when double quotes, errors out.
Interestingly, if i use:
$HistoryRecords[] = json_encode($row);
RESULT>>SyntaxError: missing ) after argument list
then it gets past the error; but I am having trouble usind json_decode to get any useful information out of the array [not even getting js braces or anything, getting another record from another table, possibly because this value is not what it thought it was going to be/either way, not sure i am even starting down the right road with json_endode/decode.]
it could be i simply do need help learning how to decode each record coming out of the json encoded var. either way, does this problem look familiar, and any ideas for how to stop getting the error? NOTE: ultimately, i need to display the array information via javascript. other related posts do not seem to cover this scenario. these double quotes are proving maddening. thanks in advance!
thx, All for helping!
the SYMPTOMS included the [a]browser crashing with console/js error: "missing ) after arg list", [b]a var in the code to display an erroneous field, unrelated to the sql used to create info for that var.
the REASON this occurred was that i had within PHP, a console.log that contained a PHP variable. so i was mixing js and PHP. the PHP was firing late and so at the time, the js console.log had nothing to display.
the CLUE which led to fixing this was noticing that the js msg was crashing in the browser, YET the php LOG_IT stmts below the js were working. the js was trying to console.log a msg that had a null value, and somehow a js var was getting loaded with wrong information. sheeze.
the CODE - before the fix the php echo from within the console.log was the culprit. i tossed the original offending stmt, but it was something like this:
<?php here in php land....., now i want to do a console.log....
?><script>console.log("show me info!: " <?php echo $info; ?></script><?php
...back in php land...
i commented out the "echo $info;" and things worked. of course i ripped it out altogether. funny, it would've cleared itself up when i stopped banging my head on the wall and simply decided to clean up and remove all the troubleshooting console.logs.
the CONCLUSION - an old lesson, "don't mix js and php", even in error logging!
thx again for all your help!

Good JSON but browser throws error

I have a PHP script that is building the header portion of an HTML(5) document. Part of what is being emitted is something like this
$rtn = <<<RTN
<script type='text/javascript'>
var _scrolls = {"alpha":{"cursorborder":"1px dashed rgba(200,13,57,1)"}};
var _floors = new Array(10000,{$mqt},{$mqf});
</script>
RTN;
The JSON is valid - at least, JSONlint.com seems to think so. However, in my browser (Chrome on Windows) this throws up an error Uncaught Syntax Error: Unexpected token illegal.
By dint of some experiment I have narrowed down the problem to the last attribute
"cursorborder":"1px dashed rgba(200,13,57,1)"
For some reason the browser is taking exception to the spaces in the attribute value. If I collapse that string so that it reads
"cursorborder":"1pxdashedrgba(200,13,57,1)"
the syntax error message disappears. I cannot think of any rational explanation for this. For completeness I guess I should mention that the JSON is being generated server side courtesy of json_encode.
Perhaps someone out here can tell me where I am going wrong?
it doesn't throw an error if I remove the first and the last ' , like this:
var _scrolls = {"alpha": {"bouncescroll":true,"boxzoom":false,"enabletranslate3d":true,"dblclickzoom":true,"gesturezoom":true,"hwacceleration":true,"horizrailenabled":true,"enablekeyboard":true,"railalign":"right","enablemousewheel":true,"nativeparentscrolling":true,"enablescrollonselection":true,"sensitiverail":true,"smoothscroll":true,"spacebarenabled":true,"railvalign":"bottom","touchbehavior":false,"autohidemode":false,"cursorcolor":"rgba(245,146,30,1)","background":"rgba(127,255,142,1)","cursorborder":"1px dashed rgba(200,13,57,1)"}};
You can't have literal line breaks in a JavaScript string literal.
Remove or escape them. Better: don't use JSON for this and just have a JavaScript object literal, that will save you from having to have a separate parse step.
As Jonas said, remove the ' from the beginning and the end of the string.
That way, the variable will become a valid Javascript Object and not just a long string containing JSON.

php htmlspecialchars and jquery .text()

I ran into an issue recently. I have a system where users can post stuff. One of the fields is the title field. So to save user input safely I use htmlspecialchars on the user submitted title and send it to a function that then saves to the database (after using mysql_real_escape)
Now on the client's side I use json get to fetch this title
$.getJSON("PHPFILE", function(json) {
// let's say json.title is the title we need so...
var title = json.title;
}
Now the thing is this user given title value can contain anything, even html tags (for reference let's say it now contains
<script>alert('');</script>Some Text!
so since I use jquery I thought of clearing those using the .text() function
var cleanTitle = $(title).text();
alert(cleanTitle);
However this immediately throws an error. In chrome it says
Uncaught Error: Syntax error, unrecognized expression ...
So I verified if this title variable is a string. And it is indeed a string. (Btw for some reason if this variable contains only numbers there is no error)
Using the following however gives me the text but the tags aren't removed
var cleanTitle = $.parseHTML(title);
cleanTitle = $(cleanTitle).text();
alert(cleanTitle);
This outputs
<script>alert('')</script>Some Text!
How can I remove all html tags? Any suggestions? I am planning to use this title text to set Browser title. Thanks.
document.title = $('<div />').append( $('<div />').html( title ).text() ).text();
Appending the string twice should fix the htmlentities issues.
Since you are using MySQL as engine to store that kind of data, you are clearly using PHP scripting. Suggestion: use PHP's strip_tag() and you are cutting "workload" for jQuery/Javascript by letting PHP do the work.

Changing servers, and getting SyntaxError: JSON.parse: unexpected character parsererror

I'm moving php code from one linux server to another, and the new one is producing rubbish.
In Firebug, the first result looks great, like normal json encoded values, and works.
The second result (new server) is preceded by "html" and "body" tags, a "p" tag, and then "quot;" instead of actual quotes around the data (sorry, I couldn't get the form to display all that), and throws an error.
In both cases, the code is the same, the output an array passed through json_encode.
Could this be a configuration error?
Thanks in advance.
John
Well, ultimately I added header('Content-type: application/json'); which removed the encoded html. I'd sure like to know why I had to do that.

Firebug: "invalid regular expression flag h" after running YUI Compressor

After I run YUI Compressor, I'm getting this error:
invalid regular expression flag h
[Break On This Error] ction(event){$(this).removeClass('lumi...cs_glb.php</b> on line <b>221</b><br/>
The part of the JS code in questions is:
//Form LUMI
$('input').focus(function(event){
$(this).addClass('lumi');
});
$('input').blur(function(event){
$(this).removeClass('lumi');
});
I validated the JS code with http://www.javascriptlint.com/online_lint.php and it points no error.
Does anybody knows what's wrong?
After trying one thousand ways to debug this, I found out that a php warning was causing the error. It is hard to find this kind of error because the Firebug debugger was pointing to a totally different direction. Thanks.

Categories