May 1, 20197 yr I have tried everything I can think of to use to get array names in a form name. Either I am using the php code wrong to execute them or how I'm putting them in just isn't right. I'll give an example. I pick out the songs each week at church for service. I keep all of the songs we sing in a database and I change the date we sing them. If I pick out 6 songs and change the date, I have to do it one at a time with over 100 forms and submit buttons. I'd like each song to have its own if where I have something like $date[1], $date[2] etc. Then when I submit it, it will change all with one submit. I have a different page that has many more pieces of data that I have set to 100 per page. Therefore, each will have a different id attached to it so I use a counter $x. When I try to set this up as name = "$name[$x]" it doesn't work. I am using the escape with the "". I have changed it around so much that I'm probably repeating myself now. How do I set this up? What I currently have looks something like this. echo "Title<input type=\"text\" name=\"shortDesc[$x]\" size=\"40\">\n"; I've tried several ways to get the submitted data. Here's one of many. $shortDesc = $_POST['shortDesc']['$x']; echo $shortDesc Edited May 1, 20197 yr by gigman7
May 1, 20197 yr That's never going to work. Try this: echo 'Title<input type="text" name="shortDesc'.$x.'" size="40">'."\n"; And then: $shortDesc = $_POST['shortDesc36']; echo $shortDesc Where 36 is the value of $x However a lot depends on the loop you are using for $x and how you subsequently process $_POST
May 1, 20197 yr Author 37 minutes ago, fisicx said: That's never going to work. Try this: echo 'Title<input type="text" name="shortDesc'.$x.'" size="40">'."\n"; And then: $shortDesc = $_POST['shortDesc36']; echo $shortDesc Where 36 is the value of $x However a lot depends on the loop you are using for $x and how you subsequently process $_POST I don't see how that is going to work since it's in a loop. I should have put that "36" as $x. When it's in the loop, it's got to be set in some sort of array.
May 1, 20197 yr This may help: <form method="post"> <input type="text" name="name[]" value="ricky at 0" /><br /> <input type="text" name="name[99]" value="ricky at 99" /><br /> <button type="submit">post</button><br /> </form> <?php if ( sizeof($_POST) ) { ?><pre><? var_dump($_POST); foreach ( $_POST["name"] as $index => $value ) { print("name at index '$index' has value '$value': {$_POST["name"][$index]}\n"); } print("And here I just use it: {$_POST["name"][99]}\n"); ?></pre><? } print("And here's the source: <pre>".htmlspecialchars(file_get_contents(__FILE__))."</pre>"); # end
May 1, 20197 yr 4 hours ago, gigman7 said: I don't see how that is going to work since it's in a loop. I should have put that "36" as $x. When it's in the loop, it's got to be set in some sort of array. No it doesn’t. The input names can be strings which you then process however you want. Edited May 1, 20197 yr by fisicx
May 2, 20197 yr 19 hours ago, gigman7 said: I pick out the songs each week at church for service. I keep all of the songs we sing in a database and I change the date we sing them. If I pick out 6 songs and change the date, I have to do it one at a time with over 100 forms and submit buttons. I'd like each song to have its own if where I have something like $date[1], $date[2] etc. Then when I submit it, it will change all with one submit. Trying to wrap my head around this. You have a database table of songs? Although an array, something like ... <?php $songs = array( array('id' => 1, 'name' => "Uptown Top Ranking", 'artist' => "Althea and Donna"), array('id' => 2, 'name' => "Hits from the Bong", 'artist' => "Cypruss Hill"), array('id' => 3, 'name' => "Trigger Hippie", 'artist' => "Morcheeba"), array('id' => 4, 'name' => "When You Were Young", 'artist' => "The Killers"), array('id' => 5, 'name' => "When Doves Cry", 'artist' => "Prince") ); ?> ... and you want to select songs to add to a playlist for a given date, e.g. 02/05/2019. If so maybe use simply a tick list? <?php $selected_songs = array(); $playdate = "2019-05-02"; if($_SERVER['REQUEST_METHOD']=='POST'){ $playdate = $_POST['play-date']; foreach($_POST['select_tracks'] as $key=>$val) { $selected_songs[] = $val; } } ?> <form method="post"> <input type="date" name="play-date" value="<?php echo $playdate; ?>"> <ul> <?php foreach($songs as $s) { ?> <li><input name="select_tracks[]" type="checkbox" value="<?php echo $s['id']; ?>"<?php if(in_array($s['id'], $selected_songs)) { ?> checked="checked"<?php } ?>>"<?php echo $s['name']; ?>" by <?php echo $s['artist']; ?></li> <?php } ?> </ul> <input name="post_choons" type="submit"> </form> ... this will make $selected_songs a list of the chosen track ids and the $playdate for which day. Not sure how or where you want to store this. Edited May 2, 20197 yr by BrowserBugs
May 8, 20197 yr Hi gigman7! I think your only error is that the line: $shortDesc = $_POST['shortDesc']['$x']; Should be (without the single quotes on $x); $shortDesc = isset($_POST['shortDesc'][$x]) ? $_POST['shortDesc'][$x] : NULL; Im assuming your looping though the fields and $x is the database row id? BrowserBugs full script works well you might be best of taking bits from that Edited May 8, 20197 yr by Logic Design & Consultancy improvement
Create an account or sign in to comment