So, I have an input as below:
<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number"/>
Which in return store as a variable:
var data = {
'phone': $('#rhc_phone').val()
};
This then can be extracted via following:
<?php
echo '
<p>Call me at ' . $_POST['phone'] . '.</p>
';
?>
However, the input can be left out.
Now, what is the if statement to check if the variable has value and if not, then show something else like below.
<?php
if (!isset($_POST['phone']) = 0) {
echo '
<p>Call me at ' . $_POST['phone'] . '.</p>
';
}else{
<p>No Number</p>
}
?>
Is it correct?
Thanks!
Use empty
if(!empty($_POST['phone'])
{
echo '<p>Call me at'. $_POST['phone'] . '</p>';
} else {
echo '<p>No Number</p>';
}
you can use empty() function.
php empty() function
if(empty($_POST['phone']))
{
echo '<p>Number is empty</p>';
}
else
{
echo '<p>Your Number is '.$_POST['phone'].'</p>';
}
Related
I am applying while loop in php but it is running 4 times because it selects 4 values from the database.but i want to run while loop according to if else condition define by me.
<td style="border:1px solid;padding:11px;">
<?php
$query5=mysql_query("select atime from doctorbooking where aday='2016-09-29' and demail='".$query2['email']."'");
while( $query6=mysql_fetch_array($query5)) {
if (($array_of_time[$key] . ' - ' . $array_of_time[$key+1])==$query6['atime']) {
echo $array_of_time[$key] . ' - ' . $array_of_time[$key+1] . '<br /><span style="color:red;background-color:#C0C0C0;">Booked</span>';
} else {
echo $array_of_time[$key] . ' - ' . $array_of_time[$key+1] . '<br /><span style="color:red;">Available</span>';
}
}
?>
</td>
Following your logic, your array_of_time can only be available or booked . So you need to turn on a flag and check it afterward.
I dont know if you want to show available in priority or booked so the example below will force booked :
<td style="border:1px solid;padding:11px;">
<?php
$array_filter = $array_of_time[$key] . ' - ' . $array_of_time[$key+1];
$is_booked = false; //set it true if you want to force "available"
$query5=mysql_query("select atime from doctorbooking where aday='2016-09-29' and demail='".$query2['email']."'");
while( $query6 = mysql_fetch_array($query5) ) {
if ( $array_filter == $query6['atime'] ) {
//it force "booked" as soon as 1 is booked
$is_booked = true;
} else {
// OR uncomment here to force "available" as soon as 1 is availabe
//$is_booked = false;
}
}
if ($is_booked) {
echo $array_filter . '<br /><span style="color:red;background-color:#C0C0C0;">Booked</span>';
} else {
echo $array_filter . '<br /><span style="color:red;">Available</span>';
}
?>
</td>
thanks for your help in advance, noob here.
So, i have this error div of a form.
<div><?php echo $error; ?></div>
I'm trying to validate an email with php, but i want its error to change depending of the language selected by the user. I have another two php files with arrays with the translated text. What i'm tryng to do is to echo the error that contains the echo for the array variable so it can be interpreted as multilingual text in the html. I know that you can't echo an echo, but i need a workaround for this. Here is the php part.
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= '<?php echo $lang["EMAIL_INVÁLIDO"]; ?>';
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
};
?>
Thanks!
instead of direct echo, try assigning it to the variable and then echo the variable.
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
};
?>
The following should suffice:
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
};
You do not need to put quotation marks around variables(if you do they will be treated as text, not variables) and you were already working within <?PHP ?> tags.
instead just echo it later in the code:
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
}
if ($error != "") {
echo '<p>' . $error . '</p>';
}
}
?>
As you say, you can´t echo an echo because PHP is server-side. You must put your echo after you now the final value of error:
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= '$lang["EMAIL_INVÁLIDO"]';
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
echo $error;
};
?>
<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>
I need to return an input box from a function but i could put the quotes correctly. Any one please help me to solve the error.
<?php
return "
<input style='background-color:#CCC;'type='text' name='contactName' id='contactName' value='".if(isset($_POST['contactName'])) echo $_POST['contactName']."' class='requiredField' />";
use this
<?php
return '
<input style="background-color:#CCC;" type="text" name="contactName" id="contactName" value="'.(isset($_POST['contactName'])?$_POST['contactName']:'').'" class="requiredField" />';
for inline if that will output somthing use this syntax:
( condition ? 'the thing that will return when condition true' : 'the false returned string' )
<?php
if(isset($_POST['contactName'])) {$contactname=$_POST['contactName'];}
return "<input style='background-color:#CCC;'type='text' name='contactName' id='contactName' value='".$contactname."' class='requiredField' />";
?>
Instead of writing in a single line, you can break it instead and return the value.
if(isset($_POST['contactName'])) {
$value = $_POST['contactName'];
} else {
$value = "";
}
$input = '<input style="background-color:#CCC;" type="text" name="contactName" id="contactName" ';
$input .= 'value="'. $value .'" ';
$input .= 'class="requiredField" ';
$input .= '/>';
return $input;
Also i can see that you are assigning a value without processing it. This is not correct.
So, it should be something like:
if(isset($_POST['contactName'])) {
$value = stripslashes($_POST['contactName']);
} else {
$value = "";
}
You can also use any other escape method if you are using PDO.
Another simple solution
<?php
if(isset($_POST['contactName']))
{
return "<input style='background-color:#CCC;'type='text' name='contactName' id='contactName' value='".$_POST['contactName']."' class='requiredField' />";
}
?>
I was wondering how would hide the text Location if the city state and country were not present and how would I let it display if any of the variables were present?
<p>Location:
<?php
if (!empty($city))
{
echo $city .',';
}
if (!empty($state))
{
echo $state;
}
if (!empty($country))
{
echo ' ' . $country;
}
?>
</p>
Use:
<?php
$address = "";
if (!empty($city))
{
$address_parts[] = $city;
}
if (!empty($state))
{
$address_parts[] = $state;
}
if (!empty($country))
{
$address_parts[] = $country;
}
$address = implode( ", ", $address_parts);
if (!empty($address))
{
echo "<p>Location: " . $address . "</p>";
}
?>
Build the address up and then test if the address is still empty before displaying location.
Edit
Changed how the address is built up. Rather than concatenate everything, the address parts are added to an array and then imploded with the appropriate separator.
Wrap it in a PHP block as well:
<?php
// Check if any of them is not empty.
if(!empty($city) || !empty($state) || !empty($country)){
echo "<p>Location:";
if (!empty($city))
{
echo $city .',';
}
if (!empty($state))
{
echo $state;
}
if (!empty($country))
{
echo ' ' . $country;
}
echo "</p>";
}
in not exactly same but a similar problem, i used this method.
may be this helps you too:)
<?php
function publish($buffer){
global $location;
if (empty($location))
$buffer="";
return $buffer;
}
ob_start("publish");
$location=$_GET["location"];
echo "Location: $location";
ob_end_flush();
?>
test the url with location parameter and without a parameter.
And you can find more detail about this in php.net output control functions