How to concatenate a php variable in an echo statement with a javascript onClick event?
I am developing an application where each user has a uid and that uid is a folder. I have changed the name of the current directory to the uid and this displays the proper files on a dropdown of the files in the folder that is on the page but then the location still goes to the parent folder and not the the uid folder. How do I concatenate the uid variable with the location so it is going to the right folder? Here is what I have so far
<?php
chdir("$uid");
echo "<select name='menu'>";
$files = array_filter(scandir($dir), function($item) {
return $item[0] !== '.';
});
foreach (glob("*.php") as $file){
if ($file != 'new.php' && $file !='config.php'){
echo "<option value='$file'>".basename($file,".php"). "</option>";
}
}
echo "</select>";
echo "<input type='button' onClick='location=this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
echo "</form>";
?>
I have tried doing this:
echo "<input type='button' onClick='location='$uid/'.this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
But then I run into syntax errors. Is there a way I can concatenate the uid with the location file name?
javascript php html
add a comment |
I am developing an application where each user has a uid and that uid is a folder. I have changed the name of the current directory to the uid and this displays the proper files on a dropdown of the files in the folder that is on the page but then the location still goes to the parent folder and not the the uid folder. How do I concatenate the uid variable with the location so it is going to the right folder? Here is what I have so far
<?php
chdir("$uid");
echo "<select name='menu'>";
$files = array_filter(scandir($dir), function($item) {
return $item[0] !== '.';
});
foreach (glob("*.php") as $file){
if ($file != 'new.php' && $file !='config.php'){
echo "<option value='$file'>".basename($file,".php"). "</option>";
}
}
echo "</select>";
echo "<input type='button' onClick='location=this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
echo "</form>";
?>
I have tried doing this:
echo "<input type='button' onClick='location='$uid/'.this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
But then I run into syntax errors. Is there a way I can concatenate the uid with the location file name?
javascript php html
2
HTML attributes, e.g.onClick
, must have their values enclosed within double quotes, e.g.onClick="your_code_goes_here"
. YouronClick
is likely failing because you never added that in. Be sure to enclose theonClick
JavaScript in double quotes, remembering to escape those quotes i.e. doonClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.
– B. Fleming
Jan 20 at 0:38
add a comment |
I am developing an application where each user has a uid and that uid is a folder. I have changed the name of the current directory to the uid and this displays the proper files on a dropdown of the files in the folder that is on the page but then the location still goes to the parent folder and not the the uid folder. How do I concatenate the uid variable with the location so it is going to the right folder? Here is what I have so far
<?php
chdir("$uid");
echo "<select name='menu'>";
$files = array_filter(scandir($dir), function($item) {
return $item[0] !== '.';
});
foreach (glob("*.php") as $file){
if ($file != 'new.php' && $file !='config.php'){
echo "<option value='$file'>".basename($file,".php"). "</option>";
}
}
echo "</select>";
echo "<input type='button' onClick='location=this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
echo "</form>";
?>
I have tried doing this:
echo "<input type='button' onClick='location='$uid/'.this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
But then I run into syntax errors. Is there a way I can concatenate the uid with the location file name?
javascript php html
I am developing an application where each user has a uid and that uid is a folder. I have changed the name of the current directory to the uid and this displays the proper files on a dropdown of the files in the folder that is on the page but then the location still goes to the parent folder and not the the uid folder. How do I concatenate the uid variable with the location so it is going to the right folder? Here is what I have so far
<?php
chdir("$uid");
echo "<select name='menu'>";
$files = array_filter(scandir($dir), function($item) {
return $item[0] !== '.';
});
foreach (glob("*.php") as $file){
if ($file != 'new.php' && $file !='config.php'){
echo "<option value='$file'>".basename($file,".php"). "</option>";
}
}
echo "</select>";
echo "<input type='button' onClick='location=this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
echo "</form>";
?>
I have tried doing this:
echo "<input type='button' onClick='location='$uid/'.this.form.menu.options[this.form.menu.selectedIndex].value;'
value='GO'>";
But then I run into syntax errors. Is there a way I can concatenate the uid with the location file name?
javascript php html
javascript php html
asked Jan 20 at 0:18
J DoeJ Doe
124
124
2
HTML attributes, e.g.onClick
, must have their values enclosed within double quotes, e.g.onClick="your_code_goes_here"
. YouronClick
is likely failing because you never added that in. Be sure to enclose theonClick
JavaScript in double quotes, remembering to escape those quotes i.e. doonClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.
– B. Fleming
Jan 20 at 0:38
add a comment |
2
HTML attributes, e.g.onClick
, must have their values enclosed within double quotes, e.g.onClick="your_code_goes_here"
. YouronClick
is likely failing because you never added that in. Be sure to enclose theonClick
JavaScript in double quotes, remembering to escape those quotes i.e. doonClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.
– B. Fleming
Jan 20 at 0:38
2
2
HTML attributes, e.g.
onClick
, must have their values enclosed within double quotes, e.g. onClick="your_code_goes_here"
. Your onClick
is likely failing because you never added that in. Be sure to enclose the onClick
JavaScript in double quotes, remembering to escape those quotes i.e. do onClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.– B. Fleming
Jan 20 at 0:38
HTML attributes, e.g.
onClick
, must have their values enclosed within double quotes, e.g. onClick="your_code_goes_here"
. Your onClick
is likely failing because you never added that in. Be sure to enclose the onClick
JavaScript in double quotes, remembering to escape those quotes i.e. do onClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.– B. Fleming
Jan 20 at 0:38
add a comment |
1 Answer
1
active
oldest
votes
You can concatenate string in js with "+" operator, while in php you should use "."
So you can try this: (i didn't test it)
echo '<input type="button" onClick="location=''.$uid.'/'+this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">';
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272518%2fhow-to-concatenate-a-php-variable-in-an-echo-statement-with-a-javascript-onclick%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can concatenate string in js with "+" operator, while in php you should use "."
So you can try this: (i didn't test it)
echo '<input type="button" onClick="location=''.$uid.'/'+this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">';
add a comment |
You can concatenate string in js with "+" operator, while in php you should use "."
So you can try this: (i didn't test it)
echo '<input type="button" onClick="location=''.$uid.'/'+this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">';
add a comment |
You can concatenate string in js with "+" operator, while in php you should use "."
So you can try this: (i didn't test it)
echo '<input type="button" onClick="location=''.$uid.'/'+this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">';
You can concatenate string in js with "+" operator, while in php you should use "."
So you can try this: (i didn't test it)
echo '<input type="button" onClick="location=''.$uid.'/'+this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">';
answered Jan 20 at 9:07
Eko SetiawanEko Setiawan
205
205
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272518%2fhow-to-concatenate-a-php-variable-in-an-echo-statement-with-a-javascript-onclick%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
HTML attributes, e.g.
onClick
, must have their values enclosed within double quotes, e.g.onClick="your_code_goes_here"
. YouronClick
is likely failing because you never added that in. Be sure to enclose theonClick
JavaScript in double quotes, remembering to escape those quotes i.e. doonClick="your_code_goes_here"
. Other problems may or may not exist, but this one would definitely cause a major syntax error.– B. Fleming
Jan 20 at 0:38