I have a table named purchase_details with some values which has the same invoice_no. Now I need each of the data with the same invoice_no and output them in a input field which will the same amount of the invoice_no length.
As i am new to PHP I have tried to use an array but it's showing the letters of the last row.
<table align="center" style="width:800px;">
<thead>
<tr>
<th>#</th>
<th style="text-align:center;">Item Name</th>
<th style="text-align:center;">Total Quantity</th>
<th style="text-align:center;">Quantity</th>
<th style="text-align:center;">Buy Price</th>
<th>Total</th>
</tr>
</thead>
<tbody id="invoice_item">
<?php
$query1="select * from purchase_details where invoice_no=$id";
$query1_run=mysqli_query($connect , $query1);
while($row=mysqli_fetch_array($query1_run)){
$product_name = $row['product_name'];
}
echo $product_name[0];
?>
<tr>
<td><b id="number"></b></td>
<td>
<select name="pid[]" class="form-control form-control-sm" required>
<option selected=""></option>
</select>
</td>
<td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>
<td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
<td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
<td></td>
</tr>
</tbody>
</table>
You should end the while loop after the table data
<table align="center" style="width:800px;">
<thead>
<tr>
<th>#</th>
<th style="text-align:center;">Item Name</th>
<th style="text-align:center;">Total Quantity</th>
<th style="text-align:center;">Quantity</th>
<th style="text-align:center;">Buy Price</th>
<th>Total</th>
</tr>
</thead>
<tbody id="invoice_item">
<?php
$query1="select * from purchase_details where invoice_no=$id";
$query1_run=mysqli_query($connect , $query1);
while($row=mysqli_fetch_array($query1_run)){
$product_name = $row['product_name'];
echo $product_name[0];
?>
<tr>
<td><b id="number"></b></td>
<td>
<select name="pid[]" class="form-control form-control-sm" required>
<option selected=""></option>
</select>
</td>
<td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>
<td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
<td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
I am trying to insert multiple rows of data from my form into my sql database. I have been looking up arrays and been trying to apply what I have found but to no avail. I am beginner to programming never took any formal classes, been learning snipits of code as I need, so my fundamentals are nonexistent.
PlayerID is not auto-increment but an actually number provided by the user.
Here is my form code
<table border="1">
<tr>
<td align="center">Form Input Employees Data</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="/wp/wp-content/plugins/FormSubmit/FSdotaArray.php">
<tr>
<td></td>
<td><b>Captain</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<td></td>
<td><b>Player 2</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="submit" value="Sent"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
Here is my php code, The header is needed because I am using wordpress and running the code as a plugin.
<?php
/*
Plugin Name: FSdota
Description: submit form data into sql database
Author: F. May
Version: 1.0
*/
//inserting data order
$con=mysqli_connect("XXXXXX","XXXXXXXX","XXXXXXXX","XXXXXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysqli_query($con,"XXXXXXXXXX");
$row_data = "($playerid,$firstname, $lastname, $position,$team,$Game)";
if (is_array($row_data));
{
foreach($_POST['playerid'] as $row=>$playerid) {
$playerid = mysqli_real_escape_string('playerid');
$firstname = mysqli_real_escape_string($con,($_POST['firstname'][$row]));
$lastname = mysqli_real_escape_string($con,($_POST['lastname'][$row]));
$position = mysqli_real_escape_string($con,($_POST['position'][$row]));
$team = mysqli_real_escape_string($con,($_POST['team'][$row]));
$game = mysqli_real_escape_string($con,($_POST['game'][$row]));
}
}
if (!empty($row_data)) {
$sql="INSERT INTO wp_es_fstest (PlayerID,FirstName,LastName,Position,Team,Game) VALUES".implode(',', $row_data);
$query = mysqli_query($con, $sql);
if (mysql_query($query))
echo 'Successful inserts: ' . mysql_affected_rows($con);
else
echo 'query failed';
}
?>
You are not sending multiple values for the same fields. Your name attributes are like name="playerid" so that means that every new entry with the same name will overwrite the previous one and only one gets sent to the server.
You should use arrays to group your fields:
<input type="text" name="playerid[SOME_ID]" size="20">
...
<input type="text" name="firstname[SOME_ID]" size="20">
...
// etc.
...
<input type="text" name="playerid[SOME_OTHER_ID]" size="20">
...
<input type="text" name="firstname[SOME_OTHER_ID]" size="20">
...
// etc.
For the SOME_ID, SOME_OTHER_ID, etc. values you could use a value from a database if it already exist or a simple counter, as long as the fields in a group use the same number / ID.
About the php-side, as mentioned by #Fred-ii- you should only use one sql API, in this case mysqli.
Also note that:
$playerid = mysqli_real_escape_string('playerid');
should be:
$playerid = mysqli_real_escape_string($con, 'playerid');
Personally I would also use a prepared statement with bound variables so that you can prepare your statement once and then run it several times with the different groups of values without having to worry about sql injection / escaping the values.
One additional remark:
This is going to fail:
$row_data = "($playerid,$firstname, $lastname, $position,$team,$Game)";
if (is_array($row_data));
{
You have just set your $row_data variable to be a string, probably with a bunch of undefined variables so checking one line after that whether it is an array is never going to work.
Alright so it has been a while since the last point, life got busy and I wanted to do a little more research.
I have modified my form.html to specify that they are arrays and created a new php file to go with it. here is the code below.
<body>
<table border="1">
<tr>
<td align="center">Dota 2 Open Team Registration</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="/wp/wp-content/plugins/FormSubmit/FSdotaArray.php">
<tr>
<td></td>
<td><b>Captain</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[playerid_1]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_1]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_1]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_1]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_1]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_1]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!-- Player 2 -->
<td></td>
<td><b>Player 2</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_2]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_2]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_2]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_2]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_2]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_2]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!--Player 3 -->
<td></td>
<td><b>Player 3</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_3]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_3]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_3]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_3]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_3]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_3]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!-- Player 4 -->
<td></td>
<td><b>Player 4</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_4]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_4]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_4]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_4]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_4]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_4]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!-- Player 5 -->
<td></td>
<td><b>Player 5</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_5]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_5]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_5]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_5]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_5]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_5]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!-- Player Sub 1 [6] -->
<td></td>
<td><b>Sub 1 (Optional)</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_6]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_6]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_6]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_6]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_6]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_6]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<!-- Player Sub 2 [7] -->
<td></td>
<td><b>Sub 2</b></td>
</tr>
<tr>
<td>Steam ID</td>
<td><input type="text" name="playerid[player_7]" size="20">
</td>
</tr>
<tr>
<td>Fisrt Name</td>
<td><input type="text" name="firstname[firstname_7]" size="20">
</td>
</tr>
<tr>
<td>last Name</td>
<td><input type="text" name="lastname[lastname_7]" size="20">
</td>
</tr>
<tr>
<td>Position</td>
<td><select name="position[position_7]">
<option value="1:hardcarry">1: Hard Carry</option>
<option value="2:mid">2: Mid</option>
<option value="3:offlane">3: Offlane</option>
<option value="4:support">4: Support</option>
<option value="5:support">5: Support</option>
</select>
</td>
</tr>
<tr>
<td>Team</td>
<td><input type="text" name="team[team_7]" size="40">
</td>
</tr>
<tr>
<td>Game</td>
<td><select name="game[game_7]">
<option value="dota2">Dota 2</option>
<option value="leagueoflegends">League of Legends</option>
<option value="starcraft2">StarCraft 2: Heart of the Swarm</option>
<option value="USFIV">USFIV</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="submit" value="Sent"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
and then the PHP code
<?php
/*
Plugin Name: FSdota
Description: submit form data into sql database
Author: F. May
Version: 1.0
*/
//inserting data order
$con=mysqli_connect("xxxxxxxx","xxxxxxx","xxxxxx!","xxxxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
//define values
$player_a = array($playerid_1,$playerid_2,$playerid_3,$playerid_4,$playerid_5,$playerid_6,$playerid_7);
$firstname_a = array($firstname_1,$firstname_2,$firstname_3,$firstname_4,$firstname_5,$firstname_6,$firstname_7);
$lastname_a = array($lastnanme_1,$lastnanme_2,$lastnanme_3,$lastnanme_4,$lastnanme_5,$lastnanme_6,$lastnanme_7);
$position_a = array($position_1,$position_2,$position_3,$position_4,$position_5,$position_6,$position_7);
$team_a = array($team_1,$team_2,$team_3,$team_4,$team_5,$team_6,$team_7);
$game_a = array($game_1,$game_2,$game_3,$game_4,$game_5,$game_6,$game_7);
$playerid = mysqli_real_escape_string($con, '$playerid_a');
$firstname = mysqli_real_escape_string($con, '$firstname_a');
$lastname = mysqli_real_escape_string($con, '$lastname_a');
$position = mysqli_real_escape_string($con,'$position_a');
$team = mysqli_real_escape_string($con, '$team_a');
$game = mysqli_real_escape_string($con, '$game_a');
for ($i = 1; $i < 7; $i++) {
//define arrays into variables with counter
// $a = $playerid_a[$];
// $b = $firstname_a[$i];
// $c = $lastname_a[$i];
// $d = $position_a[$i];
// $e = $team_a[$i];
// $f = $game_a[$i];
$str[ ] = "('{$player_[$i]}','{$firstname_[$i]}','{$lastname_[$i]}','{$position_[$i]}','{$team_[$i]}','{$game_[$i]}')";
}
$s= implode(',', $str);
$sql="INSERT INTO wp_es_fstest (playerid, firstname, lastname, position, team, game) VALUES $s";
echo $sql;
mysqli_close($con);
?>
I dont think I have defined everything correctly, in the echo i am null values.
INSERT INTO wp_es_fstest (playerid, firstname, lastname, position, team, game) VALUES ('','','','','',''),('','','','','',''),('','','','','',''),('','','','','',''),('','','','','',''), ('','','','','','')
My below code is posting at both times when the checkbox is checked or not checked. When its not checked I am getting alert and then posting. But when checked its posting automatically.
I need JavaScript to post the form when Submit button is clicked and checkbox should be kept checked. Else it should just provide me an alert. But unfortunately the page is reloading even when keeping the checkbox unchecked and using submit button.
My code is
<script type="text/javascript">
function check()
{
if(document.getElementById("privacy").checked == true)
{
document.print.submit();
}
else{
alert('Please read and accept our privacy statments to proceed.');
};
}
</script>
</head>
<body>
<form id="print" action="printfile.php">
<div id="express">
<table width="800">
<tr>
<td colspan="2"><span style="color:#FF0000; font-size:26px;">Express Application</span></td>
</tr>
<tr>
<td width="300"> </td>
<td width="500"> </td>
</tr>
<tr>
<td>First Name (Required) </td>
<td><input name="firstname" type="text" size="30"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Last Name </td>
<td><input name="lastname" type="text" size="30"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Email (Required) </td>
<td><input name="email" type="text" id="email" size="30"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Purpose of finance (Required) </td>
<td><select name="purpose" id="purpose">
<option selected="selected">Home Loan</option>
<option>Refinance</option>
<option>Investment Loan</option>
<option>Debit Consolidation</option>
<option>Others</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Your deposit amount </td>
<td><input name="depositamount" type="text" size="30"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Amount you need to borrow? </td>
<td><input name="borrowamount" type="text" size="30"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Brief description of purpose of <br />
finance (Required) <br />
<br />
<br />
<br />
<br />
<br /></td>
<td><textarea name="purposedesc" cols="60" rows="10" id="purposedesc"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Application Type<br />
<br /></td>
<td><label>
<input type="radio" name="apptype" value="Single" onclick="document.getElementById('singleapp').style.visibility = 'visible'; document.getElementById('jointapp').style.visibility = 'hidden';" />
Single Application</label>
<br />
<label>
<input type="radio" name="apptype" value="Joint" onclick="document.getElementById('singleapp').style.visibility = 'visible'; document.getElementById('jointapp').style.visibility = 'visible';" />
Joint Application</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2"><input name="privacy" type="checkbox" id="privacy" value="checkbox" />
Privacy Act (Required) <br />
<br />
I have read the Privacy Act 1988 (as Amended) and understand that by selecting the submit button I/we authorise Best Aussie Broker to act on my/our behalf and manage personal information in relation to this application. </td>
</tr>
</table>
<br />
Last step then we will get all listed Australian vendors to fight it out for your best deal.<br />
<br />
<table width="800" border="0" style="">
<tr>
<td width="400">
<div id="singleapp">
<table width="400" border="0">
<tr>
<td colspan="2" bgcolor="#333300"><span class="style1">Your application </span></td>
</tr>
<tr>
<td width="150"> </td>
<td width="240"> </td>
</tr>
<tr>
<td>Date of Birth</td>
<td><select name="day1" id="day1">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
<select name="month1" id="month1">
<option selected="selected">Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apl</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<input name="year1" type="text" id="year1" style="padding:1.2px;" size="8" maxlength="4" /> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Address</td>
<td><input name="address1" type="text" id="address1" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Suburb</td>
<td><input name="suburb1" type="text" id="suburb1" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>PostCode</td>
<td><input name="postcode1" type="text" id="postcode1" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>State</td>
<td><select name="state1" id="state1">
<option value="Victoria" selected="selected">VIC</option>
<option value="New South Wales">NSW</option>
<option value="QueensLand">QLD</option>
<option value="South Australia">SA</option>
<option value="Western Australia">WA</option>
<option value="Tasmania">TAS</option>
<option value="New Zeland">NZ</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Best contact </td>
<td><select name="phone1" id="phone1">
<option>Phone</option>
<option>Email</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Phone number </td>
<td><input name="number1" type="text" id="number1" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Marital status </td>
<td><select name="maried1" id="maried1">
<option>Single</option>
<option>Married</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Residential status </td>
<td><select name="rstatus1" id="rstatus1">
<option selected="selected">Renting</option>
<option>Home Owner</option>
<option>Home Mortgaged</option>
<option>Board</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Children/dependents</td>
<td><select name="childs1" id="childs1">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6 or more">6 or more</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Net income </td>
<td><input name="netincome1" type="text" id="netincome1" size="10"/>
per
<select name="months1" id="months1">
<option>Month</option>
<option>Year</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Current employer </td>
<td><input name="cemployer1" type="text" id="cemployer1" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Time at this<br />
employer </td>
<td> Years
<select name="years1" id="years1">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="More">More</option>
</select>
Months
<select name="emonth1" id="emonth1">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" value="Submit my Application" onclick="check();" /></td>
</tr>
</table>
</div>
</td>
<td width="400">
<div id="jointapp">
<table width="400" border="0">
<tr>
<td colspan="2" bgcolor="#333300"><span class="style1">Your partner's application </span></td>
</tr>
<tr>
<td width="150"> </td>
<td width="240"> </td>
</tr>
<tr>
<td>Date of Birth</td>
<td><select name="day11" id="day11">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
<select name="month11" id="month11">
<option selected="selected">Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apl</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<input name="year2" type="text" size="8" maxlength="4" style="padding:1.2px;" /> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Address</td>
<td><input name="address12" type="text" id="address12" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Suburb</td>
<td><input name="suburb12" type="text" id="suburb12" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>PostCode</td>
<td><input name="postcode12" type="text" id="postcode12" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>State</td>
<td><select name="state12" id="state12">
<option value="Victoria" selected="selected">VIC</option>
<option value="New South Wales">NSW</option>
<option value="QueensLand">QLD</option>
<option value="South Australia">SA</option>
<option value="Western Australia">WA</option>
<option value="Tasmania">TAS</option>
<option value="New Zeland">NZ</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Best contact </td>
<td><select name="phone11" id="phone11">
<option>Phone</option>
<option>Email</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Phone number </td>
<td><input name="number11" type="text" id="number11" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Marital status </td>
<td><select name="married12" id="married12">
<option>Single</option>
<option>Married</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Residential status </td>
<td><select name="rstatus12" id="rstatus12">
<option selected="selected">Renting</option>
<option>Home Owner</option>
<option>Home Mortgaged</option>
<option>Board</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Children/dependents</td>
<td><select name="childs12" id="childs12">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6 or more">6 or more</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Net income </td>
<td><input name="netincome12" type="text" id="netincome12" size="10"/>
per
<select name="months12" id="months12">
<option>Month</option>
<option>Year</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Current employer </td>
<td><input name="cemployer12" type="text" id="cemployer12" size="20"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Time at this<br />
employer </td>
<td> Years
<select name="years12" id="years12">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="More">More</option>
</select>
Months
<select name="emonths12" id="emonths12">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select></td>
</tr>
</table>
<br />
<br />
<br />
<br />
<br />
</div>
</td>
</tr>
</table>
<p><br />
<br />
<center>
</center>
<br />
</p>
</div>
</form>
With the inline event handler syntax you're using it is easiest to return false so the default action (the submitting) isn't executed:
<input name="submit"
type="submit"
value="Submit my Application"
onclick="check(); return false;" />
You need to make the submit return false, otherwise it will just do the regular thing a form does when you click submit.
if(document.getElementById("privacy").checked == true)
{
document.print.submit();
}
else
{
alert('Please read and accept our privacy statments to proceed.');
return false;
}
Technically as it will be submitting anyway. You could just get it to check and only intercept if it is not checked.
if(document.getElementById("privacy").checked == false)
{
alert('Please read and accept our privacy statments to proceed.');
return false;
}
Also add return to the submit onclick (link #AndreKR said)
<input name="submit" type="submit" value="Submit my Application" onclick="return check();" />
I have 2 tables one is category and one is products. Now I want to PC calculator.
I have all the fields generated dynamically based on the database. So how can I write script to make like this.
<table ALIGN=CENTER WIDTH=100% BORDER=1 CELLSPACING=1 CELLPADDING=3>
<tr>
<th>Category Name</th>
<th>Product Name</th>
<th>Price</th>
</tr>
<tr class="odd">
<td>Speaker</td>
<td>
<select name="1" size="1" id="1">
<option value="1200.00">intex</option>
<option value="2100.00">creative</option>
<option value="1400.00">intex 2.1</option>
<option value="2000.00">intex 2.1 with woofer</option>
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Laptop</td>
<td>
<select name="3" size="1" id="3">
<option value="31000.00">sony</option>
<option value="31500.00">hp</option>
<option value="31000.00">Acer</option>
<option value="36000.00">ibm</option>
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Mouse</td>
<td>
<select name="4" size="1" id="4">
<option value="120.00"> teccom</option>
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>CPU</td>
<td>
<select name="6" size="1" id="6">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Projector</td>
<td>
<select name="7" size="1" id="7">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Anti Virus</td>
<td>
<select name="23" size="1" id="23">
<option value="450.00">McAfee</option>
<option value="1200.00">AVG</option>
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Processor</td>
<td>
<select name="59" size="1" id="59">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>testing</td>
<td>
<select name="60" size="1" id="60">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>sadfasdfasdfadsfad</td>
<td>
<select name="61" size="1" id="61">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>dfgd</td>
<td>
<select name="62" size="1" id="62">
</select>
</td>
<td><div id="t1" class="price">0</div></td>
</tr>
<tr>
<td></td>
<td>Total</td>
<td><div id="total"></div></td>
</tr>
</table>
here is jQuery
jQuery(document).ready(function() {
$("#sellp").change(function () {
var str = 0.00;
$("#sellp :selected").each(function () {
var test = $(this).val();
//alert(test);
var tr = parseInt(str) + parseInt(test);
//alert(tr);
$("#t1").text(test);
});
});
var sum = 0;
$("form :element").change(function() {
$('.price').each(function() {
sum += parseFloat($(this).text());
});
$('#total').html(sum) });
});
Firstly show some code.
Assuming all items are in the page when it loads, you need to add some event handlers
Here is plain JS - jQuery may be more elegant. You will likely need to start with this and then add the validation. You will make your life much easier if you name your fields in a way that is easy to get to
<html>
<head>
<script>
function calc(theForm) {
var total = 0;
for (var i=1;;i++) {
var item = theForm.elements["item_"+i];
if (item) {
var price = item.options[item.selectedIndex].value;
if (price && !isNaN(price)) {
price = parseFloat(price);
total+=price;
var priceId = item.id.replace('item','price');
document.getElementById(priceId).innerHTML=price.toFixed(2);
}
}
else break;
}
if (total) document.getElementById('total').innerHTML="$"+total.toFixed(2);
}
window.onload=function() {
var theForm = document.forms[0]; // first form on page
for (var i=0,n=theForm.elements.length;i<n;i++) {
if (theForm.elements[i].type.indexOf("select") !=-1) {
theForm.elements[i].onchange=function() { calc(this.form); }
}
}
theForm.onsubmit=function() { calc(this); return validate(this) }
calc(theForm)
}
</script>
</head>
<body>
<form>
<table ALIGN=CENTER WIDTH=100% BORDER=1 CELLSPACING=1 CELLPADDING=3 >
<tr>
<th>Category Name</th>
<th>Product Name</th>
<th>Price</th>
</tr>
<tr class="odd">
<td>Speaker </td>
<td><select name="item_1" size="1" id="item_1">
<option value="1200.00"> intex</option>
<option value="2100.00"> creative</option>
<option value="1400.00"> intex 2.1</option>
<option value="2000.00"> intex 2.1 with woofer</option>
</select>
</td>
<td><div id="price_1" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Laptop </td>
<td><select name="item_2" size="1" id="item_2">
<option value="31000.00"> sony</option>
<option value="31500.00"> hp</option>
<option value="31000.00"> Acer</option>
<option value="36000.00"> ibm</option>
</select>
</td>
<td><div id="price_2" class="price">0</div></td>
</tr>
<tr class="odd">
<td>Mouse </td>
<td><select name="item_3" size="1" id="item_3">
<option value="120.00"> teccom</option>
</select>
</td>
<td><div id="price_3" class="price">0</div></td>
</tr>
</table>
</form>
Total: <span id="total"></span>