Php variable not printing any value - php

In PHP I am writing:
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>";
But the value "$cat" is not showing anything.
Any suggestion?
Consider that if I write in HTML:
5.000€
It works...
Probably it is something I am missing in the sintax.
Thank you!

I solved like this:
<?php
$cat = $_GET['categoria_cqs'];//(to get the value from the session)
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=$cat'>5.000€</a>";
?>
I still cannot understand why I had to get it again (in HTML it worked without it) but that is fine. Thank you for your help!

The error is in the quotes, your line should look like this:
$link = '5.000€';
Remember that the string must be closed with the same quotation mark as it was opened.
Additionally, remember that the character " " and ' ' can "escape" themselves, as MoarCodePlz said.

I don't know what your real purpose is, if it is to create this directly in PHP or not, but maybe the code below will help you, I created a variable that will store the website URL that will pass the $cat variable as a parameter, to see the result I used the <a> tag in html
The code would look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "https://yourwebsite.com/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '";
?>
5.000€
</body>
</html>
If you want to get the result only in PHP, use the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>";
echo $link;
?>
</body>
</html>
Edit
You say that your code works only using HTML and not in PHP. Well, I've been analyzing it here and I saw that this was happening due to the fact that the file is in a directory, right? PHP was not understanding that the URL was from this directory, at least for me the error was this one in addition to the quotes
Please try the code below, I believe it will work in both PHP and HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = $_GET['categoria_cqs'];
$link = '5.000€';
echo $link;
?>
5.000€
</body>
</html>

Related

Assigning a color to the text is not working in tailwindcss

I want to apply color to a text using tailwindcss but it's not working.
Here is the code-
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="output.css" rel="stylesheet">
</head>
<body>
<h1 class="text-yellow-400 font-bold">
Hello, world!
</h1>
</body>
</html>
This is what that looks like in the browser. It seems like the class is not being applied.
Try to add Tailwind with the CDN because maybe output.css doesn't contain the feature you want to use.
```html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<h1 class="text-yellow-400 font-bold">
Hello world!
</h1>
</body>
</html>
```

PHP - warning generated for variable [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed last year.
I am trying to generate an error message in my HTML if one of these two things are happening, however the appropriate message is displaying the HTML, but I'm also given a warning for having a undefined variable.
How would I fix it with how I am doing it?
Code:
$tweet = "{$_POST['tweet']}";
$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";
if(strlen($tweet) > 140){
$errorOneOutput = $errorOne;
}elseif(empty($tweet)){
$errorTwoOutput = $errorTwo;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<?= $errorOneOutput ?>
<?= $errorTwoOutput ?>
</body>
</html>
I have tried using exit() in my PHP block instead of embedded HTML but then that doesn't generate the HTML. I specifically need the error message to be displayed within the HTML like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Challenge 3</title>
</head>
<body>
<br />
<b>Warning</b>: Undefined variable $errorOneOutput in <b>C:\xampp\htdocs\WEBD\Challenges\Challenge 3 - Twitter Challenge\insert.php</b> on line <b>29</b><br />
Error: Your tweet must not be blank.</body>
</html>
I also know that I can just echo out my errors, but again those would be above the HTML and not within it.
The issue is you're trying to print a variable you haven't initialised:
<?= $errorOneOutput ?>
<?= $errorTwoOutput ?>
To initialise these variables, add this to the top of your php file:
$errorOneOutput = "";
$errorTwoOutput = "";
Put ‘$errorOneOutput = null’ at the top to make sure the variable is always defined.
$errorOneOutput is only ever defined if the length of the string contained in $tweet is greater than 140 characters. Also, there is no point re-assigning the same value to duplicate variables. You should consider refactoring your code as follows:
<?php
$tweet = $_POST['tweet'];
$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<?php if (strlen($tweet) > 140) : ?>
<?= $errorOneOutput ?>
<?php elseif (empty($tweet)) : ?>
<?= $errorTwoOutput ?>
<?php endif ?>
</body>
</html>

laravel mail function not render html tags

8
and in my controlller i have this code
$users = User::where('user_type','=',3)->get();
return Mail::send(['text'=>'emails.sallery'],['$users' => $users],function($message){
$message->to('pulldozr#gmail.com','awad')->subject('test email');
$message->from('support#softya.com','awad alsharif');
});
and this is my blade code
<h1>asdfadsfasdf</h1>
now in gmail the mail will come not as h1 will come like this
<h1>asdfadsfasdf</h1>
without rendering the html tags
any help here thanks
Try to use this code instead:
$users = User::where('user_type','=',3)->get();
Mail::send('emails.sallery', ['users' => $users] , function($message) {
$message->to('pulldozr#gmail.com','awad')->subject('test email');
$message->from('support#softya.com','awad alsharif');
});
please can you put it into html template not like what you have sent like this maybe:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Mail</title>
</head>
<body>
<h1>asdfadsfasdf</h1>
</body>
</html>

HTML header information appearing in body

I have created a simple webpage, which includes both a header and footer as separate php files, shown below
<?php
$PageName = "Home Page";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
this is the header
<?php
print("<!DOCTYPE html>");
print("<html lang='en-UK'>");
print("<head>");
print("<title>");
print($PageName);
print("");
print("</title>");
print("<meta http-equiv='content-type' content='text/html; charset=utf-8' >");
print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$CSSRoot = "/MyPage/StyleDefault.css";
print("<link rel='stylesheet' type='text/css' href=$CSSRoot>");
print("</head>");
print("<body>");
print("<h1>My Page</h1>");?>
and footer
<?php print("</body></html>");?>
but when I view it the header elements appear in the body as shown below
header information appearing in the body
I want to make clear this does not, yet, cause any problems, but I want to know what the cause is.
Thanks
EDIT
brain fart moment putting the code in the comments, sorry.
new index
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
new header
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
new footer
</body></html>
new output
<html lang="en-UK"><head></head><body>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/MyPage/StyleDefault.css">
<h1>My Page</h1>
</body></html>
this is how you should do this.
header.php
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
index.php
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
footer.php
</body>
</html>
This should work, I don't know why you would want to print all the html tags using php, while you can just output em normal.
I beleive I have found the problem, the sources panel in chrome shows that just before the doctype and end body tags are two characters (ie where the header and footer files begin), represented by red dots, according to http://apps.timwhitlock.info/unicode/inspect?s=%EF%BB%BF%EF%BB%BF this is a ZERO WIDTH NO-BREAK SPACE, I don't know how they appeared and I can't seem to get rid of them, but it seems likely that they are the cause of the problem.
UPDATE
having looked around the prolbem is that the php files where using UFT-8 BOM (byte order mark) which inserted the offending characters, changing it to UTF-8 (which is under encoding in Notepadd++) solved the problem, thanks for all the help

Storing HTML file as a PHP string

I have a full HTML page:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
I'm trying to save it in a variable like so:
$template = htmlentities("<!DOCTYPE HTML><html lang="en-US">...", ENT_HTML5, "UTF-8" );
.. but it chokes at just the first HTML tag.
That's because the first HTML tag has double quotes, just like you use for delimiting your string literal.
$template = <<<EOD
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
EOD;
You are not escaping the string propertly Try to:
Replace
htmlentities("//whatever your html code is//");
with
htmlentities('//whatever your html code is//');
user addslashes function..it will not truncate your string in between.
This function can be used to prepare a string for storage in a database and database queries.
Before storing into database or for any purpose
$final_string = addslashes('<!DOCTYPE HTML>
..........');
Before rendering that output on browser
$normal_string = stripslashes($database_retrived_string);
$data = '<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>';
base64_encode($data);
Try this:
$temp = addslashes('<!DOCTYPE HTML><html lang="en-US">...', ENT_HTML5, "UTF-8" );
$template = htmlentities($temp);

Categories