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
farkewie
Forum Newbie
Posts: 22 Joined: Sat Jun 02, 2007 11:25 pm
Post
by farkewie » Tue Dec 11, 2007 5:00 pm
Hi i am trying o add a form that creates links on a site. the user can create up to 3 at a time, what i need is so they can fill out 1-3 links and when the form is proccessed it it adds them all to the database here is my code so far..
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
<table>
<tr>
<td><label>
<input type="text" name="text_1" id="text_1" />
</label></td>
<td><label>
<input type="text" name="link_1" id="link_1" />
</label></td>
<td><label>
<input type="text" name="location_1" id="location_1" />
</label></td>
</tr>
<tr>
<td><label>
<input type="text" name="text_2" id="text_2" />
</label></td>
<td><label>
<input type="text" name="link_2" id="link_2" />
</label></td>
<td><label>
<input type="text" name="location_2" id="location_2" />
</label></td>
</tr>
<tr>
<td><label>
<input type="text" name="text_3" id="text_3" />
</label></td>
<td><label>
<input type="text" name="link_3" id="link_3" />
</label></td>
<td><label>
<input type="text" name="location_3" id="location_3" />
</label></td>
</tr>
</table>
<label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
</p>
</form>
FRM;
if(isset($_GET['Submit'])){
for($i = 0; $i >= 3; $i++){
$inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
$inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
$inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
}
foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location'].'<br />';
}
}
?>
i get an error saying "inputs" is an undefined variable..
EDIT:
i am not doing the sql just yet, once the echo works ill add the sql
nathanr
Forum Contributor
Posts: 200 Joined: Wed Jun 07, 2006 5:46 pm
Post
by nathanr » Tue Dec 11, 2007 5:15 pm
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
<table>
<tr>
<td><label>
<input type="text" name="text_1" id="text_1" />
</label></td>
<td><label>
<input type="text" name="link_1" id="link_1" />
</label></td>
<td><label>
<input type="text" name="location_1" id="location_1" />
</label></td>
</tr>
<tr>
<td><label>
<input type="text" name="text_2" id="text_2" />
</label></td>
<td><label>
<input type="text" name="link_2" id="link_2" />
</label></td>
<td><label>
<input type="text" name="location_2" id="location_2" />
</label></td>
</tr>
<tr>
<td><label>
<input type="text" name="text_3" id="text_3" />
</label></td>
<td><label>
<input type="text" name="link_3" id="link_3" />
</label></td>
<td><label>
<input type="text" name="location_3" id="location_3" />
</label></td>
</tr>
</table>
<label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
</p>
</form>
FRM;
if(isset($_GET['Submit'])){
$inputs = array();
for($i = 0; $i >= 3; $i++){
$inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
$inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
$inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
}
foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location'].'<br />';
}
}
?>
try that.. ($inputs was an undefined variable.. so i defined it)
farkewie
Forum Newbie
Posts: 22 Joined: Sat Jun 02, 2007 11:25 pm
Post
by farkewie » Tue Dec 11, 2007 6:38 pm
Thanks for the reply,
I now dont get any errors but it will not echo anything.
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Tue Dec 11, 2007 6:53 pm
Looks like that loop will never run:
The middle term should be the condition for it to run, so it needs to be:
farkewie
Forum Newbie
Posts: 22 Joined: Sat Jun 02, 2007 11:25 pm
Post
by farkewie » Tue Dec 11, 2007 7:46 pm
Thank you for the reply,
it is almost working now, it is echoing data out..
only problem it always echo'es 4 "newlink" lines out even though there are only 3 form fields and the "for" statment is set to 3.
also if i only fill the form out once it still echo'es 4 times
here is my code
Code: Select all
<?php
if ( isset($_GET['Submit']) )
{
$inputs = array();
for ( $i = 0; $i <= 3; $i++ )
{
// i have changed the code from ( isset($_GET['text_' . $i]) ) to the below to try and resolve
$inputs[$i]['text'] = ( !empty($_GET['text_' . $i]) ) ? $_GET['text_' . $i] : '';
$inputs[$i]['link'] = ( !empty($_GET['link_' . $i]) ) ? $_GET['link_' . $i] : '';
$inputs[$i]['location'] = ( !empty($_GET['location_' . $i]) ) ? $_GET['location_' .
$i] : '';
}
foreach ( $inputs as $row )
{
echo 'New link ' . $row['text'] . ' -- ' . $row['link'] . ' -- ' . $row['location'] .
'<br />';
}
}
?>
result from filling all 3 form fields out.
result from filling one of the three forms out.
New link -- --
New link Link 1 --
http://link1.com -- Location 1
New link -- --
New link -- --