Hello I am trying to disable a link in my PHP script using a JS function. I am not sure why is its not working. Any input appreciated..
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function disable()
{ document.getElementById("perfumes_web_link").innerHTML='<http://www.google.com" rel="external"">Perfumes';
}
function enable()
{ document.getElementById("perfumes_web_link").innerHTML='<a href="http://www.google.com" rel="external"">Perfumes<\/a>';
}
</script>
</head>
<?php
$disable_link = 0 ;
echo '<h1>JavaScript to disable link if set to zero</h1>';
echo '<li><span id="perfumes_web_link"></span><br>'."\n";
echo '<span class="perfumes_browser"><small>(Internet Explorer v6 or higher req.) < /small></span></li>';
if ($disable_link = 1)
{echo '<script type="text/javascript">disable();</script>';
}
elseif ($disable_link = 0)
{
echo '<script type="text/javascript">enable();</script>';
}
?>
</html>
So what am i doing wrong? Should i use if/else statement in the JS function itself or should i use true/false instead of 0 and 1.
Should i use something else like jQuery or Ajax..
Thanks
since you are comparing, use == instead of =,
like, change
if ($disable_link = 1)
to
if ($disable_link == 1)
OR you can do:
if ($disable_link == 1) {
echo '<script type="text/javascript">disable();</script>';
}
else {
echo '<script type="text/javascript">enable();</script>';
}
If you mean to open the link in new window, use target="_blank" instead of rel="external", like:
change
<a href="http://www.google.com" rel="external"">Perfumes<\/a>
to
Perfumes
Related
//So i have a php validation external and a html file. I'm trying to validate if //the input boxes are filled out correctly... so far i have one but I can't get it //to run and i tried testing it out doesn't work... do i need to download //something or is my code completely wrong. I just trying to check if its empty and if it has at least 3 characters
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cal5t</title>
</head>
<body>
<?php
$title= $_REQUEST["title"];
if ($title == "" or >3 ) {
echo "<p>5please!</p>";
?>
</body>
</html>
You are probably looking for something like:
if (($title == "") or (strlen($title) < 5))
{
echo "<p>5please!</p>";
}
if(!empty($title= $_POST['title']) && strlen($title) > 5){
echo "valid: $title";
} else {
echo "incorrect title: '$title'";
}
Also, its beter to use $_POST or $_GET over $_REQUEST.
I think you're looking for:
if(strlen($title) < 5){
echo '<p>The title you entered is not long enough.</p>";
}
You can make sure there is a value with the isset() function.
requird validation
$title = trim($_POST['title']); // it will remove stat and end spaces
if(strlen($title) <= 0)
{
echo "Title Field is required";
}
Can i use if else statement for css?
This is where i want the color of the text to change:
<?php echo $status; ?>
There will be 2 status: Pending & Delivered
Pending will be red color and delivered will be green
Can i do something like (for CSS):
.pending {text-decoration:underline; color:red;}
.delivered {text-decoration:underline; color:green;}
and if else statement:
if ($status==delivered)
{
//this is where i don't know what to do and code
}
else
{
//and here
}
What should i put there? Or any other solution?
If the $status variable in PHP actually matches your class names, just use it in your PHP when displaying whatever the thing is that's being styled:
e.g. if $status == 'pending', then:
<div class="<?= $status ?>">...</div>
will render
<div class="pending">...</div>
and match your .pending rule.
Output html with php / javascript / any other language, and assign classes to the whatever element you want.
pure PHP example:
<?php
if(true) {
echo '<div class="pending">content</div>';
} else {
echo '<div class="delivered">content</div>';
}
?>
Another way using variables (PHP + html):
<?php
if(true) {
$status = 'pending';
} else {
$status = 'delivered';
}
?>
<html>
<head>
</head>
<body>
<div class="<?php echo $status; ?>">content</div>
</body>
</html>
Alright so I've been trying to echo some JavaScript for the fancybox window to popup if a user logs in or out. This is what I have so far, but it keeps throwing a server error.
<link rel="stylesheet" type="text/css" href="scripts/fancybox/jquery.fancybox.css" />
<script type="text/javascript" src="scripts/fancybox/jquery.fancybox.js"></script>
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=0\',
title : \'Successfully Logged In!\'
}
], {
padding : 0
});
</script>';
}
else if (isset($msg['1'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=1\',
title : \'Successfully Logged Out!\'
}
], {
padding : 0
});
</script>';
}
?>
What am I doing wrong?
The problem isn't with the brackets. PHP won't echo out multiple lines of code. In order to do what you're trying to do you need to set up a variable and then echo that out:
$myVar = "";
$myVar .= "<script src='text/javascript'>";
$myVar .= "blah blah blah...";
echo $myVar;
Don't echo out the script, do something like this.
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); { ?>
<script>
//JS
</script>
<?php } ?>
if(isset($msg['0'])); {
Semicolon there? I think not. Besides is message an array? Maybe you wanted to do:
$msg = isset($_GET['msg']) ? $_GET['msg'] : false;
if($msg === '0') {
I want to change the innerHTML with PHP code. But I cannot get it to work, and I do not understand why. I want it to change some text on the page but from another file. And so I thought that I could use this:
document.getElementById ("page"). innerHTML = "<? php echo $ home?>";
But it does not work.
Here is my code:
<?php
$home = file_get_contents("home.php");
?>
<script type="text/javascript">
function ChangePage(page)
{
if(page == "home")
{
document.getElementById("page").innerHTML = "<?php echo $home ?";
}
}
</script>
There are many small typos. Try removing the space between $ and 'home' and before 'php'. This is the right statement:
document.getElementById ("page"). innerHTML = "<?php echo $home?>";
Also, where's your closing php tag?
<?php
$home = file_get_contents("home.php");
?>
<script type="text/javascript">
function ChangePage(page)
{
if(page == "home")
{
document.getElementById("page").innerHTML = "<?php echo $home; ?>";
}
}
</script>
Although this is a bad practice. Why would you want to do this instead of simply loading the php in the right place? Also, you do realize that 'page' should be the id of a pre-existing div in your html, right? Something like this would be better:
<html>
<body>
<div id = "page">
<?php echo file_get_contents("home.php"); ?>
</div>
</body>
</html>
How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/