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
?>
Related
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 the following program, it searchs for the text placed in a previous php file, and it displays the results, by adding a radiobox to check the item that will be purchased. I am not able to make the page save the item that was checked from the items found into a new table, I don't know how to do that, because the items found are placed as fetched items, therefore I don't know how to select one to save the entire row selected. Please help!.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Search option</title>
</head>
<body>
<?php
echo "<form action='slips.php' method='post'>";
if(isset($_POST['name_prod2'])){
$word=$_POST['name_prod2'];
$conn = oci_pconnect('dbname', 'password', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['Error'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, "SELECT * FROM product WHERE LOWER(name) LIKE '%" . $word . "%'");
oci_execute($stid);
echo "<table width='950' table border='1' align='center'>\n";
echo "<tr>\n";
echo "<th width='50'> <div align='center'>buy</div></th>";
echo "<th width='110'> <div align='center'>Product ID</div></th>";
echo "<th width='190'> <div align='center'>Product name</div></th>";
echo "<th width='250'> <div align='center'>Description</div></th>";
echo "<th width='100'> <div align='center'>in Store</div></th>";
echo "<th width='100'> <div align='center'>price</div></th>";
echo "<th width='190'> <div align='center'>Quantity to purchase</div></th>";
echo "</tr>\n";
while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
//echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>";
echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>', $product['product_id']);
foreach ($product as $aspect) {
echo '<td><div style="text-align:center">'.($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')."</td></div>\n";
}
echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>';
}
echo "</table>\n";
}
echo "<div style='text-align:center'><input type='submit' value='Comprar'></div>";
echo"</form>";
?>
</body>
</html>
These are two of the Javascripts that I have tried so far to complete this, but they fail to tell me when one has been selected, I don't know if I can try adding this code to a button, and when I click it, it will tell me which row from the radio box was checked, and then save it:
<script type="text/javascript" src="js/jquery.js"> </script>
<script type="text/javascript">
var user_cat = $("input[radio1='user_cat']:checked").val();
if (!$("input[radio1='radio1']").is(':checked')) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
$(document).ready(function() {
$('#btnStatus').click(function(){
var isChecked = $('#rdSelect').prop('checked');
alert(isChecked);
});
});
</script>
When is use this line echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>"; it works and displays this results
But when I use this line echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%d"></label></td></div>', $product['product_id']);it doesn't work and displays this results
OK, picking up the information from the comments to the question and doing a little guess work I will try to point you into the right direction. It is not possible to give a read-to-use answer, since still there are things not clear, but let's have a try to get started...
I see you have an html form which includes a table. That table has a header row and dynamic generated rows holding some product information each. You want to have a radio button in front of each row to allow to select a row. And you want a text input field at the end of each row which allows to enter a quantity. Then you want to post that information to the server to be able to process it.
I will stick with the "conservative" html approach and not introduce scripting here. Reason is that for the purpose described before that is not required. So let's keep things simple. Obviously nothing speaks against making things more complicated later on :-)
Your radio buttons have to be changed, they currently make no sense. You have to give the an individual value, so that you can identify which row has been selected later on. Currently you give them all the same static value 'value'. So change the loop that iterates over the products to something like:
while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>'."\n", $product['product_id']);
foreach ($row as $aspect) {
echo '<td><div style="text-align:center">'
.($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')
."</td></div>\n";
}
echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>'."\n";
}
Note: I took the liberty to change the chosen names to be more logical to product, aspect and quantity...
That is all... Now when you press the submit button the form should get posted to the target you specified: slips.php which is probably a script of yours... Inside that script you now can access the data like that:
$product = $_POST['product'];
$quantity = $_POST['quantity'];
There are more issue worth discussing and modifying, but as said: let's keep things simple and take one step after the other!
ChangeLog:
changed the literal key of the array element used as a value inside the radio button definition from id to procduct_id according to one of the comments below
Your radiobox need to stay inside the form.
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++;
}
it's should be something really simple if its possible. anyway of doing so? can't seem to find it online.
i have a set of checkboxes and textboxes beside it. i only want the textboxes to appear if i check it. my ids are dynamic using php so i cant use javascript out of the loop.
the code below is to test if it works to hide. i am able to hide on click but iam hoping to do the other way round.
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='hidden';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\"></td><br />
</tr>";
$TBCounter++;
}
You can use Javascript's window.onload to make something run every time the page loads.
Before your loop...
$elementsToHide = array();
During your loop, put this
$elementsToHide[] = $TBCounter;
Then make your script (after the loop) look like this
<script type="text/javascript">
window.onload = function ()
{
<?php foreach ($elementsToHide AS $TBCounter): ?>
document.getElementById('TBox <?php echo $TBCounter ?>').style.visibility = 'hidden';
<?php endforeach ?>
}
</script>
There are probably better (read: less intrusive) ways, but the simplest is probably to set the visibility of the element to hidden and change your Javascript to make it visible.
So, add style="visibility:hidden;" to the textbox element and change the javascript to set .style.visibility = 'visible'
Editing your code, that would be:
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='visible';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\" style=\"visibility:hidden\"></td><br />
</tr>";
$TBCounter++;
}
As per Joe's comment, this will not work for users with javascript disabled as the field if hidden when the page loads. One way around this is to set the field invisible in javascript by outputting an inline script tag.
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='visible';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\"></td><br />
</tr>
<script>document.getElementById('TBox $TBCounter').style.visibility='hidden';</script>";
$TBCounter++;
}
A third (better) alternative could be to set a class for the textboxes and use javascript to hide all elements with that class on page load.
Here is how I am echoing back a 'normal' table cell in PHP:
Code:
echo "<tr>";
echo "<td>Some cell data</td>";
echo "</tr>\n";
Attempt to echo a button within a cell:
echo "<tr>";
echo "<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>";
echo "</tr>\n";
Thank you for any help, this is quite confusing to me. I am a beginner when it comes to PHP so please pardon any errors and feel free to kindly correct me wherever a correction is needed!
You need to either escape your double quotes " that appear inside your string surrounded by double quotes, or use a different quote style (i.e. single quotes ') to surround the string that contains the double quotes. It's actually made quite obvious what the problem is by the syntax highlighting on this site.
You could either do this:
echo "<tr>";
echo "<td><form action=\"insertdata.php?page=add\" method=\"post\">
Username: <input type=\"text\" name=\"username\" >
<input type=\"submit\" >
</form>
</td>";
echo "</tr>\n";
...or this...
echo "<tr>";
echo '<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>';
echo "</tr>\n";
I suggest you read this thoroughly so you no what you can and can't legally do with strings in PHP.
As a side note, it is a bad idea to put new-line literals inside quoted strings, as it can lead to cross-platform compatibility wierdness. It's not really a problem with web output like this, but it's a bad habit to get into. You should either use \r,\n and \r\n as appropriate, or if you must put literals into the source code, use heredoc syntax. Remember that escape sequences like \r\n are only interpolated in double quoted strings!!!