PHP Refresh Page to fill HTML Dropdown - php

I have a HTML Form that is automatically filled with a variable from a SQL table. If I visit the page the dropdown is allways empty and I have to press "F5" once. Then the Dropdown is filled.
Is it possible to put a "Press of the F5 Key" once at the end of my code?
header("Refresh:0"); and other ideas I found online didn't really work.
<tr>
<td align="right">Art der Probe</td>
<td align="right">Type of Test Piece *</td>
<td><select name="inputprobetype"><option value="<?php echo "$row[probe_type]"; ?>"></option><option value="Faser">Faser</option><option value="Matrix">Matrix</option><option value="Halbzeug trocken">Halbzeug trocken</option><option value="Halbzeug ausgehaertet">Halbzeug ausgehaertet</option></select></td>
</tr>
<tr>
<td><input type="submit" name="submit" /><input type="reset" value=" Abbrechen"></td>
</tr>
Thanks for your help.

Related

PHP Submission form into MySQL db formatting

I have created a blog using PHP that submits a form into a MySQL databse. It works perfectly. What i want to know is how to get it to recognise when i've pressed enter to create a new line or to record a ' without getting a MySQL error. Basically i want to turn a normal submission form into a submission form like is used on this website.
<tr>
<td>Blog Title:</td>
<td><input type="text" id="edt" name="BlogTitle"></td>
</tr>
<tr>
<td>Blog Content:</td>
<td><textarea name="BlogCont" cols="50" rows="10" id="edt"></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="Name" value="<?=$_SESSION['Name']?>"></td>
<td><input type="submit" id="but" value="Post"></td>
</tr>
Any help would be appreciated.
For new lines with enter use nl2br (manual), for apostrophe you have to escape it with mysql_real_escape_string (manual).

Create Update Command at Same form With Many Data

I have a form with PHP language and I want to use it for inserting data, updating data, and deleting data at once. First row of table is for insert data, second table and other row is for edit or delete data.
So, I have create form like this:
FYI : UBAH is Update/Edit, HAPUS is Delete, TAMBAH is Add/Insert
I've already done with inserting data, but I'm stuck while Updating or deleting data because the ID that the button I choose is always at last ID (in picture is the ID always refer to "Sarjana") so it doesn't works because I can only update and delete the last data that called at table.
So, what I want to ask is, how to make it works without AJAX? Because what I search in Google, they said it can be done with AJAX, but I don't understand AJAX (except it will stuck forever without ajax). And I don't want to create an href link just like "beginner example" that throw it to another pages then edit/delete it.
Note: It's just like a listview at ASP but I want to do it at PHP code. Final Words, I want to create Inline editing at my PHP code.
I hope You understand and sorry for my bad English.
Add a unique id to each submit button:
<input type="submit" name="submit[1]" value="Submit">
<input type="submit" name="submit[2]" value="Submit">
<input type="submit" name="submit[3]" value="Submit">
Then you grab the id from $_POST or $_GET. For example:
Array
(
[submit] => Array
(
[3] => Submit
)
)
Edit:
Take this concept - HTML form elements to PHP arrays - and apply it to the other elements.
<table>
<tr>
<th>Program</th>
<th>Department</th>
<th></th>
</tr>
<tr>
<td><input type="text" name="program[1]" value=""></td>
<td><input type="text" name="department[1]" value=""></td>
<td>
<input type="submit" name="edit[1]" value="Edit">
<input type="submit" name="delete[1]" value="Delete">
</td>
</tr>
<tr>
<td><input type="text" name="program[2]" value=""></td>
<td><input type="text" name="department[2]" value=""></td>
<td>
<input type="submit" name="edit[2]" value="Edit">
<input type="submit" name="delete[2]" value="Delete">
</td>
<tr>
<td><input type="text" name="program[3]" value=""></td>
<td><input type="text" name="department[3]" value=""></td>
<td>
<input type="submit" name="edit[3]" value="Edit">
<input type="submit" name="delete[3]" value="Delete">
</td>
</tr>
</table>
Again: use the unique IDs to set each row's form elements apart.
Here's one method for grabbing the data in PHP. Obviously you need to get the ID from the submit button and then use that ID to reference elements in the $_POST or $_GET array.
if ( isset($_POST['edit']) && is_array($_POST['edit']) ) {
$key = key($_POST['edit']);
$program = $_POST['program'][$key];
$department = $_POST['department'][$key];
}

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

Is this possible to accomplish with html and php?

I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>

PHP can't find variable

So Im relatively new to PHP from ASP. And After converting alot of ASP code into PHP I have come across a problem where my PHP code seems unable to find the hidden variable I have set. It worked fine in ASP and was just wondering the best way to resolve this.
Start of the Form:
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
<table align="center" cellSpacing="2" cellPadding="2" border="0">
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">User name:</font> </td>
<td><input name="UserName" size="25" type="Text" autocomplete="OFF"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">Password:</font> </td>
<td><input name="Password" size="25" type="Password" autocomplete="OFF"></td>
<td> </td>
</tr>
PHP script:
<?
if ($_POST["BtnPress"]=="Pressed")
{
if ($_POST["Username"]=="*****" && $_POST["password"]=="*********")
{
$_SESSION['AdminID']="1";
header("Location: "."index.php");
}
else
{
print "<font color=#ff0000>Sorry you cannot access this part of the site.</font>";
}
}
?>
then the rest of the form:
<tr>
<td align="center" colspan="4">
<input type="hidden" name="BtnPress" value="Pressed">
<input type="Submit" value="Log In" class="mybutton" onclick="return CheckForm();">
</td>
</tr>
</table>
</td>
</form>
The PHP seems unable to find the variable BtnPress, its a similar problem throughout alot of my translated ASP to PHP script. Sorry if it is a simple solution but can anyone tell me where I am going wrong?
You have name="Password" and $_POST["password"]
Password != password
Watch your case.
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
That is invalid HTML. A <td> element cannot be a child element of a <form>. Browsers are likely to error recover in ways that break your HTML (e.g. by moving the form, but not its contents, outside the table). Do use a validator.
And stuff that isn't likely to be the cause of the problem, but is likely to be the cause of other problems.
Don't use layout tables
Do use the label element
Do use CSS for presentation (you can style your label elements instead of using the obsolete font element
Do not use a hidden input to test if a form is submitted.
use:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// processing of $_POST
}
The name of the submit button should be "BtnPressed" not the hidden field.
What I would do would be to set the form action to ?id=submit or something like that, and then check "if $_GET['id'] == "submit" then process data.
<form action="login.php?do=submit">
.... <input .....
</form>
<?php
if($_GET['do'] == "submit"){
//process data
} ?>

Categories