0

I'm pretty new, so go easy. I am using a session in PHP with a few variables. I want to concatenate two strings together from my PHP session when the clicks a button. I am using JavaScript onclick to try and achieve this.

I am having trouble getting my JavaScript to accept my PHP variables. Does anybody have any suggestions?

PHP:

$_SESSION['user1'] = $_POST['user'];
$_SESSION['string3'] = $_POST['string1'];
$_SESSION['string4'] = $_POST['string2'];

echo $_SESSION['user1'];
echo $_SESSION['string3'];
echo $_SESSION['string4'];

if(isset($_SESSION['user1'])) {
    echo 'Welcome, '.$_SESSION['user1'].". Lets concatenate ".$_SESSION['string3']." and ".$_SESSION['string4'];

} else {
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=">";
}


<button onclick="xy()">Try it</button>
<p id="answer"></p>

JS:

<script type="text/javascript">
function xy() {
    var x = "<?php $_SESSION['string3']; ?>";
    var y = "<?php $_SESSION['string4']; ?>";
    document.getElementById("answer").innerHTML = x + y;
}
</script>
6
  • what file is the js in ? Commented Jul 31, 2014 at 22:09
  • what does the source say of your php page? Commented Jul 31, 2014 at 22:09
  • These are both in the same .php file Commented Jul 31, 2014 at 22:10
  • echo "<meta http-equiv=\"refresh\" content=\"0;URL=">"; has an unescaped quote. Commented Jul 31, 2014 at 22:12
  • yes, the script is below the php Commented Jul 31, 2014 at 22:12

2 Answers 2

4

You need to echo the PHP variable there because otherwise they output nothing and the JS variable ends up empty:

var x = "<?php echo $_SESSION['string3']; ?>";
var y = "<?php echo $_SESSION['string4']; ?>";

So for example, if $_SESSION['string3'] is "abc", PHP will echo the value inside the JS and you'll get this:

var x = "abc";

instead of:

var x = ""; // no output from PHP when there's no echo

To simplify things, here is your whole function:

<script type="text/javascript">
function xy() {
    var x = "<?php echo $_SESSION['string3']; ?>";
    var y = "<?php echo $_SESSION['string4']; ?>";
    document.getElementById("answer").innerHTML = x + y;
}
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, sadly I was too slow to post a full answer, so I'll extend this one. There's a shorthand for equivalent behavior: <?= $_SESSION['string3']; ?>
@boxmein I wrote it, and then decided to remove it because OP seems new to all this and didn't want to confuse him further. But yes, it's worth mentioning, thank you. :)
Where should I echo the PHP variable inside the JS function xy()?
No, no, just once for x and once for y and that's it... From there, JS 'knows' the new values and you don't need to echo them from PHP again. I've updated my answer to show you the whole function.
@Shomz I updated the JS code as shown above. Nothing happens when I click the button. Am I missing something else?
|
0

Maybe it should be something like this?

echo "<script type=\"text/javascript\">\n";
echo "function xy() {\n";
echo "var x = ".$_SESSION['string3']."\";\n";
echo "var y = ".$_SESSION['string4']."\";\n";
echo "document.getElementById(\"answer\").innerHTML = x + y;\n}";
echo "</script>";

2 Comments

Why would you do that?
@Shomz Just in case if the page was fully generated by PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.