php display info in form - php

I have a function allowing members to update their personal details when they have logged in, the update page features a form the user will fill in and when the submit button is pressed the deatils are updated. This works fine, I am wanting to display the users current details in the appropriate text filed in the form e.g. the firstname text field will have value "James" and the user can delete this to enter "jim". Is it something to do with adding a value to the form field? My form is shown below.
<tr>
<td width="78">Firstname</td>
<td width="6">:</td>
<td width="294"><input name="firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td width="78">Surname</td>
<td width="6">:</td>
<td width="294"><input name="surname" type="text" id="surname"></td>
</tr>
Any help is much appreciated

Print value inside the text box.
<input type="text" value="<?php echo $someValue; ?>" />

echo '<input type="text" value="'. $someValue .'" />';
OR
<input type="text" value="<?php echo $someValue; ?>" />
Both obviously require that you be in a .php file, and that $someValue contains the appropriate value to set. Watch out for the double quotes around the value, too. Without them, any spaces will break the value when it's rendered.

Related

Get value of a table data on button click

I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:
<form method="post" action="exam_page.php" >
<table >
<tr style="background-color: #7F859E;color:white; height:50px;">
<th style="padding-left:140px; width:550px;">Paper</th>
<th style="padding-left:40px;">Time</th>
<th style="padding-left:40px;">Duration</th>
<th style="padding-left:40px; width:250px;"></th>
</tr>
<?php
$i=1;
while($row=mysql_fetch_array($rs)){?>
<tr style="height:80px; background-color: #CCCCCC;">
<td style="padding-left:40px;">
<input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
</td>
<td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
</tr>
<?php $i++; } $_SESSION['exam']=$i; ?>
</table>
</form>
Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.
<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
Start
</button>
Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)
To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.
<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">
and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.
If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.
Try using javascript onclick functions:
<script>
function getPaperCode(paperCode)
{
alert(paperCode);
}
</script>
Then edit your input add onclick event:
<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>
Once you click. it will alert the value of the button
passed your unique id value or examcode via hidden field like this
<input type="hidden" name="id" value="<?=$row['eid']?>">
and on button click perform query like
$id=$_POST['id'];
select * from table_name where id='$id';

Unable to add all columns entered on phpMyAdmin

I have created a web page where my customers will be able to enter details of all items there return back to my store. I have created in such a way that, 1 customer can enter his personal details once but can add any number of items he is returning. I have created a "add more" button to do this job. But when I am testing my site, database is only taking the last item entered and is not storing previous items.
<script> $(function () {
$('input.more').on('click', function () {
var $table = $('#input_fields');
var $tr = $table.find('tr').eq(0).clone();
$tr.appendTo($table).find('input').val('');
});
});
</script>
<tr>
<td width="33%"><label class="description" for="element_20">Manufacturer, Model, Serial # </label>
</td>
<td width="33%"><label class="description" for="element_10">Tag Number (If Any) </label>
</td>
<td width="33%"><label class="description" for="element_4">Item Description </label>
</td>
</tr>
</table>
<table id="input_fields" width="100%" border="0" cellpadding="4">
<tr>
<td width="33%"><input id="element_20" name="model" class="element text large" type="text" maxlength="255" value="<?php if($action != "add") echo $row["model"]; ?>"/></td>
<td width="33%"><input id="element_10" name="tag" class="element text large" type="text" maxlength="255" value="<?php if($action != "add") echo $row["tag"]; ?>"/></td>
<td width="33%"><input name="itemdesc" type="text" class="element text large" id="element_4" value="<?php if($action != "add") echo $row["itemdesc"]; ?>" /></td>
</tr>
</table>
<input class="more" type="button" value="Add more" name="addmore"/>
<ul>
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="Submit" value="Submit" />
In this, when Customer enters a item details and then clicks add more and enter other item details. Only the last enter item details are getting stored. But I want all items to be stored and displayed on DB.
You end up posting only a single model/tag/text to the server because you have the same name property for each copy of the input elements. When PHP tries to map the posted data to $_POST it will only map to a single model/tag/itemdesc key in $_POST because the input names are the same.
The easiest way to resolve this is to use array access notation in your input names like model[], tag[], itemdesc[]. This will allow PHP to assemble an array of values for each model/tag/itemdesc key in $_POST.
Try this. Do a var_dump($_POST) at the beginning of your script before changing your HTML code. Run a multi-item test case and look at the dumped value.
Then change to use array access notation in your HTML and see the difference the var_dump() gives.

PHP POST value from a looped table

Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download

Input value entering to database

<td height="25" align="center" valign="middle"><input type='text' name='day2' id='datepicker2' value='Day 2' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day3' id='datepicker3' value='Day 3' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day4' id='datepicker4' value='Day 4' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day5' id='datepicker5' value='Day 5' readonly="readonly" maxlength="50" style="color:#999999"/></td>
The values Day 4, Day 5, Day 6 etc. are enetring into database. How I block these from entering into database?
How I block these from entering into database?
It sounds like you have some system that takes all your values and posts them to the database... But we can not answer your question without seeing the php code.
Forms have little to nothing to do with mysql insert commands. They just receive information from the browser.
It isn't the most popular method but, you can use the placeholder attribute to still display the default value you want to display and leave the value attribute blank. I do this with a jQuery date picker application.
<input type="text" name="day4" id="datepicker4" value="" placeholder="Day 4" readonly maxlength="50" style="color:#999999"/>
This gives a grayed out effect while still showing the user what should go there. When a value is entered the value attribute takes over.
Also, I have just corrected the attribute tags from single quotation marks to double as well as the readonly attribute.

How to show the result when I click the submit button? (Like View history)

Here's the concept. The users will input the data from the text bar.. And when click the submit button, the data will be saved to the "VIEW HISTORY" menu bar. View history is where the data's inputted from the home.php are being made.
E.g:
Home.php
(data1)
Enter Name: (I type..)Black Cat
Age: (I type..)21
Job: (I type..)Spy
(data2)
Enter Name: (I type..)Black Dog
Age: (I type..)24
Job: (I type..)Cook
Submit Button (I click it) and it says the data is submitted to my database.
Then when I click the "View History Menu Bar".. The ff will be shown:
DATA1
DATA2
<Click next to view next page.. etc>
When I click data1, the whole name, age and job from data1 will appear. And so is from data2.. and so on.. :)
That's my only problem.. I can't show those data :( Please help! I have some codes in here.. But I dunno how to fix it :( I hope you can give some samples so I can imitate the moves, or better if we help on fixing this. :( THANKS Y'ALL!
<?php
$cfccon = include("E:/xampp/conn1/cfmscsd.php");
//This is about the localhost, username, password stuff
$query="SELECT * FROM contents";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$B1=mysql_result($result,$i,"B1");
$B2=mysql_result($result,$i,"B2");
$B3=mysql_result($result,$i,"B3");
$B4=mysql_result($result,$i,"B4");
$B5=mysql_result($result,$i,"B5");
$B6=mysql_result($result,$i,"B6");
$B7=mysql_result($result,$i,"B7");
$B8=mysql_result($result,$i,"B8");
$B9=mysql_result($result,$i,"B9");
$B10=mysql_result($result,$i,"B10");
$i++;
}
?>
<html>
<tr>
<td bgcolor="#bfb9a7"><span class="style77">CCC</span></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B1" type="text" size="18" id="B1" value="<?php echo $B1;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B2" type="text" size="18" id="B2" value="<?php echo $B2;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B3" type="text" size="18" id="B3" value="<?php echo $B3;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><center><div class="style66"><input type="button" onclick="tbsp1()" value="<?php echo $tbsp1;?>add" disabled="disabled"/></center></td>
<td bgcolor="#FEEDD8"><div align="center" class="style66"><input name="B4" type="text" size="18" id="B4" value="<?php echo $B4;?>" disabled="disabled"/></div></td>
</tr>
<tr>
<td><span class="style77">SCC</span></td>
<td><div align="center" class="style66"><input name="B5" type="text" size="18" id="B5" value="<?php echo $B5;?>" disabled="disabled"/></div></td>
<td><div align="center" class="style66"><input name="B6" type="text" size="18" id="B6" value="<?php echo $B6;?>" disabled="disabled"/></div></td>
<td><div align="center" class="style66"><input name="B7" type="text" size="18" id="B7" value="<?php echo $B7;?>" disabled="disabled"/></div></td>
<td><center><input type="button" onclick="tbsp2()" value="<?php echo $tbsp2;?>add" disabled="disabled"/></center></td>
<td bgcolor="#FEEDD8"><div align="center"><input name="B8" type="text" size="18" id="B8" value="<?php echo $B8;?>" disabled="disabled"/></div></td>
</tr>
<tr>
<td bgcolor="#bfb9a7"><span class="style77">NCC</span></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B9" type="text" size="18" id="B9" value="<?php echo $B9;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B10" type="text" size="18" id="B10" value="<?php echo $B10;?>" disabled="disabled"/></div></td>
</tr>
</html>
First of all, I recommend that you rename your table columns, B1, B2, B3 etc are very very bad names, instead use something descriptive like name, age, email, etc. That will make your code a lot easier to read and debug.
To achieve what you're asking for, do something like this:
1) When the user clicks the submit button, do a form post to a script on your site, e.g
<form method=post action=home.php>
Enter data: <input type=text name=data>
<input type=submit value=Submit />
</form>
2) When the user clicks the submit button, you could get the data he posted in $_POST, e.g $_POST['data']. Save this to a mysql database. You should give the user a unique user ID. Each row in your table should have these fields:
dataId (int,primary, auto_increment)
userID (int)
name (varchar 255)
age (int)
etc...
Then you should have a different table for users, called a user table. Each user who visits the website should have a unique userId. You can store this userId in $_SESSION, I recommend you read about session handling.
Then whenever the user submits his form, you add a new row to your data table with his info, and the userId field with the given user's id.
3) When the user clicks 'next' to see the data, you would do something like this:
$userId = $_SESSION['userId'];
$sql = "SELECT * FROM data WHERE userId='$userId'";
$result = mysql_query($sql) or die(mysql_error());
$history = array();
while ($row = mysql_fetch_assoc($result))
{
$history[] = $row;
}
4) Then simply loop through the $rows using foreach and display the history.

Categories