This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to insert PHP into jQuery?
how to insert php on jquery/javascript ? for example
php
$tot =4;
javascript
$(function()
if( can put php here? )
eg : if( <?php tot > 4 ?> )
i want to put the php in 'if statement' ,it`s possible?
Try:
$(function() {
if(<?=$tot?> > 4) {
[...]
}
});
The <?=$tot?> part will effectively echo your $tot variable in the javascript code. Beware that if $tot evaluates to '' when converted to string, it could create a syntax error in javascript code.
You can use
if (<?php echo $tot; ?> > 4)
Related
This question already has answers here:
PHP Pass variable to next page
(9 answers)
Closed 3 years ago.
After a form submit, I am redirecting to the mentioned header location using below codes:
PHP Part:
$TestMessage = "This is a sample message. This is not a constant value. Original data will be collecting from DB";
if(mysqli_affected_rows($conn) > 0) {
header('Location:mydaomain/expenses?success=1');
}else{
header('Location:mydomain/expenses?failed=1');
}
HTML Part:
<div class="SubResult" id="SubResult" >
<?php
if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
echo $TestMessage;
echo "<h1 style='color:green;padding:3%;'>Success !!</h1>";
}
if ( isset($_GET['failed']) && $_GET['failed'] == 1 )
{
echo $TestMessage;
echo "<h1 style='color:red;padding:3%;'>SQL Error !!</h1>";
}
?>
</div>
Issue with the above code is,HTML wont display $TestMessage PHP variable content.
Why I am using this header option in PHP:
Earlier, form was getting submitted whenever I refresh the page after an original submit. I found this solution to avoid that issue.
When I remove this header part from php file, I am able to echo variable to PHP (But at that I am facing issue with the form submission on page refresh issue)
Is there any way I can echo this variable to HTML ?
When you use header, you are doing a redirect, so your variable $TestMessage is not available after that, if you want to use it, make sure you pass using query string, for example:
header('Location:mydaomain/expenses?success=1&testMessage='.$TestMessage);
and in the next page use $_GET['testMessage'] to get his value.
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
if($_GET["content"] ="foo"){
echo("<h4>foo</h4>");
exit;
}
When I type in URL "127.0.0.1/_/index.php?content=foo" it seems to output the previous code I wrote
if($_GET["content"] ="picture"){
echo("Picture:<br>");
echo("<img src='./ku~1.jpg'></img>");
exit;
}
What is the problem here?
if($_GET["content"] ="picture"){
assigns $_GET["content"] the value "picture". You want to use
if($_GET["content"] =="picture"){
and
if($_GET["content"] =="foo"){
instead.
Have you started your file with
<?php
And you are assigning the value content by only using one =
Do this instead
if($_GET["content"] == "picture"){
echo "Picture:<br>";
echo '<img src="./ku~1.jpg"></img>';
exit;
}
You were also wrapping your echo with parentheses when they should be contained in quotations.
I forgot the first example... That should be:
if($_GET["content"] == "foo"){
echo "<h4>foo</h4>";
exit;
}
This question already has an answer here:
Enabling php.ini for backward compatibility
(1 answer)
Closed 9 years ago.
Ok so i want to have an include code on my index.php page that will include all of the other html pages within it. I used to do this using an id?=link.html link and this:
<?php
$id = $HTTP_GET_VARS['id'];
if ( !$id || $id == "" )
{
$number=10;
include("news/show_news.php");
}
else
{
include "$id";
}
?>
but apparantly http_get_vars is unrecognized or something? How can I fix this so that it will work? Or if things have changed, why should I not use this kind of thing for includes?
You can use $_GET to fetch the query string parameter by GET request, e.g.
index.php?id=123
The id value can be obtained by $_GET['id'].
p.s. your PHP codes are really old.
solved with the following:
<?php
if (!empty($_GET['id'])) {
$id = $_GET['id'];
$id = basename($id);
include("$id");
} else {
include("news/newsfilename.php");
}
?>
and
<a href="index.php?id=pagename.html">
Page Name
</a>
as the html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Hilighting The Current Navigation Tab Using URL in PHP
hilighiting the current tab without php extension not working
<?php if ("index.php" || "index"==$Current)
{echo "selected";}else {echo "";}?>
I think you want:
<?php if ("index.php"==$Current || "index"==$Current) {
echo "selected";
}else {
echo "";
}
?>
What you wrote didn't really make sense.
This code
if ("index.php" || "index"==$Current)
already return true because
if ("index.php")
same
if ("index.php" != "")
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?
I am using the following code:
<script type="text/javascript">
<?php $ctnme = $_SERVER['REQUEST_URI'];
$cnme = explode("/",$ctnme);
echo $cname = $cnme[1];
?>
var spge = <?php echo $cname; ?> ;
alert(spge);
</script>
The value doesn't alert. What is the mistake?
Essentially:
<?php
//somewhere set a value
$var = "a value";
?>
<script>
// then echo it into the js/html stream
// and assign to a js variable
spge = '<?php echo $var ;?>';
// then
alert(spge);
</script>
The most secure way (in terms of special character and data type handling) is using json_encode():
var spge = <?php echo json_encode($cname); ?>;
Use json_encode() if possible (PHP 5.2+).
See this one (maybe duplicate?): Pass a PHP string to a JavaScript variable (and escape newlines)
Put quotes around the <?php echo $cname; ?> to make sure Javascript accepts it as a string, also consider escaping.
**var spge = '';**
alert(spge);