HTML - forms best practice - php

It's OK to have a form with more submit buttons?
I mean I have a form which contains some input fields and a table and
3 buttons which will export some information in different formats
<form id="form1" name="form1" method="post" >
<label for="country"><strong>Countries: </strong></label><br />
<select id="country" name="country">
<?php
//some php code here
?>
</select>
<br />
<br/>
<label for="start_date"><strong>Start date: </strong></label><br />
<input name="start_date" type="text" id="start_date" value="<?php if(!empty($start))echo $start; ?>" size="20" />
<br />
<br />
<label for="stop_date"><strong>Stop date: </strong></label><br />
<input name="stop_date" type="text" id="stop_date" value="<?php if(!empty($stop)) echo $stop; ?>" size="20" />
<br />
<br />
<input type="submit" name="fill_table" value="Retrieve data" />
<div id="table">
<h1> </h1>
<hr />
<h2>Sales Report</h2>
<table class="display" id="data_table" >
<thead>
<tr>
<th>Product</th>
<th>Date</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
//some php code here
?>
</tbody>
</table>
<br />
<br />
<br />
<input type="submit" class="button" name="export_xls" value="Export xls" />
<input style="margin-left:10px;" type="submit" name="export_html" value="Export html" />
</form>
Is it ok to do it this way or should i make 3 forms each one having a particular submit button? Each time i press a submit button I'm intrested in the input fields :start_date, stop_date and the selected item from "country".
P.S. I want to know if it's optimal and how other programmers would handle this stuff

The main problem is: What happens when the user presses Enter in a text input field? On most browsers, the form data gets submitted, but does this correspond to pressing one of the buttons, and which one? See e.g. the question Multiple submit buttons in an HTML form.
It would be safest to have just one submit button per form. (However it would be safe to add submit buttons with identical functionality into a long form, for user convenience.) This means that the user would make a choice between alternatives (e.g., output formats) by using radio buttons or a select menu, not by a choice between buttons. This is not always practical, though.

I don't think it's a bad idea. But all your submit buttons must have the same name attribute.
Check out this

Yes, you can have more submit buttons in one form. Since you're using php to process the submited form, I wouldn't put the same name for each submit button as #NoZip suggested, but different instead, so that then in the php processing part of the code I would ask:
<?php
if (isset($_POST["submitButtonName1"]))
some code here...
else if (isset($_POST["submitButtonName2"]))
some other code here...
else
code logic for no submit button clicked
?>

I have just finished a system in which you cna update data records, one of the features I used was to use mulitple submits to allow the user to 'stamp' the update with different options. e.g 'active','pending', 'disable'. It saved a drop down box of multiple options and kept the system intuitive.
Multiple submit buttons with different values allow you to submit the form with the submit field having one of many values and can imrove a user interface. Plus, its valid HTML.

Related

php/mysql - form: How to keep the value of first search after second search?

I am using the value of search form Director1 to auto input in the value of "director_id_1" in the form CompanyDirectors, it is working.
However, if I use search form Director2 to auto input in the value of "director_id_2", it is working but meanwhile the value of "director_id_1" will be empty again.
So, how can I keep the auto input value of "director_id_1" after search Director2 ???
The below code is saved in the same page: Director.php
<h2> Company Director(s) - Input</h2>
<hr style="border: 1px dotted #2c1294;">
<form name="Director1" action="" method="POST" accept-charset="UTF-8" >
<input type="text" name="QueryDirector1" />
<input type="submit" name="DirectorName1" value="Search Name of Director 1 to input ID" />
</form>
<form name="Director2" action="" method="POST" accept-charset="UTF-8" >
<input type="text" name="QueryDirector2" />
<input type="submit" name="DirectorName2" value="Search Name of Director 2 to input ID" />
</form>
<hr style="border: 1px dotted #2c1294;">
<form name="CompanyDirectors" method="post" action="Director_insert.php" accept-charset="UTF-8" >
<b>ID of Director 1:</b>
<input type="number" name="director_id_1" required="required" value="<?php echo $director_id_1; ?>" >
<br>
<b>ID of Director 2:</b>
<input type="number" name="director_id_2" required="required" value="<?php echo $director_id_2; ?>" >
<br>
<input type="submit" name="submit" value="Submit">
</form>
Thank you very much for your help & support first !
If your question (which I find very hard to understand) is:
I want anything already submitted on the first form (Director 1) to be maintained upon submission of the second form (Director 2), and vice versa. How could I achieve this?
Then my answer would be use sessions or the database for persistence.
If I am understanding your question correctly, you do not understand that HTTP requests are (by nature) stateless. In other words: when you submit any form data, it is processed server-side and then ceases to exist. The "state" then disappears.
You could cache the submitted data temporarily in a $_SESSION[...] variable, or you could store it into the database (and retrieve it back out) or use HTML5 storage in the browser or do something less elegant like use hidden HTML inputs in the second form. (Or just use one form.)
Some useful links:
http://www.w3schools.com/php/php_sessions.asp
http://php.net/manual/en/session.examples.basic.php
https://en.wikipedia.org/wiki/Stateless_protocol
So for example:
<?php
session_start();
if (isset($_POST['DirectorName1'])) {
$_SESSION['directors'][1]['name'] = $_POST['DirectorName1'];
}
if (isset($_POST['DirectorName2'])) {
$_SESSION['directors'][2]['name'] = $_POST['DirectorName2'];
}
Then print the values from $_SESSION rather than $director_id_1 etc.
It's hard to help without a full copy of your code.

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.

PHP: how can I share one drop-down menu with two separate form inputs (one textarea, one file upload)

I currently have two html forms. They both POST to process.php: one posts text entered into a textarea, the other posts an uploaded file. The value of a hidden input called 'act' is either 'paste' or 'upload' respectively, and this is used by process.php to distinguish the two forms. The current code (below) works fine.
<!--Box to paste list of queries-->
<form enctype="multipart/form-data" action="process.php" method="POST" onSubmit="return showPleaseWait()">
<?php //This hidden input value 'paste' is used by process.php to distinguish the two forms ?>
<input type="hidden" name="act" value="paste"/>
<b><font color="#1F88A7">Paste in a list (one query per line):<br/></b><br/>
<textarea id="text" cols="15" rows="6" name="ID_list" style='background-color:#ffffff; border:solid 1px #1F88A7'></textarea>
<input id="butt" name="sub" type="submit" value="Submit" /><br />
</form>
<br />
<!--Box to upload a file -->
<form enctype="multipart/form-data" action="process.php" method="POST" onSubmit="return showPleaseWait()">
<?php //This hidden input value 'upload' is used by process.php to distinguish the two forms ?>
<input type="hidden" name="act" value="upload"/>
<b><font color="#1F88A7">Or upload (plain text, one query per line):</font> </b><br />
<input id="butt" name="uploadedfile" type="file" style='background-color:#ffffff; border:solid 1px #1F88A7'/><input type="submit" value="Upload File" />
</form>
In process.php I have this:
if (isset($_POST['act'])){
if ($_POST['act'] == 'upload'){
// ...process one way
echo "file uploaded";
}else if($_POST['act'] == 'paste'){
// ...process another way
echo "text uploaded";
}
}
What I would like to do now is to add a single dropdown menu that will post with whichever of these two forms are submitted. I can paste the code below into both of the two forms, but then I have this dropdown menu displayed twice (once for each form). I would like to display this dropdown menu only once at the top of the page, and its value to be posted with whichever of the two forms ('paste' or 'upload') is submitted.
<form action="process.php" method="post">
<b><font color="#1F88A7">Select your query type:</font> </b><br />
<select name="ID_type">
<option value=""></option>
<option value="type1">type1</option>
<option value="type2">type2</option>
<option value="type3">type3</option>
</option>
</select>
</form>
The goal is to process different types of query differently by including if statements into process.php, such as:
if ($_POST['ID_type'] == 'type1'){}
The problem seems simple, and maybe I can't solve it because of my lack of familiarity with html (apologies if there is anything horrible in the html above), but I have not been able to find a solution. Many many thanks for any help.
You are making it more complex than it should be, unless you really have a reason for this I would just them in the same form if I were you. Then put logic in the PHP since they are both call the same process.php script.
<form enctype="multipart/form-data"
action="process.php" method="POST" onSubmit="return showPleaseWait()">
<b><font color="#1F88A7">Select your query type:</font> </b><br />
<select name="ID_type">
<option value=""></option>
<option value="type1">type1</option>
<option value="type2">type2</option>
<option value="type3">type3</option>
</option>
</select>
<!-- Paste -->
<b><font color="#1F88A7">Paste in a list (one query per line):</b><br/>
<textarea id="text" cols="15" rows="6" name="ID_list"
style='background-color:#ffffff; border:solid 1px #1F88A7'></textarea>
<!-- upload -->
<b><font color="#1F88A7">Or upload (plain text, one query per line):</font></b>
<input name="uploadedfile" type="file"
style='background-color:#ffffff; border:solid 1px #1F88A7'/>
<input id="butt" name="sub" type="submit" value="Submit" /><br />
</form>
I wouldnt even use hiddenfield and stuff:
if (isset($_POST)){
if (is_uploaded_file($_FILES['yourfile']['uploadedfile'])){
// ...process one way
echo "file uploaded";
}else if(isset($_POST['ID_list'])){
// ...process another way
echo "text uploaded";
}
if ($_POST['ID_type'] == 'type1'){
//do whatever
}
}
Now if you don't want the user to process both , then I will focus on redesigning my Interface to just allow one type of process( maybe using a radio button or something like that)
If you don't want to get into any javascript workarounds with your existing two form structure, then you should have everything surrounded by just one form. Maybe both "act" and "query type" should be dropdowns at the top of the form. Then, depending on if act is set to paste or upload, you look at either the textarea or file input - as they will both be included if it is just one form.
There are ways to improve UX with this idea through javascript, for example to validate if the textarea contains data if act=paste or if there is a file in the file upload if act=upload before allowing the post, but you could also do this all server side if you don't want to get into JS.

PHP:confusing about keeping state with hidden filed

I would like to update the choice of poll to update every time that I've submitted.
but It seemed not working. Can anyone advice me about keeping state with hidden field?
and how to clear all the content in current page to display the poll result table?
<?php
if ($_POST['choice']==0)
$a_count = $_POST['a_count']+1;
if ($_POST['choice']==1)
$b_count = $_POST['b_count']+1;
if ($_POST['choice']==2)
$c_count = $_POST['c_count']+1;
if ($_POST['choice']==3)
$d_count = $_POST['d_count']+1;
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="POST">
<table align="center">
<tr><td>Please select</td></tr>
<tr><td><input type="radio" name="choice" value="0">aaaa</td></tr>
<input type="hidden" name="a_count" value="<?php print $a_count ?>">
<tr><td><input type="radio" name="choice" value="1">bbbb</td></tr>
<input type="hidden" name="b_count" value="<?php print $b_count ?>">
<tr><td><input type="radio" name="choice" value="2">cccc</td></tr>
<input type="hidden" name="c_count" value="<?php print $c_count ?>">
<tr><td><input type="radio" name="choice" value="3">dddd</td></tr>
<input type="hidden" name="d_count" value="<?php print $d_count ?>">
<tr><td><input type="submit" value="submit"></td></tr>
</table>
<table align="center" border="1" cellspacing="0">
<tr align="center"><th>Member Name</th><th>Vote</th></tr>
<tr><td>aaaa</td><td><?php echo"$a_count";?></td></tr>
<tr><td>bbbb</td><td><?php echo"$b_count";?></td></tr>
<tr><td>cccc</td><td><?php echo"$c_count";?></td></tr>
<tr><td>dddd</td><td><?php echo"$d_count";?></td></tr>
</table>
</form>
</body>
</html>
The code you submitted looks like it should keep the user's selection/state during their active session (as long as they do not leave that page) - but as soon as they leave it's lost. Also, their "vote" cannot be shared between any other users.
To keep the user's state, explore the use of PHP sessions or storing results in a file/database.
To share the user's vote(s) with other users, explore the use of files or a database.
To show the results table without the form, after the user has "cast their vote", you can do one of two things. You could put an if-statement around the form to check if an answer has already been selected - if so, don't display the form. The other way would be to have the results-table on a separate page that doesn't have the form (just have the form POST to the separate page, or have the page with the form redirect to the separate page).
why not use the database to store values?? And you can put the table that you are using for the input in an if condition so that it does not render when the form is submitted. Hope this helps!

Pass Value of clicked button from one page to another page Input field

Weird question this, going round in circles.
I have 2 pages.
Page 1. Has a button on it. Like
<form action="goto_page_2.php"><p class="longdesc"><span class="spanBold">Scrap Collections in North Lakes:</span><br />
<button class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
</p></form>
The above is a simple mockup. But what I want to do is, onclick of the button, parse the Value to ...
Page 2. Which has a form built in.
One of the fields is Suburb.
<!-- EMAIL SUBURB -->
<span class="commonControlLabel">Suburb:</span>
<span class="commonControlLabelItalic">(required)</span>
<span id="contactSuburbErrorMsg" class="commonControlErrorMsg"></span><br />
<input class="commonInput" type="text" id="inputSuburb" value=""/><br />
So what I want to do, is grab the VALUE of the button on PAGE 1, and add it to the Value in the input element on Page 2.
To complicate matters, Page 1. Has quite a few buttons, with different values, all unique, which we would like to pass, to the input element. Obviously onlcick of the button on page 1 we go direct to page 2.
Have the button post it's value to Page2:
Page1, I added type="submit" and name="suburb"
<button type="submit" name="suburb" class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
Page2: I added the php part in the value attribute
<input class="commonInput" type="text" id="inputSuburb" value="<?= $_POST['suburb'] ?>"/>
<!--page1::-->
<html>
<body>
<form action="test2.php" method="post">
<button name="desc" value="1">description!</button>
</form>
</body>
</html>
<!--page2::-->
<?php
echo "hello";
echo $_POST["desc"];
?>
There are several things you can do.
E.g: on form 2 say:
<input type="text" value="<?php if(isset($_POST['inputNameForm1'])){echo htmlentities($_POST['inputNameForm1']);} ?>
Or if that is not an option for you, try something like:
<? php
session_start();
$_SESSION['sessionName'] = $_POST['inputNameForm1']?>
<input type="text" value="<?php if(isset($_SESSION['sessionName']])){echo htmlentities($_SESSION['sessionName']]);} ?> />
Note: didn't test the code

Categories