how to post multiple inputs with the same name?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

how to post multiple inputs with the same name?

Postby melbourne1815 » Tue Feb 14, 2012 8:13 pm

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.

<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.';
}
?>


<h1>Post Grade</h1>
<h1>IT, Fourth Year</h1>

Please fill the following form to send The Grade


<?
$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[]" />
';

}
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[]" />
';

}
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[]" />
';

}
?>

help is needed :( :(
melbourne1815
Forum Newbie
 
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

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

Postby G l a z z » Wed Feb 15, 2012 4:58 am

Try this:
Syntax: [ Download ] [ Hide ]
<?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>
G l a z z
Forum Newbie
 
Posts: 24
Joined: Sun Feb 12, 2012 11:33 pm

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

Postby melbourne1815 » Wed Feb 15, 2012 5:43 pm

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
melbourne1815
Forum Newbie
 
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

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

Postby G l a z z » Wed Feb 15, 2012 6:13 pm

I still don't get what you are trying to do...
G l a z z
Forum Newbie
 
Posts: 24
Joined: Sun Feb 12, 2012 11:33 pm


Return to PHP - Code

Who is online

Users browsing this forum: Google [Bot] and 12 guests