Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Using array names in html forms

Featured Replies

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 by gigman7

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

  • 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.

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

 

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 by fisicx

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 by BrowserBugs

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 by Logic Design & Consultancy
improvement

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.