I have a form which is dynamic and user can add more rows to insert different hours, I wonder which is the best way to add those data to MySQL database.
<input style="margin-left:28px;" type="image" class="add_field_button" src="img/add.png" />
<table id="dataTable" width="900" style="margin-left:25px; margin-top:10px;">
<tr style="text-align:center;">
<td width="70">From</td>
<td>To</td>
<td>Duration Hours</td>
<td>Code</td>
<td>Remark</td>
</tr>
<tr style="height:85px; text-align:center;">
<td><input type="text" name="hours1[]" id="hours1" /></td>
<td><input type="text" name="hours2[]" id="hours2" /></td>
<td><input type="text" name="durationh[]" id="durationh" /></td>
<td><input type="text" name="hrscode[]" id="hrscode" /></td>
<td><textarea name="remark[]" id="remark"></textarea></td>
</tr>
</table>
Inputs are expandable so I don't know how many rows they will send to save in mysql.
you can use a foreach(), using the key to bind the input groupings together
foreach($_POST['hours1'] as $key => $value) { // could use any of the fields in $_POST['hours1']
//$_POST['hours1'][$key];
//$_POST['hours2'][$key];
//$_POST['durationh'][$key];
//$_POST['hrscode'][$key];
//$_POST['remark'][$key];
//INSERT INTO table (columns) VALUES (your $_POST values)
}
Related
I created a dynamic table of course list with 4 elements of input value each row.
<table id="tblCourse">
<tr>
<td>TXID</td>
<td>CourseID</td>
<td>Description</td>
<td>Amount</td>
</tr>
<tr>
<td><input type="text" name="TxID[0]"value="" /></td>
<td><input type="text" name="CourseID[0]"value="C101" /></td>
<td><input type="text" name="CourseDesc[0]"value="C#.NET" /></td>
<td><input type="text" name="Amount[0]"value="100.00" /></td>
</tr>
<tr>
<td><input type="text" name="TxID[1]"value="" /></td>
<td><input type="text" name="CourseID[1]" value="C102" /></td>
<td><input type="text" name="CourseDesc[1]" value="Php" /></td>
<td><input type="text" name="Amount[1]"value="200.00" /></td>
</tr>
</table>
How do I get the input values with jQuery and pass to php array(txid,courseid,coursedesc, amt)? TIA
The way you named your input fields they are already PHP Arrays once sent to a php script, but it would be four arrays
$_POST['TxID'][] (1 and 2)
$_POST['CourseID'][] (1 and 2)
...
You have to name your fields a little different.
<tr>
<td><input type="text" name="coursedata[1][TxID]" /></td>
<td><input type="text" name="coursedata[1][CourseID]" />C101</td>
<td><input type="text" name="coursedata[1][CourseDesc]" />C#.NET</td>
<td><input type="text" name="coursedata[1][Amount]" />100.00</td>
</tr>
Of course you still need to count up for each new entry (1, 2, 3 ..).
That way you would get one array "$_POST['coursedata']" and $_POST['coursedata'][1] with the value of the input field.
I think that should work. Also, you should start counting with 0.
I am new in the field of PHP.
I am working on a form to get information from a patient regarding a specific disease.
In this form i have multiple check-boxes and text fields with each check-box. If one check bos is checked then values of its text fields and checkbox values has to insert in database.
Please tell me the code to insert checked values along with textfields into database.
<form>
<table>
<tr>
<td colspan="4">Past Medical History:</td>
</tr>
<tr valign="top">
<td colspan="4" height="290"><table border="0" width="100%">
<tbody>
<tr>
<td width="26%"><div align="center">Problem</div></td>
<td width="18%"><div align="center">From (Year)</div></td>
<td width="56%"><div align="center">Details</div></td>
</tr>
<tr>
<td><input name="chkBP" id="chkBP" value="BP" type="checkbox" />
Blood Pressure</td>
<td><div align="center">
<input name="txtBPfrom" id="txtBPfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtBPDetail" id="txtBPDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkDiabetes" id="chkDiabetes" value="Diabetes" type="checkbox" />
Diabetes</td>
<td><div align="center">
<input name="txtDiabetesfrom" id="txtDiabetesfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtDiabetesDetail" id="txtDiabetesDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkHighCholes" id="chkHighCholes" value="HighCholesterol" type="checkbox" />
High Cholesterol</td>
<td><div align="center">
<input name="txtHighCholesfrom" id="txtHighCholesfrom" size="15" value="" type="text"/>
</div></td>
<td><input name="txtHighCholesDetail" id="txtHighCholesDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkArthritis" id="chkArthritis" value="Arthritis" type="checkbox" />
Arthritis</td>
<td><div align="center">
<input name="txtArthritisfrom" id="txtArthritisfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtArthritisDetail" id="txtArthritisDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkAsthma" id="chkAsthma" value="Asthma" type="checkbox" />
Asthma</td>
<td><div align="center">
<input name="txtAsthmafrom" id="txtAsthmafrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtAsthmaDetail" id="txtAsthmaDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkCirculation" id="chkCirculation" value="Circulation" type="checkbox" />
Circulation</td>
<td><div align="center">
<input name="txtCirculationfrom" id="txtCirculationfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtCirculationDetail" id="txtCirculationDetail" size="40" value="" type="text" /></td>
</tr>
</table></td>
</tr>
</form>
You will need to establish a connection to the database.
When the form is posted collect this data and insert into the database accordingly using $_POST.
Helpful example can be found here to connect
And to insert data
$link = mysqli_connect("localhost","root","","web_table");
mysqli_query($link,"INSERT INTO web_formitem (`ID`, `formID`, `caption`, `key`, `sortorder`, `type`, `enabled`, `mandatory`, `data`)
VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)")
or die(mysqli_error($link));
first add any method attribute to your form tag like, get or post
<form>
to
<form action= "" method="post">
and add a submit button too in your form
now on submit your form will post your form value
and you can catch them by php as to insert in database
<?php
if(isset($_POST['submit_btn_name']))
{
//your database connect
//catch all value, for example
$val=$_POST['check_value'];
//your insert query
}
?>
A checkbox will only post when it's checked.
A textfield will always get posted even when it's empty.
Use a form:
<form name="contactform" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Add a submit button:
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
PHP:
<?php
//var_dump($_POST);
$chkBP = $_POST['chkBP'];
$txtBPfrom = $_POST['txtBPfrom'];
$txtBPDetail = $_POST['txtBPDetail'];
//echo "-- $txtBPfrom $txtBPDetail --";
if ($chkBP == "BP"){
//echo"Bloodpressure = checked";
$sql="INSERT INTO patient_details (from, detail)
VALUES ('$txtBPfrom', '$txtBPDetail')";
mysql_query($sql);
}else{
echo"Bloodpressure = not checked";
}
?>
Demo: here
I am trying to insert an array of objects, by one entry, into a mySQL database during a registration form by using checkboxes.
At the moment the entry is showing up as "array" only, and no entries from the array.
I tried a for loop but it inserted the whole entry into the database a number of times instead of just the checkbox entries.
Any ideas on how to fix this?
<table border="0" cellpadding="3" cellspacing="1">
<tr>
<form id="callAjaxForm">
<div data-role="fieldcontain">
<td width="150">Name:</td>
<td width="300"><input type="text" name="inputName" value="" /> </td>
</tr>
<tr>
<td width="150">Username:</td>
<td width="300"><input type="text" name="inputUsername" value="" /></td>
</tr>
<tr>
<td width="150">Password:</td>
<td width="300"><input type="password" name="inputPassword" value="" /></td>
</tr>
<tr>
<td width="150">Date of Birth:</td>
<td width="300"><input type="date" name="inputDOB" value="" /></td>
</tr>
<tr>
<td width="150">Core Competencies:</td>
<td width="300">
<input type="checkbox" name="coreComp[]" value="1" />Honesty<br />
<input type="checkbox" name="coreComp[]" value="2" />Loyalty<br />
<input type="checkbox" name="coreComp[]" value="3" />Trust<br />
<input type="checkbox" name="coreComp[]" value="4" />Empathy<br />
<input type="checkbox" name="coreComp[]" value="5" />Respect</td>
</tr>
<tr>
<td colspan="2">
<button data-theme="b" id="submit" type="submit">Submit</button>
</td>
</tr>
<tr>
<td colspan="2">
<h3 id="notification"></h3>
</td>
</tr>
</div>
</form>
</table>
PHP code
<?php
include 'includes/Connect.php';
$name = $_POST['inputName'];
$username = $_POST['inputUsername'];
$password = $_POST['inputPassword'];
$dob = $_POST['inputDOB'];
$aCC = $_POST['coreComp'];
$encrypt_password=md5($password);
if(empty($aCC))
{
echo("Please pick at least one");
}
else
{
mysql_query("INSERT INTO Profile (`Name`,`Username`,`Password`,`D.O.B`,`CC`) VALUES('$name','$username','$encrypt_password','$dob','$aCC')") or die(mysql_error());
mysql_close();
echo("You have successfully registered!");
}
?>
Use implode and explode.
$aCC = implode( ';' , $_POST['coreComp'] );
$aCC will contain all values glued together with a ';' in between them.
With this you can store it in the database. When you retrieve the data from the database make sure to use explode.
http://nl1.php.net/manual/en/function.explode.php
You can't store an array, you can concat with comma and insert it into the field using implode() function in php. Check here.
Another way is to keep the Core competencies in separate table with values and populate the same in checkboxes with unique id in value of checkbox, so you can store the ids in the field as a comma separated one.
Hope this helps.
i create the code for text box, date and drop down within for loop and try to store those values in my DB using array concept. i search some code for internet and but not working good for storing data. I'm a newbie to PHP. Any idea would be great.
<?php
for($i=1;$i<=4;$i++)
{
?>
<tr>
<td>Name</td>
<td><input type="text" name="col_name[<?php echo $i;?>]" value=""/></td>
</tr>
<tr>
<td >Complete Address (Road Name, City, State, Country, Zip)</td>
<td><textarea name="add[<?php echo $i;?>]" value=""></textarea></td>
</tr>
<td> Date of birth(Ex.: mm-dd-yyyy)</td>
<td><input type="text" name="dob[<?php echo $i;?>]" id="dob<?php echo $i ;?>" value=""/>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="col_certi[<?php echo $i;?>]" value="Yes"/> Yes
<input type="radio" name="col_certi[<?php echo $i;?>]" value="No"/> No
</td>
<tr>
<td >Country</td>
<td><select name="countryid[<?php echo $i;?>]"/>
<option value="">Select</option>
</select>
</td>
</tr>
<tr>
<td>File Upload</td>
<td><input type="file" name="col_certi_file[<?php echo $i;?>]" value="<?php echo $fir_col_certi_file;?>"/></td>
</tr>
<tr><td align="center" colspan="4"><hr color="#6699CC" width="90%"/></td></tr>
<?php
}
?>
and i write the code for save the col_name data only using php
foreach($col_name as $colname)
$updatecol="update education set col_name='$colname' where employee_id=".$ employee_id;
if(!mysql_query($updatecol))
here the $colname last value only store in the database.. i want to store 4 value in database.. how to do it.
You can use for loop for inserting into DB some thing like this
for($i=0;$i<5;$i++)
{
insert query
}
Whats the best way to print the meterage1 and product1 posts as a single related string value when i loop through the foreach? So for example, product1 input box value which is related to meterage1 and product2 relates to meterage2.
Here is my form:
<form id="orderform" name"orderForm" action="tomypage.php" method="post">
<a id="add">+</a>
<table id="ordertable" width="533" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td width="33%">Product Code (e.g 66203)</td>
<td width="33%">mtrs sq Required (e.g 10)</td>
<td width="33%">Preview Image</td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode1" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage1" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode2" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage2" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
</tbody>
</table>
<button>Submit</button>
</form>
My current PHP foreach:
if ($_POST) {
foreach($_POST as $key => $value) {
$orderdetails = $key.' '.$value.'m<sup>2</sup><br />';
}
} else {
$orderdetails = 'No Order Details Selected';
}
You can assign form fields to an array, instead of numbering them, this will give you far more flexibility when accessing their values: i.e.
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
Then simply do:
print_r($_POST['meterage']);
Or access the $_POST['meterage'] array as required (i.e. loop through for action on each value).
If you then want to loop through the fieldsets, for related products/meterages, you could us:
$number_of_products=count($_POST['prodcode']);
for ( $i=0; $i<$number_of_products; $i++){
echo $_POST['prodcode'][$i]." has the meterage: ".$_POST['meterage'][$i]."<br/>";
}