Wrap php variable in quotes before echoing - php

I need to use a php variable to echo the id into an input field:
<input type="text" id={{ strtolower($val) . "Field" }}/>
But with the code above the output is
<input type="text" id=waterField/>
I want the output to wrapped in double quotes like:
<input type="text" id="waterField" />
I am using laravel but am totally willing to accomplish it without.
Is there a simple way to accomplish this? Thank you.

That is pure php, considering you have short tags enabled.
<input type="text" id="<? echo strtolower($val) ?>Field" />

use
echo '<input type="text" id="'.strtolower(htmlspecialchars($val)). 'Field"/>';
OR idle way to use html and php is
<input type="text" id="<?php echo strtolower(htmlspecialchars($val)); ?>Field"/>
If $val can have quotes in it, then need to clear them by using htmlspecialchars.

Why not just:
<input type="text" id="{{ strtolower($val) }}Field"/>

You can simply add the double quotes into your html text. Escape the double quotes like this \" so php won't parse them and you're fine.

Related

Input filter/val output escape zf2 correctly?

Do I correctly understand, that I need to escape in view smth like this:
<div id="search-box">
<?php $escaper = new Zend\Escaper\Escaper('utf-8'); ?>
<input type="text" placeholder="<?php echo $escaper->escapeHtml($this->languageText('TEXT_SEARCH_OUR_SITE',"Search Our Site"));
?>" name="query" id="query" />
<div class="search-box-bk"></div>
</div>
And how correctly filter, or validate in controller this line:
$this->view->phrase = $this->getRequest()->getParam('phrase','');
How to escape something correctly depends on the context. In your example the variable is being output in a HTML attribute, so you should use escapeHtmlAttr:
<input type="text" placeholder="<?php echo $escaper->escapeHtmlAttr($this->languageText('TEXT_SEARCH_OUR_SITE',"Search Our Site"));
?>" name="query" id="query" />
There is a list of escape functions in the overview of the component.
As for what you need to do in the controller, the answer is: probably nothing, but it depends what you are going to use "phrase" for.

Using php variable in HTML

I am using a php variable in html. $userProfile['institute'] is displaying correct value whenever $userProfile['institute'] is non-empty. When $userProfile['institute'] is empty , it display '/'. What may be the issue here ?
Institute: <input type="text" name="institute" value=<?php echo $userProfile['institute']?> /><br />
You are missing a set of quotes for your value attribute
value="<?php echo $userProfile['institute']?>"
You should add double quotes around it like this (like you have wrapped value of type and institute attributes )
Institute: <input type="text" name="institute" value=" <?php echo $userProfile['institute']?>" /><br />

Can't get buttons to append to textarea

I'm building a restaurant menu application in PHP & JavaScript
And I can't seem to get the buttons to append the text area when the ordering is being placed...(This is just test code at present) Here's my code:
$test = mysql_query("SELECT * FROM main_stock");
while($row = mysql_fetch_array($test)){
echo "<div id='".$row["RCode"]."'><a href='javascript:addTo(".$row["Name"].",".$row["RCode"].")'>".$row["Name"]."</a></div>";}
And my javascript function is as follows:
function addTo(name, Rcode) {
document.getElementById('order').value += name;
}
And HTML Form is as follows:
<form id="OrderForm" name="OrderForm" method="post" action="">
<p>
<label>
<textarea name="order" id="order" cols="35" rows="20" readonly="readonly">test</textarea>
</label>
</p><table>
<tr><td>Subtotal:</td><td><input type="text" id="subtotal" readonly="readonly" value="2.00" /></td></tr>
<tr><td>Tax:</td><td><input type="text" id="tax" readonly="readonly" value="2.00"/></td></tr>
<tr><td>Total:</td><td><input type="text" id="total" readonly="readonly" value="4.00"/></td></tr>
<tr><td></td><td><input type="submit" id="submit" value="Send Order To Kitchen"/></td></tr>
</table>
Looks like the echo may be resulting in invalid JavaScript syntax. E.g.:
addTo(Something, Inc.,STI)
Rather than:
addTo("Something, Inc.","STI")
You'll need to output additional quotes for JavaScript to use. And, since you're already using " for the PHP string and ' for HTML attributes, this will require escaping -- or encoding:
echo "... href='javascript:addTo("".$row["Name"]."","".$row["RCode"]."")'>...";
Another option may be to use json_encode since JSON is based from JavaScript syntax:
echo "... href='javascript:addTo(".json_encode($row["Name"]).",".json_encode($row["RCode"]).")'>...";
TEXTAREAs don't have a .value, they have .innerHTML
Try
document.getElementById('order').innerText += name;
There's nothing with ID 'order' in your example.
Instead of document.getElementById('order'), try document.getElementById(Rcode).

Changing a variable in php file

How to add values from <input> to $var in php file?
html
<input type="text" id="align" name="align"/>
php file
<?php
$align="center";
?>
You mean you want to post it and put it in a PHP variable?
Try this:
<form action="somephpfile.php" method="POST">
<input type="text" name="align" value="center" />
<input type="submit" value="Send!" />
</form>
somephpfile.php
$align = $_POST['align'];
echo $align;
It depends on the form action.
If your form that is holding your fields has an action="post" attribute then from the php side you have to use $_POST['align']. If you have set the action to action="get" then you have to use the $_GET['align'].
<?php
$align = $_POST['align'];
// OR
$align = $_GET['align'];
?>
Just output it into the regular HTML value attribute:
<input type="text" id="align" name="align" value="<?php echo htmlspecialchars($align); ?>" />
The htmlspecialchars() call is there to escape things like quotes, to avoid problems if your variable contains quotes, and to make your mark-up more standards-compliant.

Textbox doesn't show properly

I'm using PHP to retrieve the data from a database.
Every time I retrieve it from database it doesn't show the whole letters. It's being removed off after space???
I want like the first text box, show all the letters
both code doesn't work
<input type="text" class="textinput" value=' . $r['newsletter_title'] . '>
<input type="text" class="textinput" value=' . htmlspecialchars($r['newsletter_title']) . '>
I checked the database it's showing the whole letters "80% sale On Day"
Does anyone knows what causes this? Any solution please!!!
You need to quote the value (and use htmlspecialchars):
<input
type="text"
class="textinput"
name="newsletter_title"
id="newsletter_title"
value="' . htmlspecialchars($r['newsletter_title']) . '"
/>
Which generates:
<input
type="text"
class="textinput"
name="newsletter_title"
id="newsletter_title"
value="80% sale On Day"
/>
Otherwise you're generating invalid html/xml (which is why it isn't working)...
Type escaping the content with htmlspecialchars() - http://www.php.net/manual/en/function.htmlspecialchars.php
The value of the value-attribute is not enclosed in quotes. Enclose the value of the value attribute in quotes. Use your browser to look at the HTML that you generate. Don't just look at how your browser renders that HTML.
<input type="text" class="textinput" name="newsletter_title" id="newsletter_title" style="width:500px;" value="' . htmlspecialchars($r['newsletter_title']) . '">
Problem Solved. Thanks guys!

Categories