Page 1 of 1

how to post multiple inputs with the same name?

Posted: Tue Feb 14, 2012 7:13 pm
by melbourne1815
hi i need help the about my code. i can get the form to post three row in my database but not all the values are being posted. the problem is when i post the form only one is posted in the three different row that is the recipid only but what i want to happen is for the recipid, title and message will be posted in three different row but also the output depends on what the user inputed so not all the three rows has the same output

here is my code:
<?php
if($dn1['recipid']!=$_SESSION['username'])
{

$id = $dn1['npm']+1;

foreach($_POST['id'] as $dn1['recipid']){
if(mysql_query('insert into post_grade (id, id2, title, user1, user2, message, timestamp, user1read, user2read,Year,Sem)values("'.$id.'", "1", "'.$title.'", "'.$_SESSION['userid'].'", "'.$dn1['recipid'].'", "'.$message.'", "'.time().'", "yes", "no","SECOND","SECOND")'))
{
?>
<div class="message">The Grade has been successfully sent to the Student.<br />
<a href="list_grade.php">List of my Grades</a></div>
<?php
$form = false;
}
else
{
$error = 'An error occurred while sending the message';
}
}
}
else
{
$error = 'You cannot send a grade to yourself/fellow instructors.';
}
?>
<br />
<h1>Post Grade</h1>
<h1>IT, Fourth Year</h1>
<br />Please fill the following form to send The Grade<br />

<?
$re6 = mysql_query('select username, id from users where course = "BSIT" and yearlevel = "SECOND" order by id limit 1');
$re7 = mysql_query('select username, id from users where course = "BSIT" and yearlevel = "SECOND" order by id limit 1,1');
$re8 = mysql_query('select username, id from users where course = "BSIT" and yearlevel = "SECOND" order by id limit 2,1');
echo'<form action="grade_post.php" method="post">';
if($row = mysql_fetch_array($re6)){
echo '<input type="hidden" id="id" name="id[]" value="' . $row['id'] . '" />';
echo 'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" id="recip" name="recip[]" readonly="readonly"/>';
echo 'Subject<input type="text" value=" '.htmlentities($otitle, ENT_QUOTES, 'UTF-8').' " id="title" name="title[]" />';
echo '<input type="hidden" id="year" name="year[]" value="SECOND" />';
echo '<input type="hidden" id="sem" name="sem[]" value="SECOND" />';
echo 'Grade<input type="text" id="message" name="message[]" /><br />';

}
if($row1 = mysql_fetch_array($re7)){
echo '<input type="hidden" id="id" name="id[]" value="' . $row1['id'] . '" />';
echo 'Recipient<span class="small">(Username)</span><input type="text" value="'.$row1['username'].'" id="recip" name="recip[]" readonly="readonly"/>';
echo 'Subject<input type="text" value=" '.htmlentities($otitle, ENT_QUOTES, 'UTF-8').' " id="title" name="title[]" />';
echo '<input type="hidden" id="year" name="year[]" value="SECOND" />';
echo '<input type="hidden" id="sem" name="sem[]" value="SECOND" />';
echo 'Grade<input type="text" id="message" name="message[]" /><br />';

}
if($row2 = mysql_fetch_array($re8)){
echo '<input type="hidden" id="id" name="id[]" value="' . $row2['id'] . '" />';
echo 'Recipient<span class="small">(Username)</span><input type="text" value="'.$row2['username'].'" id="recip" name="recip[]" readonly="readonly"/>';
echo 'Subject<input type="text" value=" '.htmlentities($otitle, ENT_QUOTES, 'UTF-8').' " id="title" name="title[]" />';
echo '<input type="hidden" id="year" name="year[]" value="SECOND" />';
echo '<input type="hidden" id="sem" name="sem[]" value="SECOND" />';
echo 'Grade<input type="text" id="message" name="message[]" /><br />';

}
?>

help is needed :( :(

Re: how to post multiple inputs with the same name?

Posted: Wed Feb 15, 2012 3:58 am
by G l a z z
Try this:

Code: Select all

<?php

var_dump($_POST);

?>

<form action="" method="post">

<input type="hidden" id="id" name="test[1][id]" value="ID1" /><br />
Recipient<span class="small">(Username)</span><input type="text" value="Username1" id="recip" name="test[1][recip]" readonly="readonly"/><br />
Subject<input type="text" value="SUBJECT1" id="title" name="test[1][title]" /><br />
<input type="hidden" id="year" name="test[1][year]" value="YEAR1" />
<input type="hidden" id="sem" name="test[1][sem]" value="SEM1" />
<input type="text" id="message" name="test[1][message]" value="MESSAGE1" />

<input type="hidden" id="id" name="test[2][id]" value="ID2" /><br />
Recipient<span class="small">(Username)</span><input type="text" value="Username2" id="recip" name="test[2][recip]" readonly="readonly"/><br />
Subject<input type="text" value="SUBJECT2" id="title" name="test[2][title]" /><br />
<input type="hidden" id="year" name="test[2][year]" value="YEAR2" />
<input type="hidden" id="sem" name="test[2][sem]" value="SEM2" />
<input type="text" id="message" name="test[2][message]" value="MESSAGE2" />

<input type="submit" value="Send..." />

</form>

Re: how to post multiple inputs with the same name?

Posted: Wed Feb 15, 2012 4:43 pm
by melbourne1815
hi i still cant get it to post in my database but this time the recip only post array on three rows...need help pls

Re: how to post multiple inputs with the same name?

Posted: Wed Feb 15, 2012 5:13 pm
by G l a z z
I still don't get what you are trying to do...