-1

I have the following input within a form:

<form>
    <input type="hidden" name="hiddeninput" value="SecondProblem"> 
</form>

Using the script below, I'm trying to store the word "SecondProblem" using PHP echo as a variable in JQuery and then use this to hide the parent form. The PHP echo text, however, is not picked up by a variable.

$(function(){ 
var selectorvar = "<?php echo "SecondProblem"; ?>"
var selectorvar1=selectorvar ;
 $('input[value="+selectorvar1+"]').closest("form").css("display", "none");
});

When I directly replace "+selectorvar1+" with the text "SecondProblem" the form display successfully switches to "none" so I know it has something to do with the PHP echo not storing as a variable.

Side note: I realize that there are simpler ways to turn the display on this form to "none" but I need to do it this way; I just removed the context so I could present my problem more directly.

Any help would be much appreciated!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Snoops
  • 215
  • 1
  • 12
  • 2
    Concatenate the variable, `$('input[value="' +selectorvar1+ '"]')` – chris85 Jun 24 '17 at 22:57
  • 1
    Please use your browser console to check errors. Console will tell you where you have problems – charlietfl Jun 24 '17 at 22:58
  • @chris85 is correct, you can't expect to end `''` delimited strings with a `"`. Any sane editor will make this obvious. – Adam Williams Jun 24 '17 at 22:58
  • @AdamWilliams more so the variable isn't expanded to its value because of the single quotes, I don't if JS does that in double quotes. – chris85 Jun 24 '17 at 22:59
  • It'd only do it in a template string, AFAIK. – Adam Williams Jun 24 '17 at 23:00
  • @Snoops .... stop changing the code in the question.... you keep changing problems – charlietfl Jun 24 '17 at 23:04
  • @charlietfl I changed one comma, which had nothing to do with the issue. Thanks for everyone's help! – Snoops Jun 24 '17 at 23:09
  • 1
    Right...well that comma became an answer .... and your browser console would also have told you it was a problem – charlietfl Jun 24 '17 at 23:14
  • Possible duplicate of [JavaScript Variable inside string without concatenation - like PHP](https://stackoverflow.com/questions/3304014/javascript-variable-inside-string-without-concatenation-like-php) – chris85 Jun 24 '17 at 23:24

2 Answers2

6

$('input[value="+selectorvar1+"]') should be $('input[value="'+selectorvar1+'"]').

Notice the single quotes added.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • It worked! Thanks; it's strange, because I have a similar bit of code later on that doesn't use those extra apostrophes and it works: `var = carouselbutton1percentage = "" var percentage=carouselbutton1percentage, col1="#72CF2F", col2="white"; var t=document.getElementById('carouselbutton1'); t.style.background = "-webkit-gradient(linear, left bottom,left top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";` – Snoops Jun 24 '17 at 23:04
  • Snoops...makes no sense using ` php echo?>` to output hard code strings – charlietfl Jun 24 '17 at 23:06
  • @Snoops I see no instance in your provided example where a JS variable is inside quotes. – chris85 Jun 24 '17 at 23:08
  • The jQuery selector is a string... You have to "concatenate" it when you use a variable to add to. So, between the single quotes boudaries, is the string passed as a selector. To add the variable, it has to be cutted in two parts... to concatenate the variable using the `+` sign. – Louys Patrice Bessette Jun 24 '17 at 23:09
  • When you do `var = carouselbutton1percentage = ""`... There is no need for extra quote. – Louys Patrice Bessette Jun 24 '17 at 23:12
  • According to the way we've resolved my original issue, `"+percentage+"` seems like it shouldn't return the value `47` in place of `percentage` unless it were written `" '+percentage+' "`but it does. – Snoops Jun 24 '17 at 23:13
  • Nope... In this part you mention, the string delimiter are double-quotes... No need for additional quotes. – Louys Patrice Bessette Jun 24 '17 at 23:15
  • for `"+percentage+"` that depends on what the quotes are that start and end the string. – charlietfl Jun 24 '17 at 23:15
  • Got it. Thanks so much everyone! – Snoops Jun 24 '17 at 23:17
  • Yes... You have to know what a quote or a double quote limits. Is it a string... Or a inner part of that string. Like here, we had an inner part : `$('input[value="'+selectorvar1+'"]')` The double quotes are part of the outer string delimited by the singles. – Louys Patrice Bessette Jun 24 '17 at 23:18
-1

I believe

var selectorvar = "<?php echo "SecondProblem"; ?>",
var selectorvar1=selectorvar ;

Should be

var selectorvar = "<?php echo "SecondProblem"; ?>",
selectorvar1=selectorvar ;
pokeybit
  • 1,032
  • 11
  • 18
  • Thanks for your reply. I tried to remove `var` accordingly, but it didn't do anything, unfortunately. – Snoops Jun 24 '17 at 22:57
  • There is no comma showing on first line in OP code. Missing `;` is not an issue in non minified code – charlietfl Jun 24 '17 at 22:57
  • No problem, also your quotes are fine. PHP is parsed by the server and won't place double quotes in your javascript. – pokeybit Jun 24 '17 at 22:58
  • 1
    @charlietfl there was when he created it. It's been edited. – pokeybit Jun 24 '17 at 22:58
  • Downvoted because this doesn't cover the concatenation issue within the jQuery call. – Adam Williams Jun 24 '17 at 22:59
  • Yeah, I removed the comma because I have a similar segment of code that I used elsewhere without a comma and it worked. Nonetheless, it doesn't work either with the comma or without the comma. – Snoops Jun 24 '17 at 23:01