PHP - warning generated for variable [duplicate] - php

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>

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 variable not printing any value

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>

How can I send a PHP data to an html tag? [duplicate]

This question already has answers here:
php variable in html no other way than: <?php echo $var; ?>
(7 answers)
echo HTML with php variable
(3 answers)
php variable inside echo 'html code'
(6 answers)
Closed 3 years ago.
I have an Html file that in the future will be much larger, and another PHP document. In the PHP document, I will have the option of obtaining the number of subscribers and viewing my YouTube channel, and I want that information from the PHP file to reach the tag of the Html.
This is the PHP document:
<?php
// https://www.googleapis.com/youtube/v3/channels?part=statistics&id={Channel-ID}&key={Api-key}
$apiKey = 'APi-key';
//https://www.youtube.com/channel/{channel_id}
$channel_id = 'Channel-ID';
$response = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id='. $channel_id .'&key='. $apiKey);
$data = json_decode($response);
$subscriber_count = $data->items[0]->statistics->subscriberCount;
$view_count = $data->items[0]->statistics->viewCount;
//This is the PHP document
?>
And this is the Html document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<li>
<span>Subscribers:</span>
</li>
<li>
<span>Views:</span>
</li>
</ul>
</html>
</body>
</html>
So how can I send the subscriber information to the Span and the visualizations to the other span? Pliss help me to solve this.
Add echo statements to the html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<li>
<span>Subscribers: <?php echo $subscriber_count ?></span>
</li>
<li>
<span>Views: <?php echo $view_count ?></span>
</li>
</ul>
</html>
</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>

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