PHP inside echo in a function? - php

I have this part of the code of a plugin, which continues and has much php before it, fluidly without the ?> till the end of all the code.
function fivu_thumbnail_meta_box_html() {
echo '<label for="featured_image_url">';
_e("URL of featured image", 'fivu_textdomain' );
echo '</label> ';
echo '<input type="text" id="featured_image_url" name="featured_image_url" value="" size="30" />';
}
What i need is to change the input value="" (5th line) to this:
value="<?php get_post_custom_values("Poster"); ?>"
But as i am inside of an echo, the php is not interpreted as php but as normal text.
¿What can i do to make the php inside of the echo not to be interpeted as normal text?
EDIT: Ok, i am using
echo '<input type="text" id="featured_image_url" name="featured_image_url" value="' . get_post_custom_values("Poster") . '" size="30" />';
But i forgot, lol, i need to echo the value. So how do i add: echo $values[0]; ?

I don't like HTML in echo.
So Try :-
function fivu_thumbnail_meta_box_html() {
?>
<label for="featured_image_url">
<?php
_e("URL of featured image", 'fivu_textdomain' );
?>
</label>
<input type="text" id="featured_image_url" name="featured_image_url" value="<?php echo get_post_custom_values("Poster"); ?>" size="30" />
<?php
}

You don't need <?php again as you're already inside a PHP control. You simply need to concatenate your string with the get_post_custom_values function's return value, like so:
echo '<input ... value="'.get_post_custom_values("Poster").'" ... />';
So we have string + function's return value + string, three components which together contribute to the output of the input field HTML.

Use the string concatenation operator, .:
echo '<input type="text" id="featured_image_url" name="featured_image_url" value="' . get_post_custom_values("Poster") . '" size="30" />';

Related

PHP - How to pass variable in a text filed with double quote

My query extract a data value from mysql table where are a double quote in the text.
Select mytitle from title_table
The result is: This is my title: "for school"
This value I want to put inside a text field, but is truncated in this position This is my title:
I print the title by this: <?php echo $rows['mytitle']; ?>
How to put the entire title in a text field?
Thanks
Print inside the value of the text field, example:
<input type="text" name="title" value="<?php echo $rows['mytitle']; ?>">
I have resolve this problem. Are easy, but in a first time make a wrong Google search.
I have resolve by this: htmlentities
I think you search for this
<?php
echo '<input type="text" value="' . $rows['mytitle'] . '">';
?>
or in plain HTML with
<input type="text" value="<?=$rows['mytitle'];?>">
Look to, for the second example you must have enabled short_tags in your php.ini or use
<input type="text" value="<?php echo $rows['mytitle'];?>">
try this
<input type="text" name="yourkey" value="<?php echo $rows['mytitle'] ?>" />

Coudn't get textbox value

Here, I want to get value of textbox, but I coudn't.
Where am I wrong?
Is it must to use session if i want to get value of textbox?
<input type="submit" id="processorder" name="processorder" class="submit-green" value="Process Order"/>
<?php
foreach ($order_list as $row)
{
?>
<td class="align-center">
<input type="text" name="text[]" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
</td>
</tr>
<?php
$i++;
}
?>
if(isset($_POST['processorder']))
{
$txtvalue = $_GET['text[]'];
echo "helo".print_r($txtvalue);
}
this is wrong,
$txtvalue = $_GET['text[]'];
it should be
$txtvalue = $_GET['text'];
^ no [] here
Replace $_GET['text[]'] to $_GET['text']
The field would not be called text[] to PHP. It would just be called text, but would be an array instead of a string.
Try this:
if(isset($_POST['processorder']))
{
$arrayvalue = $_GET['text'];
foreach($arrayvalue as $txtvalue){
echo "helo" . $txtvalue . "<br>";
}
}
As your text input name is text[] with a bracket, it indicates that it is sending as an array, so to get the value you need
if (!empty($_POST["test"][0]) ) {
$test_value = $_POST["test"][0];
}
I am using 0 here since your code only shows one input text, for the server-end will only receive one array value
why you use array name="text[]" if you don't want to get multiple data using "text" than make it name="text"
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'] // get value
or
you want to multiple data using same name "Array"(mean using array)
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'][0] // 0 is a index value

PHP syntax for using echo or pint within echo or print

I'm using the server to write the html of a lot of my page to process some pagination. I'm getting a syntax error on the line just below the comment insert as shown below. I'm guessing it is because I'm trying to place echo within a echo? I've tried a lot of things here and am having no luck figuring out what I'm doing wrong. Any help appreciated greatly!
<?php
PDO STUFF HERE. . .
if() {
foreach() {
echo ' <input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
//following line showing syntax error in my text editor
<input name="transaction" type="radio" tabindex="11" value="0"' . if($transaction == '0') echo '$chkvalue'; . '/><label for="listings">Listings</label>
//following line showing syntax error in my text editor
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' . if($transaction == '1') echo $chkvalue; . '/> '
}
}
?>
if and echo are language constructs and as such cannot be used inside a string. You'll have to change the flow of the logic to fix this.
Easiest fix is to just use a bunch of semicolons.
This is not allowed:
echo 'abc' . if( $a == 1 ) { echo '3'; };
But this is fine, because it breaks everything up into steps:
echo 'abc';
if( $a == 1 ) { echo '3'; }
Try something like this:
echo '<input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
<input name="transaction" type="radio" tabindex="11" value="0"'.($transaction == '0') ? $chkvalue:"".'/><label for="listings">Listings</label>
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' .($transaction == '1') ? $chkvalue:"".'/>';
Using the ? operator, you can check a condition and echo something out if it equates, or nothing if it doesn't.
You cannot concatinate an entire if statement. It cannot be converted to a string.
You can change it to an expression with a ternary operator though:
echo 'first part of your string ' . ($transaction == '0'?'$chkvalue':'') . ' rest of your string';
Even better (I think), you can put the HTML outside of PHP tags. You can briefly open PHP tags again to execute and if statement to conditionally echo a value, or you can use the short <?= XYZ ?> tag which is short for <?php echo XYZ ?>.
This way of working is very convenient when working with larger chunks of HTML, because most syntax highlighters will actually highlight the HTML, and you won't need to escape anything. If you 'prepare' your variables before and insert them using the short <?= tags, your HTML will look like a clean template-like piece of code with only those variables in it.
<?php
foreach( ... )
{?>
<input name="flyer" type="file" id="<?=$row['ad_link']?>" tabindex="9" /></td>
<input name="transaction" type="radio" tabindex="11"
value="0" <? if($transaction == '0') echo $chkvalue; /* Actual statement */ ?>/>
<label for="listings">Listings</label>
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12"
value="1" <?= ($transaction == '1'? $chkvalue : '') /* Just output an expression */ ?>/>
<?}

Auto Filled textbox in php

I am a beginner.
I created a simple generator of pictures and I would like that when he was generate code in the text box and not as plain text.
Please help.
So it should look like: http://i.stack.imgur.com/cXT7X.jpg
I suppose you are doing something like
echo $code;
to show your code; if this is the case so you only have to do it like this :
echo '<input type="text" name="my_code" value="' . $code . '">';
or
echo '<textarea name="my_code">' . $code . '</textarea>';
That's it
Use basic HTML FORM elements with readonly attribute.
<textarea readonly><?php echo $code; ?></textarea>
Just keep in mind that PHP generates HTML that the browser parses...
<input type="text" name="url" value="<?php echo htmlspeciachars($url,ENT_QUOTES); ?>">
its important to use ENT_QUOTES or some other kind of data filter to avoid XSS.
I didn't understand what i am saying but if you want to set the text inside the text input than you should use value element in input tag. For example your code should look like this :-
<input type="text" value="<?php echo $yourvalue;?>" />
Hope this works for you.

Execute PHP Echo inside input tag (text field)

I'm using a function inside a php script to return a text field containing a value. This value is retrieved from a variable. How can I make this possible?
This is the code that I tried to use but it didn't work.
function display_name()
{
return '<input type="text" maxlength="50" readonly="readonly" value=" '. $current_user->display_name .'" name="CreatedBy" style="width:100%">';
}
By handing the function the variable as a parameter.
function display_name($current_user)
{
return '<input type="text" maxlength="50" readonly="readonly" value=" '. $current_user->display_name .'" name="CreatedBy" style="width:100%">';
}
then calling it like
display_name($current_user);
I see absolutely no point in having such a function.
It will make your code bloated and hard to maintain.
Why not just have this piece of HTML just along other HTML codes in the template?
<input type="text" maxlength="50" readonly="readonly" value="<?=$current_user->display_name?>" name="CreatedBy" style="width:100%">
or at least make it more generalized, to make it call like this
html_input("text",'CreatedBy',$current_user->display_name,
array("maxlength=>"50",readonly=>"readonly",style=>"width:100%"));
to use such a function for the every input on the site.

Categories