admin管理员组文章数量:1279178
I am passing PHP variables in URL, so my page link looks like this www.example?redirect=neworders
Based on $_GET['redirect']
value I create the content of my page in my example the input
Then in the <script>
part I want to call a function with parameters based on the PHP
value.
My current code is working for now But I believe that's not the correct way how it should be done although I checked multiple threads and usually answers were one of the option I mentioned.
Any suggestions please if I am doing it right ? I added ments in my code to be understand my case. Thank you very much.
<?php
//my url can be either www.example?redirect=neworders or www.example?redirect=deliveredorders
switch($_GET['redirect']) {
case 'neworders':
$page = 'Backlog';
break;
case 'deliveredorders':
$page = 'Shipment';
break;
}
?>
<body>
//based on $page value, I create the input value with a specific id
<?php
if ( $page == 'neworders') {
echo '<input type="text" id="BacklogName" placeholder="enter order number">'; }
elseif ( $page == 'deliveredorders') {
echo '<input type="text id="ShipmentName" placeholder="enter order number then serial number">';
}
?>
<button type="button" id="submitButton">Submit</button>
<script src=".5.1/jquery.min.js"></script>
</body>
<script>
$(document).ready(function() {
//I have some functions declared here to use based on $page value
function submitEvent(element, input) {
element.click(function() {
alert(input.val());
});
}
//based on $page value, intialize the function with exact selectors
//first Option came to my mind : check the $page value and echo function intialization as string
<?php
if ( $page == 'neworders') {
echo 'submitEvent($("#submitButton"), $("#BacklogName"));'; }
elseif ( $page == 'deliveredorders') {
echo 'submitEvent($("#submitButton"), $("#ShipmentName"));'; }
?>
//second option came to my mind : use javascript 'if else' and only echo the $page value
if ("<?php echo $page; ?>" == "neworders") {
submitEvent($("#submitButton"), $("#BacklogName")); }
else if ("<?php echo $page; ?>" == "deliveredorders") {
submitEvent($("#submitButton"), $("#ShipmentName"));
}
});
</script>
I am passing PHP variables in URL, so my page link looks like this www.example.?redirect=neworders
Based on $_GET['redirect']
value I create the content of my page in my example the input
Then in the <script>
part I want to call a function with parameters based on the PHP
value.
My current code is working for now But I believe that's not the correct way how it should be done although I checked multiple threads and usually answers were one of the option I mentioned.
Any suggestions please if I am doing it right ? I added ments in my code to be understand my case. Thank you very much.
<?php
//my url can be either www.example.?redirect=neworders or www.example.?redirect=deliveredorders
switch($_GET['redirect']) {
case 'neworders':
$page = 'Backlog';
break;
case 'deliveredorders':
$page = 'Shipment';
break;
}
?>
<body>
//based on $page value, I create the input value with a specific id
<?php
if ( $page == 'neworders') {
echo '<input type="text" id="BacklogName" placeholder="enter order number">'; }
elseif ( $page == 'deliveredorders') {
echo '<input type="text id="ShipmentName" placeholder="enter order number then serial number">';
}
?>
<button type="button" id="submitButton">Submit</button>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</body>
<script>
$(document).ready(function() {
//I have some functions declared here to use based on $page value
function submitEvent(element, input) {
element.click(function() {
alert(input.val());
});
}
//based on $page value, intialize the function with exact selectors
//first Option came to my mind : check the $page value and echo function intialization as string
<?php
if ( $page == 'neworders') {
echo 'submitEvent($("#submitButton"), $("#BacklogName"));'; }
elseif ( $page == 'deliveredorders') {
echo 'submitEvent($("#submitButton"), $("#ShipmentName"));'; }
?>
//second option came to my mind : use javascript 'if else' and only echo the $page value
if ("<?php echo $page; ?>" == "neworders") {
submitEvent($("#submitButton"), $("#BacklogName")); }
else if ("<?php echo $page; ?>" == "deliveredorders") {
submitEvent($("#submitButton"), $("#ShipmentName"));
}
});
</script>
Share
Improve this question
asked Jan 27, 2021 at 10:11
DevTNDevTN
5932 gold badges13 silver badges37 bronze badges
3
-
script
tag should be inbody
and not out – MaxiGui Commented Jan 27, 2021 at 10:13 - Does this answer your question? How to get URL parameter using jQuery or plain JavaScript? – Lelio Faieta Commented Jan 27, 2021 at 11:29
-
redirect=neworders
is a query parameter and not a php variable. A query parameter is part of the url and you can access it with php with$_GET
or with javascript/jquery. See the duplicated answer for how to do it in javascript. Don't inject php in javascript as other are suggesting – Lelio Faieta Commented Jan 27, 2021 at 11:31
5 Answers
Reset to default 6you can use it like :
<script type="text/javascript">
var baz = <?php echo 42; ?>;
alert(baz);
</script>
You can try something like this:
<?php
$var = "123";
?>
<script type="text/javascript">
alert("<?php echo $var; ?>");
</script>
Or In you case you could simply store the php variable to javascript variable
var page = "<?php echo $_GET['redirect']; ?>";
than you can do watever with javascript variable in script
Here is the much simpler solution to your problem, add as many redirects option in the array as you want.
<?php
$redirectsList = array(
'neworders'=>array(
'Page'=>'Backlog',
'ElementID'=>'BacklogName',
'Placeholder'=>'enter order number'
),
'deliveredorders'=>array(
'Page'=>'Shipment',
'ElementID'=>'ShipmentName',
'Placeholder'=>'enter order number then serial number'
)
);
$redirect = isset($_GET['redirect']) ? $_GET['redirect'] : '';
$redirectData = isset($redirect!='' && isset($redirectsList[$redirect])) ?
$redirectsList[$redirect] : '';
?>
<body>
<input type="text" id="<?php echo $redirectData['ElementID']; ?>" placeholder="<?php echo $redirectData['Placeholder']; ?>">
<button type="button" id="submitButton">Submit</button>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#submitButton').click(function(){
alert($('#<?php echo $redirectData['ElementID']; ?>').val());
});
});
</script>
</body>
It's a wrong question. Your code works well but your $page
can never be 'neworders' or 'deliveredorders' in your conditions.
You assign $page
to 'Backlog' or 'Shipment'.
Change the switch block or your if/else conditions
switch($_GET['redirect']) {
case 'neworders':
$page = 'neworders';
break;
case 'deliveredorders':
$page = 'deliveredorders';
break;
}
Correct way to pass variables to JS is with using json_encode
:
var page = <?=json_encode($_GET['redirect'])?>;
if (page == "neworders") {
submitEvent($("#submitButton"), $("#BacklogName")); }
else if (page == "deliveredorders") {
submitEvent($("#submitButton"), $("#ShipmentName"));
}
<?=$var?>
is shorthand echo
本文标签: javascripthow to use a PHP variable inside ltscriptgt tagStack Overflow
版权声明:本文标题:javascript - how to use a PHP variable inside <script> tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264895a2368273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论