For those that have a good understanding of PHP arrays, I have a form that takes in multiple inputs that can be added to the form by js, the data is added to the codeRef[] array and I have tried to do several different things to get the data into mysql. If it runs I dont get any error nor does it enter into the database. Thanks for any help anyone can give.
Jon
<form action="plan_review_comment_form.php?id_plan_review="<?php $_GET['id_plan_review'];?> method="post" id="frmMain">
<p>
<ol id="comment">
<li>
<label name="idPlanRev"> Plan Review No:</label>
<input name"codeRef[]"id="planReviewId" value="<?php echo $id_plan_review;?>"/>
<label name="emply"> Employee:</label>
<input name"codeRef[]"id="emply" value="<?php echo $_SESSION['contact_id'];?>"/><!---->
<label name="codeRef"> Code Reference:</label>
<input name="codeRef[]" id="codeRef" type="text" value=""/>
<label name="comment"> Comment:</label>
<input name="codeRef[]" id="comments" type="text" size="60" value=""/>
<label name="date">Date:</label>
<input name"codeRef[]"id="date" type="date" value="<?php echo date('Y-m-d');?>"/><!---->
<input type="button" id="add" value="Add Line"/>
</li>
</ol>
<input type="submit" id="save" value="Save "/>
</p>
</form>
<?php
?>
</div>
<!-- end content div -->
</div>
<!-- end wrapper div -->
<script type="text/javascript" src="/js/jquery.js"></script>
<!-- event handler function to add more fields boxes -->
<script type="text/javascript">
$(document).ready(function() {
$('#add').click(function()
{
var str ='<li>';
str += '<label> Plan Review No</label><input name="codeRef[]" id="planReviewId" type="text" value="<?php echo $id_plan_review;?>" />';
str += '<label> Employee</label><input name="codeRef[]" id="emply" type="text" value="<?php echo $_SESSION['contact_id'];?>" />';
str += '<label> Code Reference</label><input name="codeRef[]" type="text" value="" />';
str += '<label> Comment</label><input name="codeRef[]" type="text" value="" size=60/>';
str += '<label> Date</label><input name="codeRef[]" id="date" type="date" value="<?php echo date('Y-m-d');?>" />';
str +='<input type="button" value="Remove" class="remove"/>';
str += '</li>';
$('#comment').append(str);
});
$('.remove').live('click',function()
{
$(this).parent('li').remove();
});
});
</script>
<script type="text/javascript">
/* var mutli_codeRef=document.frmMain.input["codeRef[]"];
for(i=0;i<mutli_codeRef.length;i++)
{
alert(mutli_codeRef[i].value);
}*/
</script>
<!-- event handler function for the forms submit event ------->
<script type="text/javascript">
$(document).ready(function()
{
$('#frmMain').submit(function()
{
var flag = true;
$('#error').empty();
$('.required').each(function()
{
if(jQuery.trim($(this).val()) == '')
{
flag = false;
}
});
if(!flag)
{
$('#error').html('Please fill all the fields');
return false;
}
else
{
return true;
}
});
});
</script>
<?php
//if(isset($_POST['submit'])){
$myArr ="";
$arr=$_POST['codeRef'];
$cnt=count($arr)-1;
for($i=0;$i<$cnt;$i++){
$myArr.="'$arr[$i]',";
}
$myArr.="'$arr[$cnt]'";
print_r($myArr);
'<br>';
//if($_POST)
{
if(is_array($myArr)){
$sql="INSERT INTO plan_review_comment (
id_plan_review,
employee_id,
code_ref,
comment,
date
)values ";
$valuesArr = array();
foreach($myArr as $row){
$planReviewId = (int) $row['id_plan_review'];
$employeeId = $mysql_real_eascape_string($_SESSION['contact_id']);
$code_ref = $mysqli_real_escape_string($row['code_ref']);
$comment = $mysql_real_escape_string($_row['comment']);
$date_submit = date('Y-m-d');
$valuesArr[] = "('$planReviewId','$employId','$code_ref','$comment','$date_submit')";
}
$sql.= implode(',',$valuesArr);
$mysql_query($sql) or exit(mysql_error());
}
?><pre>
}
<?php
//echo print_r($_POST['codeRef']);
}
//foreach($_POST['codeRef'] && $_POST['codeRef2'] as $key)print_r($_POST['codeRef2']);
//echo ($key .'<br>');
?><pre>