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' />";
}
?>
Related
Can someone help me to figure out how to replace a defined string value with user input value? I am quite new in PHP programming and could not find an answer. I saw a lot of ways to replace string on the internet by using built-in functions or in arrays, but I could not find out the right answer to my question.
Here is my code:
$text = "Not found";
if ( isset($_GET['user'])) {
$user_input = $_GET['user'];
}
// from here I I tried to replace the value $text to user input, but it does not work.
$raw = TRUE;
$spec_char = "";
if ($raw) {
$raw = htmlentities($text);
echo "<p style='font-style:bold;'> PIN " . $raw . "</p>"; *# displays "Not found"*
} elseif (!$raw == TRUE ) {
$spec_char = htmlspecialchars($user_input);
echo "<p>PIN $spec_char </p>";
}
<form>
<input type="text" name="user" size="40" />
<input type="submit" value="User_val"/>
</form>
I appreciate your answers.
Lets run over your code, line by line.
// Set a default value for $text
$text = "Not found";
// Check if a value has been set...
if (isset($_GET['user'])) {
// But then create a new var with that value.
// Why? Are you going to change it?
$user_input = $_GET['user'];
}
// Define a few vars
$raw = TRUE;
$spec_char = "";
// This next line is useless - Why? Because $raw is always true.
// A better test would be to check for $user_input or do the
// isset() check here instead.
if ($raw) {
// Basic sanity check, but $text is always going to be
// "Not found" - as you have never changed it.
$raw = htmlentities($text);
// render some HTML - but as you said, always going to display
// "Not found"
echo "<p style='font-style:bold;'> PIN " . $raw . "</p>";
} elseif (!$raw == TRUE ) {
// This code is never reached.
$spec_char = htmlspecialchars($user_input);
echo "<p>PIN $spec_char </p>";
}
// I have no idea what this HTML is for really.
// Guessing this is your "input" values.
<form>
<input type="text" name="user" size="40" />
<input type="submit" value="User_val"/>
</form>
Just a guess I think you really wanted to do something more like this:
<?php
// Check if a value has been posted...
if (isset($_POST['user'])) {
// render some HTML
echo '<p style="font-style:bold"> PIN '.htmlspecialchars($_POST['user']).'</p>';
}
?>
<form method="post" action="?">
<input type="text" name="user" size="40" />
<input type="submit" value="User_val"/>
</form>
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>';
}
I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price')
{
// Start the element:
echo '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
echo ' checked="checked"';
}
// Complete the element:
echo " /> $value ";
} // End of create_radio() function.
I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span>
I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!
function create_radio($value, $name = 'gallon_price')
{
$output = "";
if (is_array($value)) {
while (count($value) > 0) {
$arr_value = array_pop($value);
$output .= create_radio($arr_value);
}
} else {
// Start the element:
$output .= '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
$output .= ' checked="checked"';
}
// Complete the element:
$output .= " /> $value ";
}
return $output;
}
A quick bit of recursion will allow the function to work for arrays and non arrays. Note: in the html you will need to echo the call to create_radio not just call it.
you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){
foreach($value as $v){
// Start the element:
$out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';
// Check for stickiness:
if(isset($_POST[$name])&&($_POST[$name]==$v)){
$out .= ' checked="checked"';
}
// Complete the element:
$out .= " /> $v ";
}
return $out;
} // End of create_radio() function.
call it:
echo create_radio(array('3.00','3.50','4.00'));
it is usually better not to echo inside the function.
This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
So I'm going to be making a couple forms with multiple text input boxes, so I figured making a function to help automate this would be a good idea.
Below is the function I've come up with: however, the results I've gotten seem to be really weird, being a combination of "echo" and a mess of single quotes. Does everything look correct in there? I'm new to PHP, so I'm really sorry if it's an obvious mistake I'm missing.
function makeTextInputField($name)
{
echo '<label for = "<?php $name ?>"> <?php ucfirst($name) ?> </label><input type = "text" name = "<?php $name?>"></input>';
}
You should not use any more tag inside the php
function makeTextInputField($name)
{
echo '<label for = "'.$name.'">'.ucfirst($name).'</label><input type = "text" name = "'.$name.'" />';
}
Working Demo
Because you can insert line breaks in strings in PHP, you can make your function more readable by using variables inside it:
<?php
function makeTextInputField($name) {
$text = ucfirst($name);
echo "
<label for='{$name}'>{$text}</label>
<input type='text' name='{$name}' />
";
}
?>
And whenver you want to use it:
<h1>Welcome</h1>
<?php makeTextInputField('email'); ?>
OUTPUT
<h1>Welcome</h1>
<label for='email'>Email</label>
<input type='text' name='email' />
Your problem is that inside PHP code you're opening new PHP tags, which actually are not required. Try this function and see if it's working for you:
function makeTextInputField($name)
{
echo sprintf('<label for="%s">%s</label> <input type="text" name="%s"></input>', $name, ucfirst($name), $name);
}
Try with sprintf.
function textInput($name)
{
$html = '<label for="%1$s">%2$s</label><input type="text" name="%1$s"/>';
echo sprintf($html, $name, ucfirst($name));
}
<?php
class DeInput
{
protected $_format = '<div>
<label for="%s">%s</label>
<input class="formfield" type="text" name="%s" value="%s">
</div>';
public function render($content,$getFullyQualifiedName,$getValue,$getLabel)
{
$name = htmlentities($getFullyQualifiedName);
$label = htmlentities($getLabel);
$value = htmlentities($getValue);
$markup = sprintf($this->_format, $name, $label, $name, $value);
return $markup;
}
}
Putting PHP code inside quotation marks is somewhat bad practice so I using (.) point to combine strings can be used.
Here is my example:
function makeTextInputField($name) {
echo '<label for="'. $name .'">'.ucfirst($name).'</label>';
echo '<input type="text" name="'.$name .' />';
}
use return intead of echo, and it will be easier to manipulate with result.
Also you can split elements generation into different functions for more flexibility:
function createLabel($for,$labelText){
return '<label for = "'.$for.'"> '.ucfirst($labelText).'</label>';
}
function createTextInput($name,$value,$id){
return '<input type = "text" name = "'.$name.'" id="'.$id.'">'.$value.'</input>';
}
function myTextInput($name,$value,$labelText){
$id = 'my_input_'.$name;
return createLabel($id,$labelText).createTextInput($name,$value,$id);
}
echo myTextInput('email','','Type you email');
function makeTextInputField($name)
{
echo '<label for = "'.$name.'"> '.ucfirst($name).'</label><input type = "text" name = "'.$name.'"></input>';
}
That should work.
You are already in php. So no need for the <?php tags. Concatenate strings together with a .
PHP:
function get_t_wrinkle_rel(){
global $mysqli;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
while ($r = $q->fetch_array()) :
echo '<p><input type="checkbox"' . $r['t_wrinkle_name'] . '</p>';
endwhile;
}
RESULT:
<input type="checkbox" value="Crows feet">Crows feet
<input type="checkbox" value="Frown lines" >Frown lines
<input type="checkbox" value="Lip augmentation">Lip augmentation
<input type="checkbox" value="Lip lines">Lip lines
<input type="checkbox" value="Marionette lines">Marionette lines
i want the result:
**LEFT** **RIGHT**
<input type="checkbox">Crows feet |<input type="checkbox" >Lip lines
<input type="checkbox">Frown lines | <input type="checkbox">Marionette lines
<input type="checkbox"">Lip augmentation
Could you not alternate a class on each second input that suggests clearing a float or something similar if you are not floating the inputs? Something along the lines of:
function get_t_wrinkle_rel(){
global $mysqli;
$flag = 1;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
while ($r = $q->fetch_array()) :
if ($flag = 1) {
$orientate=left;$flag=0;
} else {
$orientate=right;$flag=1;
}
echo '<p><input class="' . $orientate . '"type="checkbox"' . $r['t_wrinkle_name'] . '</p>';
endwhile;
}
while ($left = $q->fetch_array()) {
echo '<p>';
echo '<input...>' . $left['...'];
if ($right = $q->fetch_array()) {
echo '| <input...>' . $right['...'];
}
echo '</p>';
}
This way should fill the left column in order then the right column, keeping the order you want to achieve. I don't have access to your database so I can't test this, but it should do the job.
function get_t_wrinkle_rel(){
global $mysqli;
$q = $mysqli->query("SELECT * FROM t_wrinkle_rel ORDER BY t_wrinkle_name ASC");
$mid = floor($q->num_rows/2); // Get halfway point
$count = 0;
$array = array();
while($r = $q->fetch_array()){
$string = '<input type="checkbox" value="'.$r['t_wrinkle_name'].'" />'.$r['t_wrinkle_name'];
if($count <= $mid){
// Left column
$array[$count] = $string;
} else {
// Right column
$array[$count-$mid] .= '|'.$string;
}
$count++;
}
// Make single string
echo implode('', $array);
}
However I would recommend using the idea biscuitstack suggested, using CSS to position it the way you want, rather than doing it programatically. It's always better to try to keep presentation separate from the logic wherever possible.
You could use flag that increments everytime and
<input type="checkbox"' . $r['t_wrinkle_name'] .
if flag % 2 == 0 then echo '<br />'
Or something like that.