Is there a way to push data to two fields in a table if a check box is clicked?
So in English, as a pure hypothetical I have six fields and a check box in two rows. First row, field one is name, second is address, third is email.
What I want to do is if the information is to be transferred from left to right upon posting, a person needs to check a box.
The table would have six fields.
name, address, email, name2, address2, email2.
So what would happen is that data that would post in name would also post in name2, as long as the check box is checked.
Is this possible in PHP?
Ok, well since people want a script to look at for an example, here… I figure it be easier to write up a generic script than to post what I’m working on. Sorry, thought I’d make it easier for people here.
<?php
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server.');
$output_form = 'yes';
echo '<div id="postwrap">'
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Document Test</title>
<link rel="stylesheet" type="text/css" href="CSS/postie.css" />
</head>
<body>
<div id="title">
<h3 id="NCMR2">Document Test</h3>
</div>
<?php
//Post Data
if (isset($_POST['submit'])) {
$ab = mysqli_real_escape_string($dbc, trim($_POST['ab']));
$date = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime ($_POST['date']))));
$part = mysqli_real_escape_string($dbc, trim($_POST['part']));
$rev = mysqli_real_escape_string($dbc, trim($_POST['rev']));
$partdesc = mysqli_real_escape_string($dbc, trim($_POST['partdesc']));
$ncmrqty = mysqli_real_escape_string($dbc, trim($_POST['ncmrqty']));
$ab2 = mysqli_real_escape_string($dbc, trim($_POST['ab']));
$date2 = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime ($_POST['date']))));
$part2 = mysqli_real_escape_string($dbc, trim($_POST['part']));
$rev2 = mysqli_real_escape_string($dbc, trim($_POST['rev']));
$partdesc2 = mysqli_real_escape_string($dbc, trim($_POST['partdesc']));
$ncmrqty2 = mysqli_real_escape_string($dbc, trim($_POST['ncmrqty']));
$output_form = 'no';
if (empty($ab) || empty($date) || empty($part) || empty($partdesc)){
// We know at least one of the input fields is blank
echo '<div id="alert">';
echo 'Please fill out all of the required NCMR information.<br />';
echo '</div>';
}
$output_form = 'yes';
}
//Access the Database
if (!empty($ab) && !empty($date) && !empty($pod)) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server.');
$query = "INSERT INTO ncmr (ab, date, part, rev, partdesc, ncmrqty, ab2, date2, part2, rev2, partdesc2, ncmrqty2)
VALUES ('$ab', '$date', '$part', '$rev', '$partdesc', '$ncmrqty', '$ab2', '$date2', '$part2', '$rev2', '$partdesc2', '$ncmrqty2')";
mysqli_query($dbc,$query)
or die ('Data not inserted.');
// Clear the form data
$ab = "";
$date = "";
$part = "";
$rev = "";
$partdesc = "";
$ncmrqty = "";
$ab2 = "";
$date2 = "";
$part2 = "";
$rev2 = "";
$partdesc2 = "";
$ncmrqty2 = "";
// Confirm success with the user
echo '<tr><td class="thank">';
echo '<p>Thank you for adding the NCRM, the correct person will be informed.</p>';
echo '<p><a href="post.php"><< Back to the form</a></p>';
$output_form = 'no';
echo '</td></tr>';
mysqli_close($dbc);
}
if ($output_form == 'yes') {
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo '<fieldset>';
//Part, Rev, Part Description, NCMR Qty
echo '<div id="box1">';
echo '<div id="ab"><span class="b">Added By: </span><input type="text" name="ab" value="" /></div>';
echo '<div id="date"><span class="b">Date Filed: </span><input type="text" name="date" value="" /></div>';
echo '<div id="part"><span class="b">Part Number: </span><input type="text" name="part" value="" /></div>';
echo '<div id="rev"><span class="b">Part Revision: </span><input type="text" name="rev" value="" /></div>';
echo '<div id="partdesc"><span class="b">Part Description: </span><textarea name="partdesc" rows="4" cols="22" ></textarea></div>';
echo '<div id="ncmrqty"><span class="b">NCMR Qty: </span><input type="text" name="ncmrqty" value="" /></div>';
echo '</div>';
echo '<div id="box2">';
echo '<div id="ab2"><span class="b">Added By: </span><input type="text" name="ab2" value="" /></div>';
echo '<div id="date2"><span class="b">Date Filed: </span><input type="text" name="date2" value="" /></div>';
echo '<div id="part2"><span class="b">Part Number: </span><input type="text" name="part2" value="" /></div>';
echo '<div id="rev2"><span class="b">Part Revision: </span><input type="text" name="rev2" value="" /></div>';
echo '<div id="partdesc2"><span class="b">Part Description: </span><textarea name="partdesc2" rows="4" cols="22" ></textarea></div>';
echo '<div id="ncmrqty2"><span class="b">NCMR Qty: </span><input type="text" name="ncmrqty2" value="" /></div>';
echo '</div>';
echo '<div id="button3"><input type="submit" value="Submit NCMR" name="submit" /></div>';
echo '</div>';
echo '</fieldset>';
echo '</form>';
}
echo '</div>';
?>
</body>
</html>
Set a name to the checkbox, say “duplicateBox“, and have its value set to
1when checked.