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;
Related
I have read a lot of question here but I couldn't get anything to work. I have such a instructions:
$query = "SELECT nazwa,rok_prod,wypornosc FROM statek where id_statek=$id";
$wynik = pg_query($query);
$liczba_kolumn = pg_num_fields($wynik);
echo "<form action=edos.php method=post>";
echo "<table border width=1>";
for($k = 0;$k<$liczba_kolumn; $k++)
{
echo "<tr>";
echo "<td>";
echo pg_field_name($wynik,$k);
echo "</td>";
echo "<td>";
echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";
echo "</td>";
echo "</tr>";
}
echo "</table>";
And I want to display values from SELECT in fields, that I could change it later - it is for editing form. I have tried in a lot of ways:
<input type="text" name="name" value="<?php echo $name;?>" />
or
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
but nothing is working. I have tested pg_fetch_result($wynik,0,$k) and there is what I want but how to display it and make it editable?
First, I recommend you to read about PHP Strings, especially the difference between single and double quoted strings and how to escape characters.
Second, you're forgetting to add the double quotes around the HTML attribute values, forgot a dot and am missing a space. This line:
echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";
Would output something like <input type=text name=foovalue=bar>, assuming that your first function call will return foo and the second returns bar.
What you need is, for example:
echo "<input type=\"text\" name=\"".pg_field_name($wynik,$k)."\" value=\"".pg_fetch_result($wynik,0,$k)."\">";
Which should output <input type="text" name="foo" value="bar">
As this tends to get messy and is therefore error-prone, I would recommend you to look into parsing strings with functions like printf.
Apart from that, I am guessing that your functions pf_fetch_name and pg_fetch_result are not returning anything (or an empty string), and therefore you get empty input fields. Hence, the error might lie within these function and/or the SQL queries they are (probably?) carrying out. This is what you should look into.
Edit:
To get things a bit tidier, I would further recommend to avoid all the echos. This can be done by simply having the actual HTML markup outside of the <?php ?> tags and then injecting your values with the shorthand tags <?= ?>. A shortened example:
<?php
// Some PHP code
for ($i = 1; $i <= $something; ++$i) {
?>
<table>
<tr>
<td><?= fetch_foo(); ?></td>
<td><?= fetch_bar(); ?></td>
</tr>
<table>
<?php
} // closing the for loop
?>
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]' />
--------------------^
can anyone tell me why is that the action in the form tag is not working on google chrome?i use echo to display the the table and form..
i have this code..
echo "<form method='post' name='computation' action='savepagibigcomputation.php'>";
echo "<table>";
echo "<tr>";
echo "<td>Blocknumber:</td><td class='reset_border_left'><b><label>$blocknumber</label></td>";
echo "<input type='text' name='blocknumber' value='$blocknumber'>";
echo "</tr>";
echo "<tr>";
echo "<td>Lotnumber:</td><td><b><label>$lotnumber</label></td>";
echo "<input type='text' name='lotnumber' value='$lotnumber'>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='5' align='right'><input type='submit' name='save' value='RESERVE' class='button'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
so when i click the submit button the action should be excuted. In firefox is all working but in google chrome i really having a hard time to figure out why it will not re direct to the said action above??
any help is highly appreciated..
any one can help please..
Your HTML is not well-formed. Your <input type="text"> tags end outside of table cells. Make sure your inputs are inside cells (<td> or <th>) of the <table>.
If something doesn't work, validating your HTML (in browser right click -> view source) with W3C HTML validator should be the first step in debugging, before trying to think what else could be wrong.
If fixing the HTML doesn't work, try looking at developer tools in Firefox (Firebug) or Chrome (Developer tools are integrated in Chrome) both at the DOM and the HTTP request the browser tries to make when you hit submit.
If it still doesn't work, you should provide us with a link so we don't have to guess (especially when your HTML is dynamic) what's wrong - we could instantly see what's wrong. For all we know, the issue might not even be in the code you posted, it could be a HTML formatting error somewhere else in <head> or <body>
Sometimes Chrome can mess with markup if there is an error in it. I suspect that the error is due to some other part of the code and not the part you have pasted here. Try this extract, it works on my chrome, this will rule out that element of your problem:
<?
$blocknumber = 1;
$lotnumber = 3;
echo "<form method='post' name='computation' action='savepagibigcomputation.php'>";
echo "<table>";
echo "<tr>";
echo "<td>Blocknumber:</td><td class='reset_border_left'><b><label>$blocknumber</label></td>";
echo "<input type='text' name='blocknumber' value='$blocknumber'>";
echo "</tr>";
echo "<tr>";
echo "<td>Lotnumber:</td><td><b><label>$lotnumber</label></td>";
echo "<input type='text' name='lotnumber' value='$lotnumber'>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='5' align='right'><input type='submit' name='save' value='RESERVE' class='button'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
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=\"\"
I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}