I'm trying to output some text stored in a variable in my HTML file
<span class="error">*<?php echo $piIdError;?></span>
I have declared and initialized the variable already along with the rest of some other php code that works
if (empty($_POST['piId']))
{
$piIdError = "Pi Id is Required";
}
else
{
$id = $_POST['piId'];
}
but when I run the file I get this error:
Notice: Undefined variable: piIdError in C:\xampp\htdocs\piWebConfig\index.php on line 86
Anyone have any ideas to what might be happening?
Thanks
Just initialize the variable $piIdError with the default value like
$piIdError = '';
if (empty($_POST['piId']))
{
$piIdError = "Pi Id is Required";
}
else
{
$id = $_POST['piId'];
}
Because if the condition failes then it goes for the else part at where the $piIdError was not defined.Orelse you can use isset like
<span class="error">*
<?php if(isset($piIdError))
echo $piIdError;?>
</span>
In your HTML code, use isset() to check if the variable is declared. You can pair it with a ternary operator, and you're all set:
<span class="error"><?php echo (isset($piIdError)) ? $piIdError : ''; ?></span>
<?php
if ($_POST['piId'] == '') {
$piIdError = "Pi Id is Required";
} else {
$id = $_POST['piId'];
}
?>
<?php
if(isset($piIdError)) {
echo '<span class="error">*'.$piIdError.'</span>';
}
?>
Related
I have some questions about isset() function.
isset() function checks if variable is set or not set or in other words it checks if value of variable is not NULL.
But what if I do something like this:
<?php
isset($var);
?>
What happens when I use isset() function on a variable that doesnt exist/isnt declared/isnt defined or whatever you call that?
I am asking because I am writing some code
<?php
function renderForm($firstName = '', $lastName = '' , $error = '', $id = ''){
?>
<div id='recordsForm'>
<h1><?php if($id != '' ){ echo "Edit Record"; } else { echo 'Create New Record'; } ?></h1>
<?php if($error != ''){ echo $error; } ?>
<form action='records.php' method='POST'>
<?php
if($id != ''){
?>
<input type='hidden' name='id' value='<?php echo $id; ?>'>
<?php
echo "<h3>Record ID: {$id}</h3>";
}
echo "First Name: <input type='text' name='firstname' value='".$firstName."' />";
echo "<br>";
echo "Last Name: <input type='text' name='lastname' value='".$lastName."' /> ";
echo "<br>";
echo "<input type='submit' name='submit' value='submit' />";
?>
</form>
</div>
<?php
}
if(isset($_GET['id']) && is_numeric($_GET['id'])){
// edit record
renderForm(NULL,NULL,NULL,$_GET['id']);
} else {
// add new record
if(isset($_POST['submit'])){
// Do some form processing stuff
} else {
renderform();
}
}
?>
?>
As you can see I wrote isset($_POST['submit']) even if $_POST['submit'] doesnt exist since i didnt called renderForm() function.
So does this means that isset() only checks if variable is not null even if variable doesnt exist like in my case?
I hope i didnt confused you :D
isset($var) checks that the variable is defined in the current scope and its value is not null. Some examples:
<?php
isset($var); //false
?>
<?php
$var = null;
isset($var); //false
?>
<?php
$var = "some string";
isset($var); //true
?>
<?php
$var = "";
isset($var); //true
?>
<?php
$var = false;
isset($var); //true
?>
isset() will return true for any variable that is set and not identical to NULL.
Example:
$a = null;
$b = '';
$c = 0;
$d = false;
$e = true;
var_dump(isset($a), isset($b), isset($c), isset($d), isset($e), isset($f));
should display:
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
In your case since you are not defining $var, isset($var) will return false.
I am echoing the $indicator before $total using if else condition.
But I am getting php error
Notice: Undefined variable: indicator.
How I can achieve this without getting any error and get my result which will be yellow according to the if else condition. Is it possible?
<?php
$total=6;
echo $indicator;
echo $total;
if($total<5) {
$indicator="red";
} else if($total>7) {
$indicator="green";
} else {
$indicator="yellow";
}
?>
Why don't you try like this
UPDATE
<?php
echo myFun();
function myFun(){
$total=6;
if($total<5){
$indicator="red";
}elseif($total>7){
$indicator="green";
}else{
$indicator="yellow";
}
return $indicator = $indicator.$total;
}
Update 2
I'll just leave this here for future references. But this is the solution I created with all the help I got here. Thanks!
<script>
function Refresh() {
location.reload();
}
</script>
<?php $number = $_SESSION["page_id"]; ?>
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]');
session_destroy();
echo ('<button class="btn btn-0001" onclick="Refresh()">Show All</button>');
}
else{ echo do_shortcode('[RICH_REVIEWS_SHOW num="all"]'); }
;
?>
Update
So I'm trying to just do a simple echo to see if the session is set using this code:
<?php if(isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo 'Set and not empty, and no undefined index error!');
};?>
But doing this breaks my page, I just get a blank page? How do I check if the session is set? When I do a echo of the session using this code:
<?php echo $_SESSION["page_id"]; ?>
It does output the correct session value?? What am I doing wrong?
I have a sessions saved with PHP and I'm using this so that the page ID from Wordpress is echo-ed in a shortcode do_shortcode('');
This is what my code looks like:
<?php $number = $_SESSION["page_id"]; ?>
<?php echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]'); ?>
<?php echo do_shortcode_all('[RICH_REVIEWS_SHOW category="page" num="all" id="all"]'); ?>
<?php echo $shortcode ;?>
<?php echo $shortcode_all ;?>
Now, what I would like to do is IF the page_id is not stored in the session it should echo all. So how do I go about this?
I found this code, and I think its something I need... But I'm not that great a programmer/coder...
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
So, if I where to put these two together I would get something like this
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number)) {
echo $shortcode_all ;
}
// Evaluates as true because $var is set
if (isset($number)) {
echo $shortcode ;
}
?>
Am I in the right direction?
Solution
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo('Set and not empty, and no undefined index error!');
};
?>
What am I supposed to feel from the missing (?
If you are using wordpress then you should use the session_start(); in wp-config.php file so please first put in you wp-config.php at top and then check.
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number) || $number=='') {
echo $shortcode_all ;
}else{
echo $shortcode ;
}
?>
I having issues getting a function to echo, where $lightbox_link1 = get_custom_field('lightbox_link1'). I'm fairly new to PHP.
Below is the defining function:
// Check for a lightbox link, if it exists, use that as the value.
// If it doesn't, use the featured image URL from above.
if(get_custom_field('lightbox_link1')) {
$lightbox_link1 = get_custom_field('lightbox_link1');
} else {
$lightbox_link1 = $image_full[0];
}
Echo Function:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '';
} ?>
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
should be
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
= is used for assignment
== is used for comparison
=== is used for typesafe comparison
also you can't declare <?php ... ?> inside another <?php ... ?>
to get something like <?php ... <?php ... ?> ... ?>
take a look at what you did up to here:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '<a href="<?php
Instead, using doublequotes in your echo statement will allow for the php variables inside to be parsed, so you could just do
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
to get
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
} ?>
PHP Part is here So when I post it, else part does not work at all (meaning: the text I set just disappear), while the other half works (meaning: I can assign variables, but can't leave it blank). I have been struggling with it all night...please help!
So this is the second page, if you need to take a look at the code on the first page, please let me know!
<?php
$lovers = $_POST['lovers'];
$quote = $_POST['quote'];
$color = $_POST['color'];
$font = $_POST['font'];
$imdblink = $_POST['imdblink'];
?>
<?php
if (isset($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}else{
echo "P & M";
}
if (isset($_POST['quote']))
{
$quote = $_POST['quote'];
}else{
echo "I love you.";
}
if (isset($_POST['color']))
{
$color = $_POST['color'];
}else{
echo "yellow";
}
if (isset($_POST['font']))
{
$font = $_POST['font'];
}else{
echo "Futura";
}
if (isset($_POST['imdblink']))
{
$imdblink = $_POST['imdblink'];
}else{
echo "http://www.imdb.com/";
}
?>
Here is the HTML part: Is there anything wrong here? Please help!
<div class="artGroup slide">
<div class="artwork"> <img src="../_images/M&P.png">
<div class="detail">
<div class="movie01c" style="font-family: <?php echo "{$font}"; ?>; font-size: 20px; color: <?php echo "{$color}";?>;">
<?php echo "{$quote}";?>
</div>
<div class="movie01t"><a href="<?php echo "{$imdblink}";?>">
<?php echo "{$lovers}";?>
</a></div>
</div>
</div>
</div>
Instead of using echo, use variable assignment too in the else part.
if (isset($_POST['lovers'])) {
$lovers = $_POST['lovers'];
}else{
$lovers = "P & M";
}
isset() tells you if the variable exists. If you have a form (which you didn't show the html for), and someone leaves the field blank, the field exists but is empty. There is no way that the form element will not exist after the form is submitted, so isset() will never return FALSE. So you want to use !empty() instead of isset().
your statements should be like...
if (isset($_POST['lovers']) && ($_POST['lovers'] != ''))
{
$lovers = $_POST['lovers'];
}
else
{
$lovers = "P & M";
}
ternary operator will make it really clearer.
for instance:
$imdblink = ((isset($_POST['imdblink'])) ? ($_POST['imdblink']) : ('http://www.imdb.com/'));
furthermore, you should define your default values, and use variable variable.
(here an example not using define)
$vars = array('imdblink' => 'http://www.imdb.com/');
foreach ($vars as $varName => $defaultVal)
$$varName = ((isset($_POST[$varName])) ? ($_POST[$varName]) : ($defaultVal));
You don't want to echo, you want to assign it, then you'll print it in your other PHP script.
I think you're trying to set default values if it doesn't come in the $_POST array. Try
$color = isset($_POST['color']) ? $_POST['color'] : 'yellow';
If you never want a blank color, use
$color = !empty($_POST['color']) ? $_POST['color'] : 'yellow';
This should work
<?php
$lovers = isset($_POST['lovers']) ? $_POST['lovers'] : "P & M" ;
$quote = isset($_POST['quote'])? $_POST['quote'] : "I Love You";
$color = isset($_POST['color'])? $_POST['color'] : "yellow";
$font = isset($_POST['font']) ? $_POST['font'] : "Futura";
$imdblink = isset($_POST['imdblink'])? $_POST['imdblink']: "http://www.imdb.com/";
?>
you can replace isset with !empty if you want to make sure the field is not ... empty
isset returns false only when the variable is not set or it is set to null. it will return true when the variable is ''(empty string) which will be the value you get when no value is entered in that field in the first page.
You need to use empty for this to work properly which will return true for unset and empty vlues. It returns for false for non-empty values. You need to prepend it with !(not operator) to use it here.
And you are using variable assignment in if and echo in else. I think that is what the cause of the problem. Change that echo to assignment.
if (!empty($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}else{
$lovers = "P & M";
}
Don't forget !.
All your isset $_post functions might works thats because it would not come to else part.
Here is an idea. Declare the variable and value first. If the $_POST['lovers'] is not null then change the variable value:
$lovers = "P & M";
if (isset($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}
echo $lovers;