Is it possible to use PHP variables as attributes in form inputs?
I have the following variable already declared:
$InStock //number of items in stock
I would like to use this variable per below, but the code isn't working (the max constraint is not being applied to the field):
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Is this legal, or am I attempting something syntactically impossible? Sorry if this is a stupid question - I'm new at this, and I embarrass myself frequently. : /
It's possible, your syntax is incorrect though. You're double dipping on the php brackets.
I usually prefer to do something like this instead of using print, it's easier to read, especially if your IDE is doing syntax highlighting:
?><td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td><?php
You could also do it within a print/echo statement, but you need to have the variable inline without the extra brackets:
echo "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
Note that without concatenation, this only works with double quotes.
Instead of your:
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Use:
print "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
This is because PHP will insert the values of variables into a string if you put the variable name into the string and PHP will replace this with the value of your variable. For example print "Hi there, $name"could produce "Hi there, James" when executed - assuming $name is equal to "James".
Please check this Muddal: http://muddal.com/muddaltut.php?id=163
Related
Okay, so typically I would write the following:
<input type='text' class='form-control' name='name' value='<?=$user['name'];?>'>
However, because I am using ' in my HTML, and if the name has a ' in it, (i.e. the last name is O'Brian for instance) It doesn't echo correctly, because the value is ending the input abruptly.
Of course a simple solution is to use " quotation marks with my html, but that doesn't help - because what about when I want to echo quotation marks as well? What can I do?
Use <input type='text' class='form-control' name='name' value='<?php echo htmlentities($user['name'], ENT_QUOTES); ?>'>
i want to print a input value using html.
input will be like below..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row['$tr_id'];'>
i am using below code to get the required input
$field_name='tr_id';
$field_type='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
echo $html;
but i think i am getting error on value. can you please help how can i print
Your intent is not so clear, however it might be logical to assume that you may really have a typo in your PHP Code where you were echoing out the field_name.
Here is a correction on that part.
<?php
$field_name ='tr_id';
$field_type ='text';
$html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />";
echo $html;
Notice that the line: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='<?php echo $row['".field_name."']'>";
has changed to reflect what is presumably what you intended to do: $html .= "<input type='".$field_type."' name='".$field_name."' class='form-control' id='".$field_name."' style='height:35px;width:200px;' value='{$row[$field_name]}' />"
The reason for this is because you are already in PHP Mode so you don't need to do something like <?php echo $row...?> within a PHP block... (meaning: you don't open & close a PHP Block within an already existing PHP Block) and, in fact, since you are doing echo $html it would be pointless to echo anything again within the code that builds up your string. The Point here is that you should rather build-up your String without echoing anything and then finally echo $html once you are done building up your HTML String. This way you'd avoid the Errors you got.
try below code..
<input type='text' name='tr_id' class='form-control' id='tr_id' style='height:35px;width:200px;' value='<?php echo $row[$tr_id];'>
And
$field_name='tr_id';
$field_type='text';
$row = array('field_name','test');`enter code here`
$html .= '<input type ="'.$field_type.'" name="'.$field_name.'" class="form-control" id="'.$field_name.'" style="height:35px;width:200px;" value="'.$row["field_name"].'" >';
echo $html;
I have this code and it works.
echo "<input type='submit' name='liga' value='Liga'>";
if (isset($_POST['liga'])) {
unset($_POST['liga']);
liga();
?>
But I need change the name LIGA to the php variable $on. I tried this but don't work.How can I insert the variable in this code?
$on=1;
echo "<input type='submit' name='$on' value='Liga'>";
if (isset($_POST['$on'])) {
unset($_POST['$on']);
liga();
}
?>
The problem here is that variables inside single quotes don't get interpolated whereas variables inside double quotes do.
echo "<input type='submit' name='$on' value='Liga'>";
becomes
<input type='submit' name='1' value='Liga'>
But $_POST['$on'] stays the same. To solve this, use $_POST[$on] (or the equivalent $_POST["$on"]).
PHP
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<tr>";
for($i=0;$i<7;$i++){
if($i==0){
echo "<td><input type='text' val='$row[$i]' /></td>";
}
if($i>1){
echo "<td>$row[$i]</td>";
}}echo "</tr>";
}
Except for the first column, which contains the input tag, everything comes out fine. The input tag shows up, but it appears empty--nothing to see, nothing to highlight. If I do 'inspect element', however, I see it has the correct value according to the output from the queried table.
Any thoughts as to what causes this strange behavior and how to fix it?
You have to use basic HTML Tag
You have to use "value" instead of "val"
See the Basic HTML Tag
<input type="text" value="Nikunj"/>
You should use value attribute instead of val:
echo "<td><input type='text' val='$row[$i]' /></td>";
-----------------------------^
should be:
echo "<td><input type='text' value='$row[$i]' /></td>";
-----------------------------^
Change val to value. val is not a valid attribute.
<input type='text' val='$row[$i]' />
--------------------^
I have tried to search through the forums but I am a novice and am getting more confused.
I am trying to bring an input from a form and use it as a variable in a MySql query. A shortened version of the form is -
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input value=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
I am then trying to store the input into a variable using code -
$newVar = $_GET['leave'];
However I am getting an undefined index error.
Can anyone help with this? Sorry if its a very basic problem :)
The problem is with your HTML. You need to name the input.
echo '<input name="leave" class="text" autocomplete="off" type="text" value="' . $_SESSION['leave'] . '" />';
You declaring the "value attribute twice, you need to declare name:
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input name=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
echo '<form method="get" action="">';
echo "<tr><td>Leave:</td><td><input value='{$_SESSION['leave']}' class='text' autocomplete='off' type='text' name='leave'/></td></tr>";
echo "</form>";
If you use single quotes and doubles quotes alternating, you can make your code look nicer.
For your problem, you're missing your input name:
<input type=".." name="leave" ..>
Also, notice in your output of the field, you have the value set to the session value and an empty value near the end.
value=\"\"