TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-19-2007, 02:45 PM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default how to $_POST this?

if i have a code like this:
PHP Code:
for($i=0;$i<3;$i++){
echo 
'<input type="submit" name="submit'.$i.'" value='.$i.'>';
}
if (isset(
$_POST["submit$i"])){<--------this is not correct.
do 
the following

how would i post for name that is incremented?
meshi is offline  
Reply With Quote
Old 12-19-2007, 02:51 PM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I do believe that you would need a second loop which would wrap your if statement.

And I'm not positive about $i's scope, but I don't think you can call $i outside of your for loop.
bdm is offline  
Reply With Quote
Old 12-19-2007, 03:01 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by bdm View Post
I do believe that you would need a second loop which would wrap your if statement.

And I'm not positive about $i's scope, but I don't think you can call $i outside of your for loop.
Correct.
He must create another loop inside his if statement
Tanax is offline  
Reply With Quote
Old 12-19-2007, 03:03 PM   #4 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Correct.
He must create another loop inside his if statement
You mean a for loop wrapping his if statement.
bdm is offline  
Reply With Quote
Old 12-19-2007, 03:26 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by bdm View Post
You mean a for loop wrapping his if statement.
lol, woops yea
Tanax is offline  
Reply With Quote
Old 12-19-2007, 03:10 PM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Yes, initializing the same, twice, isn't possible. (edit, you CAN reset a counter with the reset(); command)

PHP Code:
<?php

/**
 * @author ReSpawN
 * @copyright 2007
 */

    
$count 3;
    for(
$i 0$i <= $count$i++){
        echo 
'<input type="submit" name="submit'.$i.'" value="Submit'.$i.'" />';
    }
    
    if (isset(
$_POST['submit'])) {
        echo 
$_POST['submit'].' has been set.';
    }  
    
?>
__________________
"Life is a bitch, take that bitch on a ride"

Last edited by ReSpawN : 12-19-2007 at 06:21 PM.
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-20-2007, 04:33 PM   #7 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default

sori i think a little confuse here

PHP Code:
for($i=1;$i<4;$i++){ 
echo 
'<input type="text" name=="a'.$i.'" value='.$i.'><input type="submit" name="submit'.$i.'" value="Submit">'

if (isset(
$_POST["submit$i"])){<--------this is not correct
echo 
$_POST["a$i"];

the code iterpretation above is this
1 Submit
2 Submit
3 Submit
what i want is to display the value of the textbox.how can i
display its value?when its submit button hit?
meshi is offline  
Reply With Quote
Old 12-20-2007, 06:25 PM   #8 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Perhaps because the if( $_POST['submit$i] ) is always checking $_POST[submit4]. You may want to put the submit button into a form control before expecting to have a $_POST in the first place. If you're generating all the 4 buttons in the same form, note that only one button will be available at a given point (e.g.: you can't set all the 4 post buttons to be available after posting the form).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-20-2007, 07:30 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about either creating an array of fields, like so:

html4strict Code:
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />

Or using a foreach loop on the POST? Like this:

php Code:
foreach($_POST as $szKey => $szValue)
{
    if(preg_match('~^submit~i', $szKey))
    {
        echo 'Set';
    }
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-20-2007, 07:37 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

To summarise the comments saying to use a second for loop, here's a visual example.

PHP Code:
// First loop, to output the form elements
for ($i 1$i <= 3$i++)

    
printf('<input type="text" name="a%d" value="%d" /><input type="submit" name="submit%d" value="Submit" />'$i$i$i);
    
// or, see: http://www.talkphp.com/showthread.php?t=1129
    // printf('<input type="text" name="a%1$d" value="%1$d" /><input type="submit" name="submit%1$d" value="Submit" />', $i);


// Second loop, to respond if the form was posted
for ($i 1$i <= 3$i++)
{
    
// Alternative: isset($_POST['submit' . $i])
    
if (array_key_exists('submit' $i$_POST))
    {
        echo 
$_POST['a' $i];
    }

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
codefreek (01-08-2008)
Old 01-08-2008, 10:23 AM   #11 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default

thanks guys..i understand now
meshi is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design