469,271 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to get all set $_post ?

25
So I have a javascript on my page, which makes me able to add another input text field when I click on a button. And it's named "test" when I click the button it creates another input named test1, on another click it creates test2 and so on... But I have a problem on my form processing page, I need to get know which is the last "test" and also I need to set variables for each of them. I imagine some small code which will make something like this for me:

$test = $_POST('test');
$test2 = $_POST('test2');
$test3 = $_POST('test3');
$test4 = $_POST('test4');

And it will go on until last test. Thanks for helping me.
Aug 4 '09 #1
17 4107
Markus
6,050 Expert 4TB
@Alino
There is a solution to this problem of yours - you can have your input elements become an array, that is to say, you can access them as an array inside the post array. To do this, you simply name the elements the same, and append '[]' to them, like so:

Expand|Select|Wrap|Line Numbers
  1. <input name="my_array[]" value="1" />
  2. <input name="my_array[]" value="2" />
  3. <input name="my_array[]" value="3" />
  4. <input name="my_array[]" value="4" />
  5.  
And to access those elements in PHP, you do something like so:

Expand|Select|Wrap|Line Numbers
  1. print_r($_POST['my_array']);
  2.  
  3. foreach($_POST['my_var'] as $key => $my_var) {
  4.     printf("Key #%d has the value <strong>%s</strong>", $key, $my_var);
  5. }
  6.  
Mark.
Aug 4 '09 #2
Alino
25
thank you man, it works
Aug 6 '09 #3
Markus
6,050 Expert 4TB
@Alino
No problamo.

Mark.
Aug 6 '09 #4
Alino
25
now I have problem with imploding it
Expand|Select|Wrap|Line Numbers
  1. implode(" ", $pod_albumom_photo_id);
I receive the following error message then:
Warning: implode() [function.implode]: Invalid arguments passed in /storage/www1/9/site46049/wwwroot/xxx/process.php on line 90
what's wrong? ;[ how to fix it?
Aug 6 '09 #5
Markus
6,050 Expert 4TB
Seems like $pod_albumom_photo_id is not an array. What does var_dump($pod_albumom_photo_id); produce?
Aug 6 '09 #6
Alino
25
it produces
string(4) "12"
thats number of my inputs (4) and value from the last. "12"
Aug 6 '09 #7
Markus
6,050 Expert 4TB
@Alino
No, that can't be right; 4 should be the length of the string, and it says 'string' not 'array'. :S
Aug 6 '09 #8
Alino
25
so what i have to do to have it in array? :P
Aug 6 '09 #9
dlite922
1,584 Expert 1GB
@Alino
put empty brackets at the html form input name.
Aug 6 '09 #10
Alino
25
i have done that already, my code looks like
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. function addfile()
  4. {
  5. var obj = document.getElementById("sprytextfield2").cloneNode(true);
  6. document.getElementById('upfiles').appendChild(document.createElement('br') );
  7. document.getElementById('upfiles').appendChild(obj);
  8. }
  9.  
  10. </script>
Expand|Select|Wrap|Line Numbers
  1.         <fieldset class="border" id="upfiles">
  2.         <legend>Vymazať fotku v profile pod albumom:</legend>
  3.         <label for="pod_albumom_photo_id[]">ID fotky:</label><br />
  4.         <span id="sprytextfield2">
  5.         <input type="text" name="pod_albumom_photo_id[]" value="" onKeyPress="ipt_onkeypress()" />
  6. <span class="textfieldInvalidFormatMsg">Iba cifry prosím. </span></span>
  7.         <input class="button" type="button" value="Pridať pole" onclick="return addfile()" />
  8.         </fieldset>
and in my form process page:
Expand|Select|Wrap|Line Numbers
  1.             foreach($_POST['pod_albumom_photo_id'] as $key => $pod_albumom_photo_id)
  2.             {
  3.             printf("Foto s ID <strong>%s</strong> bude vymazaná<br />", $pod_albumom_photo_id);
  4.             }
  5.             $x = implode(" ", $pod_albumom_photo_id);
now i just need to get it work, that implode.
Aug 6 '09 #11
dlite922
1,584 Expert 1GB
When you do a foreach, you're already "imploding" in a sense.

$_POST['pod_albumom_photo_id'] /is/ an array (do a var dump if you don't' believe me)

What do foreach's do? traverse arrays? to what? strings. thus $pod_albumom_photo_id is a string.

If you want it to be an array, just assign what's in the post:

$pod_albumom_photo_id = $_POST['pod_albumom_photo_id'];

TADA! you've got an array now you can implode.



Dan
Aug 6 '09 #12
Alino
25
man thank you, you make my day :) it just works !!! :D:D
Aug 6 '09 #13
Alino
25
my next problem is creating link of this $pod_albumom_photo_id
I wanted it to implode to be able to make link like
implode($iframe_part1 . "http://www.mywebpage.com/x.php?action=100&uid=" .$user_id. "&iid=", $pod_albumom_photo_id);
but then i noticed that i am not able to make those links because i can't add
$iframe_part2
after array. How to solve this?
Aug 7 '09 #14
dlite922
1,584 Expert 1GB
I don't get your question. Why can't you add a variable?

It looks like you're doing complex string building, why not stick with the foreach?

What is your expected results? What are your inputs and what do you want output? Give sample data.

Thanks,




Dan
Aug 7 '09 #15
Alino
25
that form on my page creates new html page, and in that html should be iframes with urls and in those urls are values from $pod_albumom_photo_id

so i was trying to implode that array to create those iframes, but it didnt close each iframe correctly only the last.

i am using this to write to new html file
Expand|Select|Wrap|Line Numbers
  1. ...
  2. $handle = fopen("$filepath", 'w+');
  3.         if($handle)
  4. {
  5.  
  6.     $all_what_to_do =
  7.              "<html>
  8.              <head><title>new html file</title></head>
  9.              <body>
  10.  
  11.             </body>
  12.             </html>"
  13.             ;
  14.  
  15.             if(!fwrite($handle, "$all_what_to_do"))
  16.             die("couldn't write to file.");
  17. ...
it works pretty good but
i don't know what to put inside body tag, to create those iframes with url in which are those values from $pod_albumom_photo_id
Aug 7 '09 #16
Alino
25
I am still seeking for help
Aug 10 '09 #17
Alino
25
nah i don't need help anymore, i found myself stupid and now i know how to do that, thanks to all for helping me guys, you are all really cool.
Aug 10 '09 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Adrian Parker | last post: by
15 posts views Thread by zorro | last post: by
7 posts views Thread by Dynamo | last post: by
6 posts views Thread by comp.lang.php | last post: by
5 posts views Thread by comp.lang.php | last post: by
3 posts views Thread by scabman | last post: by
12 posts views Thread by Todd Michels | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.