/    Sign up×
Community /Pin to ProfileBookmark

adding rows to tables?

Hi. I’m not sure if this is possible but I have a field with several forms and a spot for a password input. I have the PHP script check the password and check the forms to see if they’re valid. I want a row to add to the html table and I want the values of those forms to be inputted into those rows. Is that possible?

I wrote a script to add tables using javascript but I would rather use PHP since it would be easier. Any suggestions?

Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 26.2005 — [code=php]<?
if($_POST['submitMethod'] == "add row"){//we are adding a row:
if(isset($_POST['box']) && isarray($_POST['box'])){
$noTrs = count($_POST['box']);
}else{
$noTrs = 5;
};
for($i = 0; $i < $noTrs; $i++){
echo "<tr>
<td><input type='text' name='box' value='".$_POST['box'][$i]."'></td>
</tr>";
};
};
?>[/code]
Copy linkTweet thisAlerts:
@ChrisSauthorApr 26.2005 — I was playing around with your suggestion and I was wondering if this would work. My html table is named "movies".

[code=html]
<form action="<?=$PHP_SELF?>" method="post" name="new_movie">
Movie Title:<br/>
<input type="text" size="20" name="title"><br/>
Genre:<br/>
<input type="text" size="20" name="genre"><br/>
Rating:<br/>
<input type="text" size="20" name="rating"><br/>
Password:<br/>
<input type="text" size="10" name="password"><br/>
<input type="submit" value="Submit"><br/>
</form>
[/code]

[code=php]
<?php
if($_POST['submit'])
{
if($_POST['password'] == 'password')
{
if(!$_POST['title'])
{
echo "Enter the Movie Title"
exit;
}
if(!$_POST['genre'])
{
echo "Enter the Genre"
exit;
}
if(!$_POST['rating'])
{
echo "Enter the Rating"
exit;
}
$date = date("m-d-Y");
if(isset($_POST['movies']) && isarray($_POST['movies']))
{
$noTrs = count($_POST['movies']);
}
else
$noTrs = 260;
for($i = 0; $i < $noTrs; $i++)
{
echo "<tr>
<td><input type='text' value='".$_GET['title'][$i]."'></td>
<td><input type='text' value='".$_GET['genre'][$i]."'></td>
<td><input type='text' value='".$_GET['rating'][$i]."'></td>
<td><input type='text' value='".$_GET['date'][$i]."'></td>
</tr>";
}
}
else
{
echo "Bad Password";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@grailquester5Apr 26.2005 — The question is - does it work? I'm going to assume not as you've used both $_POST and $_GET superglobals, and only 1 will be available from the form input...

Also:
[code=php]
if(isset($_POST['movies']) && isarray($_POST['movies']))
{
$noTrs = count($_POST['movies']);
}
else {
$noTrs = 260;
for($i = 0; $i < $noTrs; $i++)
{
echo "<tr>
<td><input type='text' value='".$_GET['title'][$i]."'></td>
<td><input type='text' value='".$_GET['genre'][$i]."'></td>
<td><input type='text' value='".$_GET['rating'][$i]."'></td>
<td><input type='text' value='".$_GET['date'][$i]."'></td>
</tr>";
}
}
[/code]

The way this code reads now, if the 'movies' post is an array then $noTrs will be set to the number of items in the array, and then the script won't do anything else. Otherwise (the next "else"), $noTrs will be set to 260 and then you'll use your "for" loop to create 260 rows. So you might want to think about moving that "}" up so that $noTrs is set to either the array count or 260, THEN the for loop occurs...

(And don't forget about the $_GET variables.)
Copy linkTweet thisAlerts:
@scragarApr 27.2005 — neither would work.

if you notice on my codes I close the if [b]before[/b] doing the loop.

if(isset($_POST['box']) && isarray($_POST['box'])){

$noTrs = count($_POST['box']);

}else{

$noTrs = 5;

}; //closing the IF.

for($i = 0; $i < $noTrs; $i++){

echo "<tr>
×

Success!

Help @ChrisS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...