Jump to content

Inserting Conents which are submitted in an array


nithan

Recommended Posts

I have a form which displays name and a text field after fetching names from the database in an array:

<input id='<? echo $name; ?>' type='text' name='students[<? echo $name; ?>]' disabled="disabled"/>

Example: Name and TextField.

 

 

When I want to insert it in database, I can't insert. Following is the code that I am using. Am I doing right thing?

 

$marks= $_POST['students'];

$marksn = array_filter($marks);

 

foreach($marksn as $name=>$vals){

trim($vals) !== '';

 

 

$query="INSERT INTO attend (Name, Training, Date, Attendance, Marks) VALUES ('$name','$trainings','$today','$val','$vals')";

$result=mysql_query($query);

 

 

Link to comment
Share on other sites

OK so straight up without seeing any of your code, you are going to get nothing in your $_POST array as your text fields are all disabled.

 

If you would like further help, please post your form code as well as the code you are using to process the $_POST array.

Link to comment
Share on other sites

OK so straight up without seeing any of your code, you are going to get nothing in your $_POST array as your text fields are all disabled.

 

If you would like further help, please post your form code as well as the code you are using to process the $_POST array.

-----------------------------------------

Thank you. This is the code that I am using: I am very new to PHP. Please correct me if I am wrong.

This is my Form code:

<style type="text/css">
table.hovertable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th {
background-color:#c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertable tr {
background-color:#d4e3e5;
}
table.hovertable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}

table.hovertable {
width: 30%;
text-align: center;
font: 14px Verdana, Arial, Helvetica, sans-serif;
}
tr.yellow td {
border-top: 1px solid #FB7A31;
border-bottom: 1px solid #FB7A31;
background: #FFC;
font: 20px Verdana, Arial, Helvetica, sans-serif;
}
</style>
<script>
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
    document.location = delUrl;
  }
}
</script>
<script type="text/javascript">

  function enableText(checkBool, textID)
  {
    textFldObj = document.getElementById(textID);
    //Disable the text field
    textFldObj.disabled = !checkBool;
    //Clear value in the text field
    if (!checkBool) { textFldObj.value = ''; }
  }

</script>
<html>
<body>
<?
include("attendrop.php");

?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


<label class="form-label-left" id="label_4" for="input_4">Select Training:  </label>
        <div id="cid_4" class="form-input">
          <select class="form-dropdown" style="width:150px" id="input_4" name="training">
            <OPTION VALUE=0>Choose
<?=$optionsa?>
</SELECT> 



<input type="submit" name="submit" value="Mark Attendance"><br>

</form>

<?php
if(isset($_POST['submit']))
$training= $_POST['training'];
{

?>
<div align="center">
Mark Attendance for <font color="red"> <? echo $training; ?> </font>Training. 
<form method="post" action="attendinsert.php" >
<input type="hidden" value="<? echo $training; ?>" name="training" />



<table class="hovertable" align="center"  width="30%" cellpadding="50" >
<tr class="yellow">
<tr>
<td><font face="Arial, Helvetica, sans-serif">Name</font></td>
<td><font face="Arial, Helvetica, sans-serif">OSS ID</font></td>
<td><font face="Arial, Helvetica, sans-serif">Mark Attendance</font></td>
<td><font face="Arial, Helvetica, sans-serif">Marks</font></td>



<?php
include("database.php");
$training= $_POST['training'];
   $from= $_POST['from'];
$to= $_POST['to'];

$tablename="agentstable";
$query="SELECT * FROM agentstable ORDER BY name ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
$options="";

$i=0;
while ($i < $num) {


$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$empcode=mysql_result($result,$i,"emp_code");
$oss=mysql_result($result,$i,"oss_username");
$emails=mysql_result($result,$i,"emailaddress");
?>


<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

<td><? echo $name; ?></td><td><? echo $oss; ?></td><td>
<input id="myCheckBox" type='checkbox' onclick="enableText(this.checked, '<? echo $name; ?>');" name='student[<? echo $name; ?>]' value='1' />
</td><td> <input id='<? echo $name; ?>' type='text' name='students[<? echo $name; ?>]' disabled="disabled"/></td>
</tr>




<?php
$i++;
}

?>



</table>

<input type='submit' value='Submit' name='submit_button' />
</form>
</div>
<?
}
?>
</div> 
</body>
</html> 

This code is to insert:

<?php
include("database.php");
$tablename="agentstable";
date_default_timezone_set('Asia/Calcutta');
$format = 'm/d/Y';
$today = date ( $format );
  if(!isset($_POST['submit_button'])){
   //form has not been submitted
   return;
  }

  //form HAS been submitted
  $student_array = $_POST['student'];
$trainings = $_POST['training'];
$marks= $_POST['students'];
$marksn = array_filter($marks);
/*foreach($marks as $marks=>$vals){





}
*/
foreach($student_array as $name=>$val){
   $val = ($val == "1")?1:0;  
if($val == 1) 

{
    //$val needs to be 0 or 1.
  

foreach($marksn as $name=>$vals){
trim($vals) !== '';


$query="INSERT INTO attend (Name, Training, Date, Attendance, Marks) VALUES ('$name','$trainings','$today','$val','$vals')";
$result=mysql_query($query);
}
}

if($result){
echo("<br>Input data is succeed");
echo "<a href='attendanceform.php'>Back to Attendance Page</a>";

} 
else
{
    echo("<br>Input data is fail");

}
  }


mysql_close();

?>

Link to comment
Share on other sites

Im sorry but if you are sending data via a standard html form then this

 

If you want to send a value with a disabled text field you have to set something to the value attribute.

 

<input type="text" name="name" id="id" value="some value" />

 

is incorrect.

 

As per http://www.w3schools.com/tags/att_input_disabled.asp

 

Disabled <input> elements in a form will not be submitted.
Link to comment
Share on other sites

Im sorry but if you are sending data via a standard html form then this

 

If you want to send a value with a disabled text field you have to set something to the value attribute.

 

<input type="text" name="name" id="id" value="some value" />

 

is incorrect.

 

As per http://www.w3schools.com/tags/att_input_disabled.asp

 

Disabled <input> elements in a form will not be submitted.

 

:shrug: My bad

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.