how can I evaluate the following nested quotes echo statement? - php

My problem is that I want $id to be evaluated and passed. I attempted to use escape quotes and failed. How can I do it properly?? if I put "" or '' I always get $id to be passed as a string $id
echo '<form name="aform" action=go.php?id=$id" method="POST">'.
'';

Properly concatenate it to the string
echo '<form name="aform" action=go.php?id='.$id.'" method="POST">';

You can also use curly braces as a quicker form of concatenation assuming you used double quotes instead of single:
echo "<form name='aform' action='go.php?id={$id}' method='POST'>";
OR if you want double quotes to remain in your html you can escape them
echo "<form name=\"aform\" action=\"go.php?id={$id}\" method=\"POST\">";
Give this page a read:
http://php.net/manual/en/language.operators.string.php

Related

Php add value to input field?

Is it possible to add a value to a dynamically created input field?
Im trying something like this: echo "<input value="$row['test']">" but that gives me a syntax error. It has to be in my php file since im calling it via ajax and want to keep my html and php seperate.
Im getting content via ajax and I need to set many field names as there are records in the database.
You can do it like this:
echo '<input value="' . $row['test'] . '">';
Alternatively escape the " (not recommended if not needed, because it is hardly readable):
echo "<input value=\"" . $row['test'] . "\">";
Otherwise you’re mixing quotation marks, which is a syntax error. The . is to combine strings with variables (in this case).
You can also Use:
<input value="<?php echo htmlspecialchars($row['test']); ?>" >
OR
echo "<input name='test' value=".$row['test'].">";
OR
echo "<input name='fred' value='{$row['test']}'>";
Reference
When using certain php values within a quoted string, such as the array syntax in the question, you should either escape from the quotes or encapsulate the variable in curly braces. Also, as the string was quoted with double quotes you should use single quotes around attributes and values.
echo "<input name='fred' value='{$row['test']}'>";
<input type="text" name="post_title" class="form-control" value="<?php
if(isset($post_title))echo $post_title;
?>">
If you want to add it into the HTML

php echo outputs only part of a variable in a form

I'm loading a variable from a database like:
$adres = $row['adres']; //(= "Hoge Filterweg")
Then using it in a echo like:
echo input type='text' name='adres' value='{$adres}'
It displays on the form only the first part of the adress ( "Hoge"), but not the whole adress.
What could I do now?
Single quotes surrounding the inline variable like value='{$adres}' dit the trick.
thanks
You need double quotes to pass params with space.
This code should help you:
echo "<input type=\"text\" name=\"adres\" value=\"{$adres}\" />"

Use form input to build url with php.

I'm trying to create a form where the user can input their id (username) and it will be appended as a variable in a url that is used in my php script. This is what I have.
<?php
if(isset($_POST['submit']))
{
$id = $_POST['id'];
echo 'http://example.com/default.asp?action=data&id=$id';
}
?>
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="id"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
It collects the user's id properly, and if i just echo $id, it outputs the proper value, but when I try to echo the url, it just outputs $id instead of the actual value of the $id variable. What am I doing wrong?
echo "http://example.com/default.asp?action=data&id=$id";
^---wrong quotes ^--- ditto
single-quoted strings do not interpolate variables.
Single quotes won't interpolate the variable, either use double quotes or use string concatenation.... Three options:
echo "http://example.com/default.asp?action=data&id=".$id;
or
echo "http://example.com/default.asp?action=data&id=$id";
or
echo 'http://example.com/default.asp?action=data&id='.$id;
This line:
echo 'http://example.com/default.asp?action=data&id=$id';
Should be
echo 'http://example.com/default.asp?action=data&id='.$id;
If you are using single quotes in PHP with a string it will print whatever is inside the string without evaluating anything (ie no variables are evaluated). So you can either use double quotes or append the variable like I did above.

How do I Escape PHP inside of a echo function?

What I have is this:
function add_email_form () {
echo "<form class=\"email-me-form\" id=\"initialize\" action=\"<?php echo $_SERVER[PHP_SELF] ?>\" method=\"post\" name=\"contact_me\">\n";
}
How do I make this syntactically correct?
Don't use double quotes unless you need to. Use single quotes, '. That way, you don't have to escape anything except control characters like the \n, and in that case, do drop to double quotes. So the above would be:
echo '<form class="email-me-form" id="intialize" action="'.$_SERVER['PHP_SELF'].'"
method="post" name="contact_me">'."\n";
(newline added to I don't cause a horizontal scrollbar)
You don't need to do htmlspecialchars() looks like what you want.
You don't need to (and in fact, cannot) call "<?php echo?>" inside a PHP statement. Only when you're outside of PHP does that work. In this case, just concatenate with ..
And as stated in the comments, you should quote array keys when they're strings, as otherwise PHP will throw a warning and could potentially be confused.
Something like so:
echo "<form class=\"email-me-form\" id=\"initialize\" action=\"",
htmlspecialchars($_SERVER['PHP_SELF']),
"\" method=\"post\" name=\"contact_me\">\n";
function add_email_form () {
echo "<form class=\"email-me-form\" id=\"initialize\" action=\"" . $_SERVER[PHP_SELF] . "\" method=\"post\" name=\"contact_me\">\n";
}

Text containing single quote as input value [PHP]

I try to show this string : «let's go» in a the value of an input tag
i wrote:
$chaine="let's go";
echo "<input type=text name=test value='".$chaine."'>";
result: let
What can i do to show the correct string in this case ?
use htmlspecialchars
echo "<input type=text name=test value='".htmlspecialchars($chaine, ENT_QUOTES)."'>";
You can look at htmlentities()
htmlentities($chaine, ENT_QUOTES) ;
This produces
<input type=text name=test value='let's go'>
You can see, that for HTML (--> your Browser) the value ends after "let". Everything after that is invalid (but ignored). Escape
$chaine = "let\'s go";
However, in HTML double quotes are prefered and omitting the quotes is also no good style
$chaine="let's go";
echo '<input type="text" name="test" value="'.$chaine.'">';
In this case you must escape every double quote.

Categories