passing while loop value to another form - php

In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">

I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...

Related

How can I pass variables from hybrid .php/HTML file to another .php file? -- I've tried all the methods I've found on-line [duplicate]

I have the following code:
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="$student_no" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
On the next page PROCESS_FEE007.PHP the value is not received.
Variables are not parsed by interpreter inside single quotes. You should use double quotes or explicit string concatenation.
In your example the value of $_POST['student_no'] will be string '$student_no', not the value of the $student_no variable.
Besides if you're using method="POST" in your form, you can only get the inputs value through the $_POST array.
<?php
$student_no = $_POST['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
parse student_no in form as
<?php
$student_no = $_GET['student_no'];
echo '<form name="student" action="PROCESS_FEE007.php" method="POST">';
echo '</br><table>';
echo '**<input name="student_no" type="hidden" value="'.$student_no.'" />**';
echo '<td>Amount: </td><td>'.'<input name="amount" type="text" /></td></tr>';
echo '<tr> <td>Remarks: </td><td>'.'<input name="remarks" type="text" /> </td>';
echo '<tr> <td>';
echo '<td>'.'<input type="submit" name="submit_save" value="Save"/></td></tr>';
echo '</table>';
echo '</form>';
?>
and on the PROCESS_FEE007.php page use
<?php
if ($_POST['submit_save']){
var_dump($_POST);die();
}
?>
check the attribute "VALUE" of hidden input field. The value is not put in the field.
First make the input field a text box and after fixing the bug make it a hidden field.
may be useful. (I forgot cuz I am out of PHP long time).
try using $_REQUEST instead of get example $student_no = $_REQUEST['student_no'];

Post variables from a second table in form

I'm trying post 3 variables in order to insert them in table2. As you can see I'm using a SELECT to get 3 variables from table1 to insert into a table2 but can't see the input value when looking at the source.
The loop works. I get results in the loop but the input post echo's nothing. I've tried many different ways but can't seem to get it to work. Can someone help?
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
?>
<input type="hidden" name="name" id="name" value="<?php echo "{$_POST['name']}"; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo "{$_POST['cover']}"; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo "{$_POST['pageno']}"; ?>">
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
I don't see why there are two queries. You can just drop the first one. Also, I'd like to suggest you this-
Instead of echoing like this
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
echo it like this-
while($row = mysqli_fetch_array($result))
{
echo "<input type='text' value='" . $row[name] . "' name='name' id='name'>";
echo "<input type='text' value='" . $row[cover] . "' name='cover' id='cover'>";
echo "<input type='text' value='" . $row[pageno] . "' name='pageno' id='pageno'>";
}
In your last bit of code, please make these small changes-
<input type="hidden" name="name" id="name" value="<?php echo $_POST['name']; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo $_POST['cover']; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo $_POST['pageno']; ?>">
Please clarify- why would you echo input controls in a loop? You could potentially get hundreds of input controls rendered on your page. Now you don't want that do you? Hope my answer helps.

always last database line is deleted when deleting from php page

Im fetching data from database table when i m trying to delete a particular record no matter what i click last record get deleted what m i doing wrong, this is code m using after submit/click del button
// DELETE
if(isset($_POST['del']))
{
require'conn.php';
$delete_id = $_POST['del_id'];
print_r($_POST);
die;
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo "<h1>".' sucess ! record was deleted' ."</h1>";
}
else
{
echo "<h1>".' record was not deleted '."</h1>";
}
mysqli_close($conn);
}
include'fetchtable.php';
and this is my table structure and del button code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
}
?>
</form>
As Abhik Chakraborty says, you need a form for each row, or an other logic;
one solution is to put the <form ...>...</form> within the loop:
this is - AFAIK - not correct, in does not conform to HTML std. and works only with some browsers, bcause <table> and <form> ar mixed in wrong order, I use it only as egsample to show you problem
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo "<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">"
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
echo "</form>"
I would preferre to have only one hidden field in the form und to set the value of that field with onClick event of the submit button.
only the last lines again:
...
echo '<td> <input type="submit" name="del" value="delete" onclick="form.row_id.value='$the id$';"/>'
echo '<td> <input type="submit" name="edit" value="edit" onclick="form.row_id.value='$the id$';"/>'
echo"</tr>";
}
?>
<input ID='row_id' type="hidden" name="del_id" value="no set till now" />'
</form>
You need to have multiple for each action and so you need to have the code as
while (mysqli_stmt_fetch($stmt)){
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">' ;
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo "</tr>";
echo "</form>";
}
Also your code is not safe, you need to use mysqli_real_escape_string() for your POST data. or prepared statement

keep data in form when clicking submit

EDIT: Apologies - all code now pasted below
Apologies for the first time newbie question - I've been looking for an answer here and on google - I get the feeling this is some simple coding i'm messing up.
I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish.
I have a section of the form where this doesn't work however - where you can ask multiple questions and each question can have up to 4 answers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>Test page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylenewtest.css" type="text/css" />
<script language="javascript" src="tableadd.js"></script>
</head>
<body>
<?php
$string = isset($_POST['quiz_format']) ? $_POST['quiz_format'] : false;
$quiz_format = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $string);
$stringquiz_100 = isset($_POST['quiz_100']) ? $_POST['quiz_100'] : false;
$quiz_100 = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_100);
$stringquiz_fback = isset($_POST['quiz_fback']) ? $_POST['quiz_fback'] : false;
$quiz_fback = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_fback);
$stringquiz_title = isset($_POST['quiz_title']) ? $_POST['quiz_title'] : false;
$quiz_title = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_title);
$stringquiz_instruct = isset($_POST['quiz_instruct']) ? $_POST['quiz_instruct'] : false;
$quiz_instruct = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\?,\"\'\._#\\/ !&()=\+;:\t\r\n-]", "", $stringquiz_instruct);
$stringquiz_time = isset($_POST['quiz_time']) ? $_POST['quiz_time'] : false;
$quiz_time = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_time);
?>
<div id="content">
<p><span class="error">* required field.</span></p>
<form method="post" action="">
<div class="datagrid">
<table>
<tr><th colspan="2">Please choose the format of the quiz<span class="error">*</span></th></tr>
<tr><td>Hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Hangman") echo "checked";?> value="Hangman"></td></tr>
<tr><td>Gap fill</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Gap fill") echo "checked";?> value="Gap fill"></td></tr>
<tr><td>Multiple choice</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Multiple Choice") echo "checked";?> value="Multiple choice"></td></tr>
<tr><td>Simple Q & A</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Simple Q & A") echo "checked";?> value="Simple Q & A"></td></tr>
<tr><td>Word formation hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Word formation hangman") echo "checked";?> value="Word formation hangman"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Continue repeating the exercise until 100% achieved?<span class="error">*</span></th></tr>
<tr><td>Yes</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="Yes") echo "checked";?> value="Yes"></td></tr>
<tr><td>No</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="No") echo "checked";?> value="No"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Show the answer as correct/incorrect after every question or just at the end?<span class="error">*</span></th></tr>
<tr><td>After every question</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="After every question") echo "checked";?> value="After every question"></td></tr>
<tr><td>Just at the end</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="Just at the end") echo "checked";?> value="Just at the end"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Please enter in the title of the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz title:</td><td><input type="text" name="quiz_title" value="<?php echo $_POST[quiz_title];?>"></td></tr>
</table>
<br />
<table>
<tr><th colspan="2">Please enter in the instructions for the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz instructions:</td><td><textarea name="quiz_instruct" rows="10" cols="40"><?php echo $_POST[quiz_instruct];?></textarea></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<script language="javascript">
window.onload = moreFields;
window.onload = init;
</script>
<table>
<tr><th colspan="5">Please enter in the quiz's questions and answers:<span class="error">*</span></th></tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove a question field"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a4'][$i] ?>"></td>
</tr>
</table>
</div>
<span id="writeroot"></span><input type="button" id="moreFields" value="Click here to add further question fields" /><br />
</div>
<br />
<div class="datagrid">
<table>
<tr><td><input type="submit" name="Submit1" value="Submit"/></td></tr>
</table>
</div>
</form>
<?php
if ( isset( $_POST['Submit1'] ) ) {
echo "<form method=post action=insert.php>";
echo "<h1>Please confirm the quiz details are correct</h1>";
echo "<div class=datagrid>";
echo "<table>";
if(empty($string)){
echo("<tr><td>Quiz format:</td><td><h3>Please go back and choose a quiz format<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz format:</td><td>" . $quiz_format . "</td></tr>");
echo("<input type=hidden name=quiz_format value='" . $quiz_format . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td>" . $quiz_100 . "</td></tr>");
echo("<input type=hidden name=quiz_100 value='" . $quiz_100 . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td>" . $quiz_fback . "</td></tr>");
echo("<input type=hidden name=quiz_fback value='" . $quiz_fback . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_title)){
echo("<tr><td>Quiz title:</td><td><h3>Please go back and enter in a title for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz title:</td><td>" . $quiz_title . "</td></tr>");
echo("<input type=hidden name=quiz_title value='" . $quiz_title . "' />");
}
if(empty($stringquiz_instruct)){
echo("<tr><td>Quiz instructions:</td><td><h3>Please go back and enter in instructions for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz instructions:</td><td>" . $quiz_instruct . "</td></tr>");
echo("<input type=hidden name=quiz_instruct value='" . $quiz_instruct . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_time)){
echo("<tr><td>Time limit:</td><td>not specified</td></tr>");
}
else
{
echo("<tr><td>Time limit:</td><td>" . $quiz_time . "</td></tr>");
echo("<input type=hidden name=quiz_time value='" . $quiz_time . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td colspan=5>The questions and answers to your quiz:</td></tr>";
$aq = $_POST['q'];
$aa1 = $_POST['a1'];
$aa2 = $_POST['a2'];
$aa3 = $_POST['a3'];
$aa4 = $_POST['a4'];
$N = count($aq);
for($i=1; $i < $N; $i++){
if($aq[$i]==""){
echo("<tr><td>Question:</td><td><h3>Please go back and enter a question</h3></td></tr>");
$showbutton=1;
}
elseif($aa1[$i]==""){
echo("<tr><td>Answer:</td><td><h3>Please go back and enter at least one answer</h3></td></tr>");
$showbutton=1;
}
else{
echo ("<table><tr><td>Question:</td><td colspan=3>" . $aq[$i]. "<input type=hidden name=q[] value='" . $aq[$i] . "'></td></tr>
<tr><td>Answer 1:</td><td>Answer 2:</td><td>Answer 3:</td><td>Answer 4:</td></tr>
<tr><td>" .$aa1[$i] . " <input type=hidden name=a1[] value='" .$aa1[$i] . "'></td>
<td>" .$aa2[$i] . " <input type=hidden name=a2[] value='" .$aa2[$i] . "'></td>
<td>" .$aa3[$i] . " <input type=hidden name=a3[] value='" .$aa3[$i] . "'></td>
<td>" .$aa4[$i] . " <input type=hidden name=a4[] value='" .$aa4[$i] . "'></td> </tr></table>");
}
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td><input type=button value='<< Go Back' onclick='goBack()' /></td></tr>";
if ($showbutton =="1"){
}
else{
echo "<tr><td><input type=submit value=Submit></td></tr>";
echo "</table>";
echo "</form>";
}
}
?>
</div>
</body>
</html>
but every time I submit, the questions and answers carry through but the form fields lose all the data. How can I keep the data in the fields?
Many thanks in advance
You can simply set the value of the input to either $_POST['old_value_name] or $_GET['old_value_name'] depending on which form method you are using. Therefore, your new code would look something like this (assuming that you are looking for the key $i from your question):
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
</tr>
</table>
As far as I've understood your problem you want to keep values in a form after they were sent to server. "I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish"
It's ok. I remember I wanted the same a few months ago. This is why I tried to help.
There is only a problem that after pressing "submit" button form is sent to server and page is reloaded.
In return you get a new page from webserver which you pointed in the form field "action".
I would do it so:
Target form: <br/>
<form id="target" action="index.php" method='post'>
<input name="first_field" type="text" value="<?php if ( isset($_POST['first_field']) )
echo $_POST['first_field']; ?>" /> <br/>
<input name="second_field" type="text" value="<?php if ( isset($_POST['second_field']))
echo $_POST['second_field'];?>" /> <br/>
<p><b>What kind of girls do you like?:</b><Br>
<input type="radio" name="browser" value="brunette" <?php if( $_POST['browser']==="brunette" ) echo "checked"; ?> > brunette<Br>
<input type="radio" name="browser" value="blonde" <?php if( $_POST['browser']==="blonde" ) echo "checked"; ?> > blonde<Br>
<input type="radio" name="browser" value="red" <?php if( $_POST['browser']==="red" ) echo "checked"; ?> > red<Br>
</p>
<input type="submit" />
</form>
Any ideas how it can be changed/improved are encouraged.

Get all form elements and irregular characters

I have a form that can have any number of elements. Each element has a few characteristics. How can i get all the form's data into a url for ajax posting?
the variable id is the id of the given item.
Item 1
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
Item 2
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
Item 3
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
if i do
var inputs = $('#formName :input');
var data = "";
inputs.each(function() {
data += this.name+"="+$(this).val()+"&";
});
the brackets in the identifier mess everything all up. I know im am going about this incorrectly. What should i be doing?
if i do serialize the same thing occurs. I know its not ideal to use brackets in a element id, but how else can i associate a few characteristics of each item to the id?
Here is my rendered form:
<form name='formName' id='formName' action='#'>
<ul id="sortable">
<li class="ui-state-default" id='201817'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='201817['name']' id='a_201817['name']' value='hat'>
<input type='text' name='201817['size']' id='a_201817['size']' value='small'>
</li>
<li class="ui-state-default" id='501817'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='501817['name']' id='a_501817['name']' value='shirt'>
<input type='text' name='501817['size']' id='a_501817['size']' value='small'>
</li>
<li class="ui-state-default" id='522227'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='522227['name']' id='a_522227['name']' value='shirtB'>
<input type='text' name='522227['size']' id='a_522227['size']' value='Large'>
</li>
</ul>
</form>
You can use form.serialize it will consider all :input fields. Also remember that serialize will consider the element name and not the id.
So Try:
var data = $('#formName').serialize();

Categories