homepage Welcome to WebmasterWorld Guest from 107.21.163.227
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
form variable + submit button not concatenating with php
I want to make $preset + $submitbutton=3 = $preset3
digitaltoast




msg:4194964
 11:57 am on Sep 1, 2010 (gmt 0)

Got a seemingly unique problem with concatenating form variables + the name of the button used to submit.

This works:

$word="number"; $number=1; myalbum=$word.$number;

echoing $myalbum gives "number1". No surprises there.

BUT if I POST the form to a php script with the intention of writing to file ONLY the data on row on which the button was pushed, I get problems.

So, let's say I've got 10 rows, and the button for row 5 is pushed. If I get the script to echo what button was pushed ($button), I get "5" back. If I get the script to echo what's in the box in row 5 (in this case, "$number5=5") then by echoing $number5 I get 5.

But if I concatenate $number.$button, I get nothing, when I expect "number5". And yet, if I concat any two parts of the submitted data, it works as expected.

I've been over the variables section at php.net, I've been over the w3 forms tutorials. I've googled. . I've checked and triple checked my spelling.

I even started from scratch - again, it's almost as if appending the value of the button kills the concatenation process.

The output from the form: preset1=Name+of+preset+1&url1=http%3A%2F%2Fexample.com%2F1&preset2=Name+of+preset+2&url2=http%3A%2F%2Fexample.com%2F2&preset3=Name+of+preset+3&url3=http%3A%2F%2Fexample.com%2F3

The code for the form handler:

<?php
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Preset: " . $preset . " - Title:" . $title . $submitButton . " - Submit Button:" . $submitButton . "\n";
fwrite($fh, $stringData);
fclose($fh);
?>


The output from the above: Preset: 3 - Title:3 - Submit Button:3

So, we know it knows what buttons has been pressed. But not the output I expected.

But if I change the line to $stringData = "Preset: " . $preset3 . " - Title:" . $title3 . " - Submit Button:" . $submitButton . "\n";

then I get, as expected: Preset: Name of preset 3 - Title:http://www.example.com/3 - Submit Button:3

But of course, this is no good. I understood that if $preset.$submitButton would be the same as $preset3 if submitButton was 3.

Oh, and I've also tried $thepreset='$title' . $submitbutton; and then using that - all I get is "Title:$title"

 

StoutFiles




msg:4194966
 12:11 pm on Sep 1, 2010 (gmt 0)

I understood that if $preset.$submitButton would be the same as $preset3 if submitButton was 3.


Nope.

I've had this problem before though. I solved it with arrays.

$title[$submitButton] - Rework the problem using submitButton as your array key.

omoutop




msg:4195015
 2:12 pm on Sep 1, 2010 (gmt 0)


echo what's in the box in row 5 (in this case, "$number5=5") then by echoing $number5 I get 5.


Shouldn't this be: echo $_POST[$word.$button]; ?

If i read your code correctly:
$_POST[$word.$button] = $_POST['number5'] (for your example).

digitaltoast




msg:4195324
 10:42 pm on Sep 1, 2010 (gmt 0)

Thanks for both those replies - but again, this is where it gets weird. They looked hopeful, but I get:
"PHP Notice: Undefined variable: title"

but $title3 exists, and $submitButton=3, so, #*$!...I don't know.
I'd point you at my code if WebmasterWorld didn't ban such things.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved