I have the form below with them I can add more rows to insert more values in the mysql table.
<head>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<form action="process" class="register" method="POST">
<p>
<input type="button" value="Add Passenger" onClick="addRow('dataTable')" />
<input type="button" value="Remove Passenger" onClick="deleteRow('dataTable')" />
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label>Name</label>
<input type="text" required="required" name="BX_NAME[]">
</td>
<td>
<label for="BX_age">Age</label>
<input type="text" required="required" class="small" name="BX_age[]">
</td>
<td>
<label for="BX_gender">Gender</label>
<select id="BX_gender" name="BX_gender[]" required="required">
<option>....</option>
<option>Male</option>
<option>Female</option>
</select>
</td>
<td>
<label for="BX_birth">Berth Pre</label>
<select id="BX_birth" name="BX_birth[]" required="required">
<option>....</option>
<option>Window</option>
<option>No Choice</option>
</select>
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
<input class="submit" type="submit" value="Confirm »" />
<div class="clear"></div>
</form>
</body>
Now, I want pass the variable (for example: BX_NAME[]) with two different values to insert.php page:
If I have two rows in the form, How to create the insert?
How to build a php script to crate dynamics insert?
If I got it, you want to inset your rows in database with one query. If no tell me to delete this answer.
You'll get each variable in PHP side with it's index. for example:
chk[0], BX_Name[0], ..., chk[1], BX_Name[1], ...
here you must manage them and generate your query.
$insert = array();
for($i=0; $i < count($_POST['chk']); $i++)
{
$chk = isset($_POST['chk'][$i]) ? $_POST['chk'][$i] : '';
$BX_Name = isset($_POST['BX_Name'][$i]) ? $_POST['BX_Name'][$i] : '';
// instead of '' you must put default value which your db expect
...
$insert[] = "({$chk}, {$BX_Name}, ...)";
}
$values = implode(', ', $insert);
$query = "INSERT INTO `tbl_test` (`chk`, `bx_name`, ...) VALUES $values";
//execute it
Related
Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.
OK so I have two different types of forms and I want to use one form to submit them both. I am using a auto-responder form for form1 fields= first, last and email. My second form is a contact me form fields= name. email and a body field. I am trying to use both these forms at one shot. I have a form that will post fields to each form (INDEX.PHP) the problem is I am not sure how to set up form one to post fields into form1 and form2 the way I want. I want to use the fields that match from form index.php to both the other forms (form1, form2). so name and email will go to both forms and the body field will go to form2. The main form will post first and last in the name field of form2....also as you can see from form 1 and 2 each form has a some sort of redirect page I need to work around.?
I have main form:
Index.php
<html>
<head>
<title>Main Form</title>
</head>
<body>
<h2>Winner Aution Item Request Form</h2>
<p><span class="error"><FONT><font color="red">* required field.</font></span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
First Name: <input type="text" name="first_name" value="<?php echo $first_name;?>">
<FONT><font color="red"> *</font>
<br>
Last Name: <input type="text" name="last_name" value="<?php echo $last_name;?>"> <FONT><font color="red"> *</font>
<br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<FONT><font color="red"> *</font>
<br><br><i><b>Copy and paste auction item name below.</b></i><br>
Product Name: <input type="text" name="product_Name" rows="1" cols="10">
<?php echo $product_Name;?><FONT><font color="red"> *</font>
<input type="Submit" value="Submit" name="submit"></input>
</form>
</body>
</html>
the above form was found on a website and was pretty close to what I want it to do but not exactly....
Form1
<?php session_start(); ?>
<html>
<head>
<title>autoresponder</title>
</head>
<body>
<center>
<table cellspacing="10" bgcolor="#CCCCCC" style="border: 0px solid #000000;">
<tr><td>
<form action="http://newsletter.jeremyahenry.com//s.php" method=GET>
<strong><font color="#660000">Your First Name:</font></strong>
<input type="text" name="f" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#660000">Your Last name:</font></strong>
<input type="text" name="l" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#000066">Email address:</font></strong><br>
<input type="text" name="e" style="background-color : #FFFFFF" size=20 maxlength=50>
<input type="image" src="http://blog.jeremyahenry.com/mailermanager/images/go-button.gif" name="submit" value="Submit"><br>
<input type="hidden" name="r" value="4">
<input type="hidden" name="a" value="sub">
<input type="hidden" name="ref" value="none">
<br>
<font color="#003300">HTML: <input type="RADIO" name="h" value="1">Yes
<input type="RADIO" name="h" value="0" checked="checked">No<br>
</font>
</form>
</td></tr>
</table>
</center>
</body>
</html>
Form 2
<?php session_start(); ?>
<html>
<head>
<title>request form</title>
<style>p{font:10pt arial;}</style>
</head>
<body>
<form action="contact_me/process.php" method=post>
<table align=left border=0 height=300>
<tr>
<td nowrap>
<p> Your name:
<input maxlength=25 name=name size=25>
</td>
</tr>
<tr>
<td nowrap>
<p> Your email:
<input name=from size=25 maxlength=25>
</td>
</tr>
<tr>
<td colspan=2>
<center>
<p align=center>Enter your Auction item Name below:
<br>
<textarea cols=50 name=message rows=7></textarea>
<p align=center>
<input type=submit value="Send Message">
<input type=reset value=Reset name="reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
ass you can see from the code I was going to use sessions. But not to familiar with it even though I have experience with php I am still quite new to it and need help. I want to pars form data to both forms. and the product name to form two. any help on this would be greatly appreciated.. I hope this is clear enough for some one out there to help.....
Ok New code Three forms 1.main form submits information to form1 and form2.
I have set up using a session so I have: session.php
<?php
session_start();
// store session data
$_SESSION['af_first_name'] = $af_first_name;
$_SESSION['af_last_name'] = $af_last_name;
$_SESSION['af_email'] = $af_email;
$_SESSION['cf_address'] = $cf_item_name;
?>
That handles variables.
My new code for index.php
<?php
// including the session file
require_once("session_start.php")
?>
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
?>
</head>
<body>
<h2>Winner Aution Item Request Form</h2>
<p><span class="error"><FONT><font color="red">* required field.</font></span></p>
<form name="form1">
First Name: <input type="text" name="$af_first_name" id="af_first_name" value="<?php if(isset($_SESSION['af_first_name'])){echo stripslashes($_SESSION['af_first_name']); unset($_SESSION['af_first_name']); } ?>" /><br>
Last Name: <input type="text" name="$af_last_name" id="af_last_name" value="<?php if(isset($_SESSION['af_last_name'])){echo stripslashes($_SESSION['af_last_name']); unset($_SESSION['af_last_name']); } ?>" /><br>
E-Mail: <input type="text" name="$af_email" id="af_email" value="<?php if(isset($_SESSION['af_email'])){echo stripslashes($_SESSION['af_email']); unset($_SESSION['af_email']); } ?>" /><br>
</form>
<form name="form2">Copy and Paste Auction Name Below!<br>
Product Name <br><input type="text" name="$cf_item_name" id="cf_item_name" value="<?php if(isset($_SESSION['cf_item_name'])){echo stripslashes($_SESSION['cf_item_name']); unset($_SESSION['cf_item_name']); } ?>" /><br>
<input type="Submit" value="Submit" name="submit" onsubmit="form2.submit(); form3.submit();"></input>
That form takes input and sets to session. Session.php picks it up and places it in variable form. From here I used a strip for the underscores. and when it goes to form1 and form2 varables pick it up and fill in the form. Here is my new form1
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
if(!empty($_SESSION['_mf'])): //Strip all possible back slashes
stripZlashes($_SESSION['_mf']);
endif;
// including the session file
require_once("session_start.php")
?>
<center>
<table cellspacing="10" bgcolor="#CCCCCC" style="border: 0px solid #000000;"> <tr><td>
<form action="http://newsletter.jeremyahenry.com//s.php" method=GET>
<strong><font color="#660000">Your First Name:</font></strong>
<input type="text" id="$af_first_name" value="<?php echo (!empty($_SESSION['_af']['af_first_name'])?$_SESSION['_af']['af_first_name']:'') ?>" name="f" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#660000">Your Last name:</font></strong>
<input type="text" id="$af_last_name" value="<?php echo (!empty($_SESSION['_af']['af_last_name'])?$_SESSION['_af']['af_last_name']:'') ?>" name="l" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#000066">Email address:</font></strong><br>
<input type="text" id="$af_email" value="<?php echo (!empty($_SESSION['_af']['af_email'])?$_SESSION['_af']['af_email']:'') ?>" name="e" style="background-color : #FFFFFF" size=20 maxlength=50>
<input type="image" src="http://newsletter.jeremyahenry.com/images/go-button.gif" name="submit" value="Submit"><br>
<input type="hidden" name="r" value="4">
<input type="hidden" name="a" value="sub">
<input type="hidden" name="ref" value="none">
<br>
<font color="#003300">HTML: <input type="RADIO" name="h" value="1">Yes
<input type="RADIO" name="h" value="0" checked="checked">No<br>
</font></form>
</td></tr></table>
</center>
<?php
if(!empty($_SESSION['_mf'])):
unset($_SESSION['_mf']);
endif;
?>
form2:
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
if(!empty($_SESSION['_mf'])): //Strip all possible back slashes
stripZlashes($_SESSION['_mf']);
endif;
// including the session file
require_once("session_start.php")
?>
<form action="process.php" method=post>
<table align=left border=0 height=300>
<tr>
<td nowrap>
<p> Your name:
<input maxlength=25 name="af first name" id="af_first_name" value="<?php echo (!empty($_SESSION['_af']['af_first_name'])?$_SESSION['_af']['af_first_name']:'') ?>" size=25> />
</td</tr>
<tr>
<td nowrap>
<p> Your email:
<input name=af email id="af email" value="<?php echo (!empty($_SESSION['_af']['af_email'])?$_SESSION['_af']['af_email']:'') ?>" size=25 maxlength=25> />
</td></tr>
<tr>
<td colspan=2>
<center>
<p align=center>Enter your Auction item Name below:
<br>
<input name="cf item name" id="cf item name" value="<?php echo (!empty($_SESSION['_cf']['cf_item_name'])?$_SESSION['_cf']['cf_item_name']:'') ?>" rows="1" />
<p align=center>
<input type=submit value="Send Message">
</td></tr></table>
</form>
</center>
Ok now i have an issue with it submitting correctly...
<form name="form1">
<input type="text" value="" name="somename" onkeyup="form2.somename.value=this.value" />
</form>
<form name="form2">
<input type="text" value="" name="somename" />
</form>
check out the above code. hope this could be helpful. Or if this is not the result which you require. so you can be brief with your requirements in reply.
I don't know the usage or need of your implementation: maybe it is like a 3 step registration form?
It could be done in different ways without complicating it too much.
You could put all three forms in the same page and only showing the right one
according to th data being posted to the same page. It is the old way of doing things.
With Ajax calls, templates and javascript it could be don simpler but it depends on
your experience.
on each element you can mention as per their relevent events, such as
Ex. onkeyup="form2.elementname.value=this.value"
I have two php forms, both for registration means (enter name, username, password etc). My first registration form (reg1.php) contains the basic inputs as I mentioned above and then I have a second registration form (reg2.php) that includes checkboxes where the form would ask the user other questions before finalizing their sign up.
What I am having trouble with is that when I enter data into my inputs in the first registration form, then proceed to the next registration page and fill that out and click Sign Up, my output on my server only obtains the information I inputted in the second registration page, not the first.
How can I get both inputted data from the two registration pages to appear together? I have been told about POSTBACK to help with this but I am not all that familiar with how to use it.
First php page:
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table style="width:300px">
<form name="Rego" method="post" action="rego2.php">
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pass" /></td>
</tr>
<tr>
<td><label>Confirm Password:</label></td>
<td><input type="password" name="conpassword" id="conpass"/></td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Next Page">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
Second php page
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
When the first form is submitted either store the data in a $_SESSION variable and retrieve it when you process the second form, or write it to hidden <input> elements on your second form and retrieve it from the $_GET or $_POST array
In 2nd input form you take first inout from value and keep those one as hidden.
and 2nd input form you should
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<input type="hidden" name="fname" value="<?php echo $_POST['fname'];?> " />
<input type="hidden" name="username" value="<?php echo $_POST['username'];?> " />
<input type="hidden" name="passowrd" value="<?php echo $_POST['password'];?> " />
<input type="hidden" name="conpassowrd" value="<?php echo $_POST['conpassword'];?> " />
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
I hope it will work for you.
In the second page, you can get the post data from page 1 and set it in a hidden field like this, say the fname:
<input type="hidden" name="fname" id="fname" value="<?php echo $_POST['fname'] ?>">
When you submit from the second page, you can catch this data in the destination page, say
$_POST['fname']
I have made this code and I can't get the $txt2 to be inserted into mysql!
BUT the $tele2 is inserted, also when having multi rows from the dynamic form!.
But the $txt2 will not be inserted into DB.
Can someone see what the problem is. I think that it is some place in the foreach
This is the action post page!!
<?php if(isset($_POST)==true && empty($_POST)==false):
$day = $_POST['day'];
$month = $_POST['month'];
$text=$_POST['txt'];
$tele=$_POST['tele'];
include("db.php");
foreach($tele as $a => $b){
$a+1;
$txt2 = $_POST['text'][$a];
$tele2 = $_POST['tele'][$a];
$sql_ins="INSERT INTO SmsSend (`PHONENUM`, `TEXT`) VALUES
('".$tele2."','".$txt2."')";
$res_ins1=mysql_query($sql_ins);
}
?>
Sending my info from this dynamic form:
<fieldset class="row2">
<legend>Besked og bruger info</legend>
<p>
<input type="button" value="Tilføj bruger" onClick="addRow('dataTable')" />
<input type="button" value="Fjern bruger" onClick="deleteRow('dataTable')" />
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label for="txt">text</label>
<input type="text" required="required" maxlength="160" name="txt[]">
</td>
<td>
<label for="tele">TELE</label>
<input type="text" required="required" maxlength="8" name="tele[]">
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
You have $_POST['text'][$a] in your action
but in html markup you have
<input type="text" required="required" maxlength="160" name="txt[]">
So you need either change name of html input to text or use $_POST['txt'][$a]
Other thing is that your code is vulnerable to SQL Injection. You should use prepared statements.
I'm trying to pre-populate my form values with variables passed through the URL.I've tried many different solutions, sometimes I don't get an error, the variable just doesn't show up. Any help is appreciated, thanks!
URL Example: website.com/?firstname=john
Code:
<html>
<script type="text/javascript">
function writeform(){
selobj = document.getElementById('selform');
newobj = document.getElementById('newform');
p = document.getElementById('menu').selectedIndex + 1;
a = document.getElementById('menu2').selectedIndex + 1;
if((p < 14 && (a == 1 || a == 2 || a == 3 ||a == 4)) { // write the 1st form
txt = 'Person 1: '+'<input type="text"/><br/>';
txt += 'Person 2: '+'<input type="text"/>';
} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';
}
</script>
<div style="width: 400px; float:left;"> <?php echo $_GET["firstname"]; ?></div>
<div style="width: 400px; float: left;"> <!-- Primary Div-->
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select>
<br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<br/>
</td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
<br/>
<button onclick="writeform();">Search</button></td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</html>
Test that what you get in $_GET variable by using var_dump($_GET), then use:
echo isset($_GET["firstname"]) ? $_GET["firstname"] : "";
Firstly use print_r($_GET) at the begining of the file to check wether you have the parameters passed.
Then you might want to clean up that mess, because defining $firstname 3 times with the same value just to echo it out makes no sense.
Secondly, you would really like to change those action url as I'm pretty sure it's wrong:
<form action="http://site1.com/upload" method="get">
Thirdly, your input names are name="fname" meanwhile using firstname in $_GET. Not really sure if you will ever relate these two but, whatever.
Some advices:
learn to write code quite more readable than this.
go to jQuery.com and do some research, as it really helps you write less , do more.
CSS doesn't use equal (=) sign as value setter , which in your case is height=350px when it should be height: 350px;.
Give elements some ID's or Classes and use some .css files , it will clean your code more than you can imagine.
You had started wrongly, that's why URL doesn't appering
<script type="text/javascript">
function writeform(){
selobj=document.getElementById('selform');
newobj=document.getElementById('newform');
p=document.getElementById('menu').selectedIndex+1;
a=document.getElementById('menu2').selectedIndex+1;
if((p<14 && (a==1 || a==2 || a==3 ||a==4)){ // write the 1st form
txt ='Person 1: '+'<input type="text"/><br/>';
txt+='Person 2: '+'<input type="text"/>';} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';}
</script>
<body>
<form action="http://site1.com/upload" method="get">
<?php echo $_GET["firstname"]; ?>
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select><br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option></select><br/>
</td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
<br/>
<button onclick="writeform();">Search</button></td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</body>
</html>
A couple of problems that I'm seeing here ... for one you have multiple elements with the same id (see id="selform").
To load get variable into a text input the pattern is like this:
<input type='text' name='fieldname' value='<?= isset($_GET['field'])?$_GET['field']:"") ?>'/>
for a checkbox or radio control it is like this
<input type='checkbox' name='fieldname' value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"checked=\"checked\"":"") />
for select boxes it you do this:
<select name='fieldname'>
<option value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"selected=\"selected\":"" ?>>My Val Label</option>
<option value='myval2' <?= isset($_GET['field']) && $_GET['field'] == 'myval2'?"selected=\"selected\":"" ?>>My Val2 Label</option>
</select>
Here is a nifty select box function that will allow you too more concisely output a select in your code (i find the check with every element a little tedious)
function showSelect($name, $options, $selected, $attr = array()){
$str = "<select name='".$name.'"';
foreach($attr as $name=>$val){
$str.= " ".$name."='".$val."'";
}
$str.=">";
foreach($options as $k=>$val){
$str.= "<option value='".$val."'".($val==$selected?" selected='selected'":"").">".$k.'</option>';
}
$str.="</select>";
}
and you can use it like this...
$days = array();
for($d = 1; $x<=31; $x++){
$days[(string)$d] = (string)$d;
}
echo showSelect("formDays", $days, $_POST["formDays"], array("id"=>"formDays"))