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!
Related
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.
is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived form filled by user
generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.
i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if (isset($_POST["fname"])) {echo v($_POST["fname"]);}?>" required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo v($_POST["lname"]);}?>" required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo v($_POST["email"]);} else {echo "xo#xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:
In your PHP script you could have something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determines if the form was sent through the POST method
foreach ($_POST as $fieldName => $formValue) {
// Validate $formValue
}
}
What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.
This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.
<?php
foreach($_POST as $name => $item) {
print "name::$name item::$item<br>";
}
?>
<html><title>test</title>
<form method="post">
field one: <input type="text" name="one">
<br>
field two: <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>
Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).
Take a look at the answer here.
This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.
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.
Hi i am generating checkbox list by using a for each loop, I want to pass the value of checked checkbox into database.
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
when i checked the checkbox i want to pass the label name into the database can someone help.
Check this code. You need to take array as checkbox name i.e. list[]. Once form is submitted you can use it's value to insert in to database.
<?
if(isset($_POST['submit'])) // once form is submitted build your logic
{
$list = $_POST['list'];
foreach($list as $value)
{
echo "<br />Checked: $value";
// use $value to insert into database
$sql = "insert into...";
mysql_query($sql);
}
}
?>
<form name="frm" method="post">
<table>
<? foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[]" value="<?php echo $list->nList ?>" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
<tr>
<td><input name="submit" value="Submit" type="submit" /></td></tr></table>
</form>
Ok, you've got a few problems there..
Firstly, please avoid opening php tags and closing them constantly. Just echo back the HTML. Going in and out of php so often will cause performance issues.
So, first off, you need to set up a form on here, your form needs to point to a second page (or self-process using an isset function, but let's go with the former solution for clarity). The second page is what's going to dump things into the DB for you based on what the user ticked.
So, here's the HTML for your form;
<form action="database.php" method="POST">
<div>
<label for="checkbox1">This is the first option:</label>
<input type="checkbox" id="checkbox1" name="checkbox1" />
</div>
<div>
<label for="checkbox2">This is the second option:</label>
<input type="checkbox" id="checkbox2" name="checkbox2" />
</div>
<div>
<input type="submit" />
</div>
</form>
Again, I'd recommend echo'ing this back in your foreach loop - a short function could accomplish this for you.
So, once someone hits the "Submit" button, they'll be taken to your second page (database.php as we've called it), and their options will be stored in the POST array. For good practice, always use POST rather than GET when dealing with DB entries.
So on this page, we'd check what checkboxes were selected, and then update the db as required;
if (isset($_POST['checkbox1']))
{
// user ticked checkbox1
$sql = "UPDATE table
SET 'checkbox1' = 1
WHERE 'user' = 'username';"
mysqli_query($sql);
}
Note that's just a rough solution - you haven't told me exactly how you're doing this, so I'm assuming your DB is tied to user's votes etc. You can also update checkbox1 by incrementing the value.. etc etc.
Hope this helps.
Eoghan
In the name you should use [] (because it's an array). So in this case it'd be name="list[]". Then when you submit it (either post or get) you just get the checked data on the list[] array. I'm not sure if I understood your question. If I didn't; please share a little more information.
I think this is what you're looking for:
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[<?php echo $list->nList ?>]" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
Then in PHP, you would do foreach($_POST['list'] as $listval) to loop over it again. Substitute $_POST for $_GET if you are using GET in your form.
Edit: I just noticed where you did echo $list->nList that this variable is either not going to change in your loop or it's a typo. Assuming it's a typo, it should be echo $lst->nList instead. If it's not a typo, then you want to just remove it and have name="list[]" in your HTML
I need to get textbox value to another php page. By clicking 'Box' icon i want to submit perticular row only. I already got row ID to 'Box' icon. How can i do that with the while loop.
thanks in advance
Tharindu
You should arrange the HTML code for this in the following fashion:
<table>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
</table>
Then once you hit the image, it will submit the form. Add this code to the top of your PHP script "<?php print_r($_POST); ?> and you will see that you can now process the posted contents based on the data that was posted without the need for any while loop.
let the you have posted be list.php
in the text box like
<input type=text name="rid" value= " <?php echo $rid ?>" onclick="location.href='view.php'"/>
get the row id in next page that is view.php
$id = $_GET['rid'];
pass it as hidden in view.php
<input type="hidden" name="id" value="<?php echo $id; ?> "/>
Make sure all your db connection goes perfect and echo all data whatever you want from row.
In the original php file generate a different html form for each box.
<form action="page2.php" method="post">
<input name="Z067DA" />
</form>
In the page2.php file use code similar to this. $value contains the user
submitted information.
foreach($_POST as $key=>$value)
{
// Use submitted value.
}
If you know the input tag names in advance you can just access them directly in your php code.
$value = $_POST['Z067DA'];