how to post multiple inputs using a single submit button?

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

Post Reply
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

how to post multiple inputs using a single submit button?

Post by melbourne1815 »

hi i need help on posting multiple inputs in a single button...
here is my codes:

Code: Select all

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","'.$year.'","'.$sem.'")'))
				{
$re6 = mysql_query('select username from users where course = "BSIT" and yearlevel = "FOURTH"');
?>
<br />
<h1>Post Grade</h1>
<h1>IT, Fourth Year</h1>
<br />Please fill the following form to send The Grade<br />
<?
while($row = mysql_fetch_assoc($re6))
{
    echo'<form action="grade_post.php" method="post">';
        echo'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" readonly="readonly" id="recip" name="recip" />';
        echo'Subject<input type="text" value="'.htmlentities($otitle, ENT_QUOTES, 'UTF-8').'" id="title" name="title" />';
       echo'<input type="hidden" value="FOURTH" id="year" name="year" />';
      echo'<input type="hidden" value="FIRST" id="sem" name="sem" />';
        echo'Grade<input type="text" id="message" name="message" ><br />';
}
}
?>
i get all my recipients in every input type, but when i tried to post it in my database not all of them are posted rather only one of them are posted in my database :? :? ...what i want to happen is that all of my recipients in every input type will be posted in my database with different ids'...help pls...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

Try this and you will get arrays for each $_POST variable:

Code: Select all

$n = 0;
while($row = mysql_fetch_assoc($re6))
{
    echo'<form action="grade_post.php" method="post">';
    echo'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" readonly="readonly" id="recip" name="recip[' . $n . ']" />';
    echo'Subject<input type="text" value="'.htmlentities($otitle, ENT_QUOTES, 'UTF-8').'" id="title" name="title[' . $n . ']" />';
    echo'<input type="hidden" value="FOURTH" id="year" name="year[' . $n . ']" />';
    echo'<input type="hidden" value="FIRST" id="sem" name="sem[' . $n . ']" />';
    echo'Grade<input type="text" id="message" name="message[' . $n . ']" ><br />';
    ++$n;
}
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

hi i'm sorry but i still get the same errors but this time my grades and names dont appear and i still get one id...
this is my whole code...
i appreciate the help...tnks

Code: Select all

<?php
if(isset($_SESSION['username']))
{
$form = true;
$otitle = '';
$orecip = '';
$omessage = '';
$oyear= '';
$osem = '';
if(isset($_POST['title'], $_POST['recip'], $_POST['message'],$_POST['year'],$_POST['sem']))
{
	$otitle = $_POST['title'];
	$orecip = $_POST['recip'];
	$omessage = $_POST['message'];
	$oyear = $_POST['year'];
	$osem = $_POST['sem'];
	if(get_magic_quotes_gpc())
	{
		$otitle = stripslashes($otitle);
		$orecip = stripslashes($orecip);
		$omessage = stripslashes($omessage);
		$oyear = stripslashes($oyear);
		$osem = stripslashes($osem);
	}
	if($_POST['title']!='' and $_POST['recip']!='' and $_POST['message']!='' and $_POST['year']!="" and $_POST['sem']!="")
	{
		$title = mysql_real_escape_string($otitle);
		$recip = mysql_real_escape_string($orecip);
		$message = mysql_real_escape_string(nl2br(htmlentities($omessage, ENT_QUOTES, 'UTF-8')));
		$year = mysql_real_escape_string($oyear);
		$sem = mysql_real_escape_string($osem);
		$dn1 = mysql_fetch_array(mysql_query('select count(id) as recip, id as recipid, (select count(*) from post_grade) as npm from users where username="'.$recip.'"'));
		if($dn1['recip']<=100)
		{
			if($dn1['recipid']!=$_SESSION['username'])
			{
				$id = $dn1['npm']+1;
				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","'.$year.'","'.$sem.'")'))
				{
?>
<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.';
			}
		}
		else
		{
			$error = 'The recipient does not exists.';
		}
	}
	else
	{
		$error = 'A field is empty. Please fill of the fields.';
	}
}
elseif(isset($_GET['recip']))
{
	$orecip = $_GET['recip'];
}
if($form)
{
if(isset($error))
{
	echo '<div class="message">'.$error.'</div>';
}

?>
<?
$re6 = mysql_query('select username from users where course = "BSIT" and yearlevel = "FOURTH"');
?>
<br />
<h1>Post Grade</h1>
<h1>IT, Fourth Year</h1>
<br />Please fill the following form to send The Grade<br />
<?
while($row = mysql_fetch_assoc($re6))
{
    echo'<form action="grade_post.php" method="post">';
        echo'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" readonly="readonly" id="recip" name="recip" />';
        echo'Subject<input type="text" value="'.htmlentities($otitle, ENT_QUOTES, 'UTF-8').'" id="title" name="title" />';
       echo'<input type="hidden" value="FOURTH" id="year" name="year" />';
      echo'<input type="hidden" value="FIRST" id="sem" name="sem" />';
        echo'Grade<input type="text" id="message" name="message" ><br />';
}
?>
thank you for the help...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

melbourne1815 wrote:hi i'm sorry but i still get the same errors but this time my grades and names dont appear and i still get one id...
Well, you didn't use the code I posted so you will still get only one id.
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

im sorry i used your code but i still get the errors...i just posted my codes before i use yours just to show if i have errors in my whole codes...i hope you can check my whole codes and correct it with yours...i need your help pls...tnk u
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

What errors did you get?
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

when i used your code and try to post it...i only get one id in my database and the recipient, subject, and grade doesn't have anything
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

That's not really an error. That's just the program doing exactly what you told it to do. Try my code and when you receive the post variables the will all be arrays.
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

hi im sorry i used your codes but i still got nothing...pls help

Code: Select all

<?php
if(isset($_SESSION['username']))
{
$form = true;
$otitle = '';
$orecip = '';
$omessage = '';
$oyear= '';
$osem = '';
if(isset($_POST['title'], $_POST['recip'], $_POST['message'],$_POST['year'],$_POST['sem']))
{
	$otitle = $_POST['title'];
	$orecip = $_POST['recip'];
	$omessage = $_POST['message'];
	$oyear = $_POST['year'];
	$osem = $_POST['sem'];
	if(get_magic_quotes_gpc())
	{
		$otitle = stripslashes($otitle);
		$orecip = stripslashes($orecip);
		$omessage = stripslashes($omessage);
		$oyear = stripslashes($oyear);
		$osem = stripslashes($osem);
	}
	if($_POST['title']!='' and $_POST['recip']!='' and $_POST['message']!='' and $_POST['year']!="" and $_POST['sem']!="")
	{
		$title = mysql_real_escape_string($otitle);
		$recip = mysql_real_escape_string($orecip);
		$message = mysql_real_escape_string(nl2br(htmlentities($omessage, ENT_QUOTES, 'UTF-8')));
		$year = mysql_real_escape_string($oyear);
		$sem = mysql_real_escape_string($osem);
		$dn1 = mysql_fetch_array(mysql_query('select count(id) as recip, id as recipid, (select count(*) from post_grade) as npm from users where username="'.$recip.'"'));
		if($dn1['recip']<=100)
		{
			if($dn1['recipid']!=$_SESSION['username'])
			{
				$id = $dn1['npm']+1;
				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","'.$year.'","'.$sem.'")'))
				{
?>
<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.';
			}
		}
		else
		{
			$error = 'The recipient does not exists.';
		}
	}
	else
	{
		$error = 'A field is empty. Please fill of the fields.';
	}
}
elseif(isset($_GET['recip']))
{
	$orecip = $_GET['recip'];
}
if($form)
{
if(isset($error))
{
	echo '<div class="message">'.$error.'</div>';
}

?>
<?
$re6 = mysql_query('select username from users where course = "BSIT" and yearlevel = "FOURTH"');
?>
<br />
<h1>Post Grade</h1>
<h1>IT, Fourth Year</h1>
<br />Please fill the following form to send The Grade<br />
<?
$n = 0;
while($row = mysql_fetch_row($re6))
{
    echo'<form action="grade_post.php" method="post">';
    echo'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" readonly="readonly" id="recip" name="recip[' . $n . ']" />';
    echo'Subject<input type="text" value="'.htmlentities($otitle, ENT_QUOTES, 'UTF-8').'" id="title" name="title[' . $n . ']" />';
    echo'<input type="hidden" value="FOURTH" id="year" name="year[' . $n . ']" />';
    echo'<input type="hidden" value="FIRST" id="sem" name="sem[' . $n . ']" />';
    echo'Grade<input type="text" id="message" name="message[' . $n . ']" ><br />';
    ++$n;
}
?>
?>
pls help
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

Try this code so you can see what the form is doing:

Code: Select all

</head>
<body>
<?php
if (isset($_POST['recip'])) {
	echo '<pre>' . print_r($_POST, 1) . '</pre>';
} else {
    echo'<form action="' . $_SERVER['SCRIPT_NAME'] . '" method="post">';
	$otitle = 'foo';
	$rows = array(
		0 => array('username' => 'zero'),
		1 => array('username' => 'one'),
		2 => array('username' => 'two'),
		);
	$n = 0;
	foreach ($rows as $row)
	{
	    echo'Recipient<span class="small">(Username)</span><input type="text" value="'.$row['username'].'" readonly="readonly" id="recip" name="recip[' . $n . ']" />';
	    echo'Subject<input type="text" value="'.htmlentities($otitle, ENT_QUOTES, 'UTF-8').'" id="title" name="title[' . $n . ']" />';
	    echo'<input type="hidden" value="FOURTH" id="year" name="year[' . $n . ']" />';
	    echo'<input type="hidden" value="FIRST" id="sem" name="sem[' . $n . ']" />';
	    echo'Grade<input type="text" id="message" name="message[' . $n . ']" ><br />';
	    ++$n;
	}	
	echo '<input type="submit" value="send" name="send" /></form>';
}
?>

</body>
</html>
Once you get the form submitting the data you want, then add the database stuff.
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

hi...im sorry again but i still got nothing on my database...i still cant get my form to submit the data i need on my database the only thing i get on my database is the id and there is only one...im sorry i hope you will not get tired on helping me..pls thank you
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to post multiple inputs using a single submit button

Post by Christopher »

So, did you try the code I posted? It shows what data is returned by the form. Then add the database code to build the $rows array -- and see what the data looks like. Finally, build SQL INSERT statements from the data.
(#10850)
melbourne1815
Forum Newbie
Posts: 13
Joined: Wed Sep 28, 2011 4:01 am

Re: how to post multiple inputs using a single submit button

Post by melbourne1815 »

hi im sorry it took me so long i hope you will still help me...finally i got my code to post many id's that i needed on my database but the problem is not all my data is posted in my dtababse only the id and the recip is posted plus the id's that o got is all the same...when i tried to post 3 different items...i got on my dtabase is a 3 row with only the recip posted with the same id's

here is my code hope you can help me again:

<?
foreach($_POST['recip'] as $value)
{
$id = $dn1['npm']+1;
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","'.$year.'","'.$sem.'")'))
{
?>
<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';
}
}

$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 '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 '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 '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 />';
}
echo '<input type="submit" value="Send" id="up" name="up"/>';
echo '</form>';
?>
Post Reply