How to add variable in error message (php) multilanguage? - php

Need error message in two languages, and have some trouble with single quotes...
This is how it looks like:
else if(trim($FullName) == '')
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
And how when I put variable
else if(trim($FullName) == '')
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>';
}
So, when I put it like this, syntax becomes incorrect because single quotes..

did u try
else if(trim($FullName) == '')
$error = '<div class="errormsg">'. $lang["error"] .'</div>';
}
EDIT : echo is a php instruction that does not require you to re-open and re-close php tags (<?php and ?>). You can compute strings thanks to concatenation which is some basic stuff in PHP

I would use empty, i.e.:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
the following, doesn't make sense since you're inside a php block:
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>
Change for:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">'.$lang['error'].'</div>';
}

Related

Echo value from only assigned array value

Please check following codes and tell me how can i echo $error[] array when each error receives a value when my code conditions meet. or is there any short solution for that? i want to echo error message when conditions meet. whats the best short hand solution for that?
if (isset($_POST['submit_update'])) {
if (empty($_POST['old_pass']) || empty($_POST['new_pass'])) {
$error[] = '<div class="alert alert-danger">Error: Field can not be empty</div>';
}elseif ($user->check_old_admin_pass($_POST['old_pass'])==true) {
if($user->update_pass($_POST['new_pass'])==true){
$error[] = '<div class="alert alert-success">Success: New password is set</div>';
}else{
$error[] = '<div class="alert alert-danger">Error: Fail to update</div>';
}
}else{
$error[] = '<div class="alert alert-danger">Error: Wrong old password</div>';
}
}
You could just try something very simple like so:
<?php
$strError = '';
if(isset($error) && !empty($error)){
$strError .= '<div class="error_wrapper">';
$strError .= implode("\n", $errors);
$strError .= '</div>';
}
echo $strError;

PHP echo in HTML file

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>';
}
?>

What is wrong with this PHP code - Unexpected T_CONSTANT

I keep getting the following error in my log
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'
The error is related to this line but I'm not sure what's wrong with it
<?php
if ($num != null) {
$query_string = 'msisdn=' . $num . '&ref=' . get_post_meta($post->ID, "ref", $single = true) ;
echo '<div class="highlight"><b>Join Now</b></div>';
}
else{
echo '<div class="highlight"><b>Join Now</b></div>';
}
?>
Your adding PHP opening tags when you are already within a PHP tag. You should change to:
<?php
if ($num != null) {
$query_string = 'msisdn='.$num.'&ref='.get_post_meta($post->ID, "ref", $single = true);
echo '<div class="highlight"><b>Join Now</b></div>';
} else {
echo '<div class="highlight"><b>Join Now</b></div>';
}
?>
else{echo '<div class="highlight"><b>Join Now</b></div>';}
There lays the problem. You see the '+2711111111'. it uses " ' ". You'll have to escape that one, because it will end your string there.
Also, you do not need the opening tags for php in their... just remove them as you are already in a php-snippet.
Try this:
<?php
if ($num != null) {
$query_string = 'msisdn=' . $num . '&ref=' . get_post_meta($post->ID, "ref", $single = true);
echo '<div class="highlight"><b>Join Now</b></div>';
} else {
echo '<div class="highlight"><b>Join Now</b></div>';
}
?>
Your problem was that you had a <?php echo... ?> in the middle of a string that was already being echoed. This had a ' in it, which was the type of quote used to encapsulate the string that was already being echoed. You could escape it (like \') but this would have your resulted in <?php echo... ?> being echoed into your HTML, which I doubt is what you want, instead your should remove this and put the function call into the middle of your echo.
This should be easy spot if you are using an editor/IDE with syntax highlighting. If your are not, look at EditPad, Notepad++ (editors) or Eclipse (IDE). Or Google it...
You are trying to echo a string containing <?php ?> tags
echo '<div class="highlight"><b>Join Now</b></div>';
Should propably be
echo '<div class="highlight"><b>Join Now</b></div>';

simple if/else in PHP

I display the form validation error in codeigniter as below:
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
i want to do it so that if there is error, then it should print error, otherwise it should print the info div.
For example,
if form_error, then
<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>
else
<div class="info">Your first and last name. </div>
As form_error is not just a simple variable that i can check if it is empty then print info. How can i do it? Thanks.
You can do something like this:
if ( form_error('name') )
{
echo form_error('name');
}
For form_error might not be a variable but it's a function that returns a string. If the string is empty (NULL, FALSE, "", 0, ...), the if statement will fail (meaning there is no error) and the form_error('name') won't be called.
This sould do it :
if (form_error('name')){
echo form_error('name', '<div class="form_error">', '</div>');
} else {
echo '<div class="info">Your first and last name. </div>';
}

Styling an If Else Statement

I know that it's not possible to style any given PHP but I do know that it is possible to style the output HTML that the PHP provides you with. I was wondering if there was any method available for me to use that would enable me to style the output from my IF ELSE statement.
if ($result != false) {
print "Your entry has successfully been entered into the blog.";
}
Just use HTML in your printed string. Nothing fancy needed.
if ($result != false) {
print "<p class=\"success\">Your <strong>entry</strong> has successfully been entered into the blog. <img src=\"smileyface.png\" alt=\"Smiley face\" /></p>";
}
You mean this?
if ($result != false) {
print "<span style='color:#ff0000'>Your entry has successfully been entered into the blog.</span>";
}
What I like to do, just to make life simple, is to switch between HTML and PHP in my page. For example,
<?php if ($fooSuccess = true) {?>
<p><span style="color: green;">Success!</span></p>
<?php } else {?>
<p><span style="color: red;">Error!</span></p>
<?php } ?>
You need to print HTML then.
if ($result != false) {
print "<b>Your entry has successfully been entered into the blog.</b>";
}
Or even
if ($result != false) {
print "<div class='Style1'>Your entry has successfully been entered into the blog.</div>";
}

Categories