First form fills out second form on same page

Hello hello,

this is one of the final things I need to do for my site!

I currently have a page that allows users to edit movies, code here:

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/func.php';
$genrecheck = mysqli_query($link, 'SELECT genre_abbreviation, id FROM genres');
	if (!$genrecheck)
		{
			$error = 'Error fetching info from database.';
			include 'error.php';
			exit();
		}
	while ($row = mysqli_fetch_array($genrecheck))
		{
			$genres[] = array('genre_abbreviation' => $row['genre_abbreviation'], 'id' => $row['id']);
		}
$userid = mysqli_query($link, "SELECT id FROM members WHERE username = '$username'");
			$row = mysqli_fetch_assoc($userid);
				if (!$row)
					{
						$error = 'Error.';
						include 'error.php';
						exit();
					}
$userid = $row['id'];
$moviecheck = mysqli_query($link, "SELECT movieid, moviename FROM movies WHERE userid='$userid'");
	if (!$moviecheck)
		{
			$error = 'Error fetching info from database.';
			include 'error.php';
			exit();
		}
	while ($row = mysqli_fetch_array($moviecheck))
		{
			$movies[] = array('movieid' => $row['movieid'], 'moviename' => $row['moviename']);
		}
?>
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/func.php';
if(isset($_POST['editmovie_submit'])) {
$username = mysqli_real_escape_string($link, $_SESSION['username']);
$userid = mysqli_real_escape_string($link, $_SESSION['userid']);
$movieid = $_POST['movie'];
$genre = mysqli_real_escape_string($link, $_POST['genre']);
$region = mysqli_real_escape_string($link, $_POST['region']);
$status = mysqli_real_escape_string($link, $_POST['status']);
$wishlist = mysqli_real_escape_string($link, $_POST['wishlist']);
$date = html($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day']);

$editmovie = mysqli_query($link, "UPDATE movies SET genreid='$genre', movieregion='$region', moviestatus='$status', wishlist='$wishlist', wishlistdate='$date' WHERE movieid='$movieid'");
if (!$editmovie)
		{
			$error = 'Error.';
			include 'error.php';
			exit();
		}
		else
		header('Location: /movies.php?user=' . $username . '');
}
if(isset($_POST['editmovie_delete'])) {
$username = safe($link, $_SESSION['username']);
$movieid = $_POST['movie'];
$deletemovie = mysqli_query($link, "DELETE FROM movies WHERE movieid='$movieid'");
if (!$deletemovie)
		{
			$error = 'Error.';
			include 'error.php';
			exit();
		}
		else
		header('Location: /movies.php?user=' . $username . '');
}
?>
	<?php
if ($_GET['user'] != $_SESSION['username']) {
			$error = 'Cannot add to or edit other users collections.';
			include 'error.php';
			exit();
		}
?>

<form action="editmovie.php" method="post">
			
					<label for="moviename"><strong>Select Movie:</strong></Label><br/>
						<select name="movie" id="movies"/><br/>
								<?php foreach ($movies as $movie): ?>
								<option value="<?php echo $movie["movieid"]; ?>" name="movie"><?php echo $movie["moviename"]; ?></option>
								<?php endforeach; ?>
						</select><br/>
				</div>
			
					<strong>Region</strong><br/>
						<input type="radio" name="region" id="regionUS" value="US" CHECKED /><label for="regionUS"> US</label>
						<input type="radio" name="region" id="regionJP" value="JP"/><label for="regionJP"> JP</label>
						<input type="radio" name="region" id="regionAU" value="AU"/><label for="regionAU"> AUSTRALIA</label>
				
					<label for="genre"><strong>Genre</strong></Label>
					<br/>
					<select name="genre" id="genres">
						<?php foreach ($genres as $genre): ?>
						<option value="<?php htmlout($genre['id']); ?>" name="genre"><?php htmlout($genre['genre_abbreviation']); ?></option>
						<?php endforeach; ?>
					</select>
				
					<label for="status"><strong>Status</strong></label><br/>
						<input type="radio" name="status" value="UNWATCHED" CHECKED /> Unwatched
						<input type="radio" name="status" value="WATCHED"/> Watched
				
				<label for="wishlist"><strong>Wishlist?</strong></label><br/>
					<input type="checkbox" name="wishlist" value="1"/>
				
					<label for="date"><strong>Release Date (YYYY-MM-DD) (LEAVE BLANK IF MOVIE IS RELEASED):</strong></Label><br/>
							<label for="year"><strong>Year:</strong></Label>
							<label for="month"><strong>Month:</strong></Label>
							<label for="day"><strong>Day:</strong></Label>
							<input type="text" name="year" maxlength="4"/>
							<input type="text" name="month" maxlength="2"/>
							<input type="text" name="day" maxlength="2"/>
					
			
				<input type="submit" name="editmovie_submit" value="Update Movie"/>
				<input type="submit" name="editmovie_delete" value="Remove Movie"/>

but I am wondering if it possible for me to have only the ‘SELECT MOVIE’ option set come up, this part:

<label for="moviename"><strong>Select Movie:</strong></Label><br/>
<select name="movie" id="movies"/><br/>
<?php foreach ($movies as $movie): ?>
<option value="<?php echo $movie["movieid"]; ?>" name="movie"><?php echo $movie["moviename"]; ?></option>
<?php endforeach; ?>
</select><br/>

with a ‘select movie’ button added, and when that button is pressed, the page is reloaded and the rest of the form options appear and are filled out with the information from the selected movie in the ‘select movie’ form.

My question is, is this possible, and can I do it with just some changes to my current page or will I have to have another page to include or something?

Thanks for any tips!

Sure it’s possible.
IF(ISSET()) will be your friend.


if(isset($_POST['moviename'])) {
 //The User has selected a movie. Do whatever you want to do about that movie here.
} else {
  //the User has not selected a movie. Probably a good place to put your 'Select Movie' form.
}

Ok cool thank you, just wanted to make sure before I started attempting this… I actually think I can work this one out :D… if not I will post further questions.

Thanks!

ok I’m going pretty well considering my limited skill (definitely learning to be able to do a lot more myself thanks to this site)

I’ve handled it by replacing both forms with issets and if selectmovie_submit isset I do:


if (isset($_POST['selectmovie_submit'])) {
$movieid = $_POST['movie'];
$selectmovie = mysqli_query($link, "SELECT * FROM movies WHERE movieid='$movieid'");
$row = mysqli_fetch_assoc($selectmovie);
				if (!$row)
					{
						$error = 'Error.';
						include 'error.php';
						exit();
					}
$username = safe($link, $_SESSION['username']);
$userid = safe($link, $_SESSION['userid']);
$moviename = $row['moviename'];
$genreid = $row['movieid'];
$region =  $row['movieregion'];
$status =  $row['moviestatus'];
$wishlist =  $row['wishlist'];
$wishlistdate =  $row['wishlistdate'];

$genre = mysqli_query($link, "SELECT genre_abbreviation FROM genres WHERE id='$genreid'");
				$row = mysqli_fetch_assoc($genre);
				if (!$row)
					{
						$error = 'Error.';
						include 'error.php';
						exit();
					}
$genre_abbreviation = $row['genre_abbreviation'];

so I have all my variables set. still working on getting the DATE variable set too, need it as $year, $month, $day… but it is set as DATE in the database. I know there’s a way to prune each one from DATE but gotta figure that out. also know I can get genre_abbreviation by modifying the $selectmovie query, but need to figure that out too.

what I can’t figure out is how to output the correct results in all of the parts of the second form. mainly the following types of fields.


<strong>Region</strong><br/>
<input type="radio" name="region" id="regionUS" value="US" CHECKED /><label for="regionUS"> US</label>
<input type="radio" name="region" id="regionJP" value="JP"/><label for="regionJP"> JP</label>
<input type="radio" name="region" id="regionAU" value="AU"/><label for="regionAU"> AUSTRALIA</label>
				
			
<label for="status"><strong>Status</strong></label><br/>
<input type="radio" name="status" value="UNWATCHED" CHECKED /> Unwatched
<input type="radio" name="status" value="WATCHED"/> Watched
				
<label for="wishlist"><strong>Wishlist?</strong></label><br/>
<input type="checkbox" name="wishlist" value="1"/>


I worked out how to do it for the <select> parts by just donig

<option value="0"><?php echo $variable; ?></option>

I don’t even know where to begin with the other form fields, but I’m quite happy I figured out as much as I did! heh

Okay. Deep breaths time :wink:

From what it looks like you’re asking for, you’re trying to populate the fields with data from the database?

Assuming this is so, consider this:


$regions = array(array("US","UNITED STATES"),array("JP","JAPAN"),array("AU","AUSTRALIA"));
//Note: This can be populated by a database query as well. I'm just throwing it in so that I know my example's structure.
foreach($regions AS $possregion) {
  //Possregion is now a 2-index array - the first time through, for example, $possregion[0] = "US", and $possregion[1] = "UNITED STATES"
  echo '<input type="radio" name="region" id="region'.$possregion[0].'" value="'.$possregion[0].'"';
  //Now, is it to be checked?
  if($region == $possregion[0]) {
    echo " checked ";
  }
  //And finish it off...
  echo ' /><label for="region'.$possregion[0].'">'.$possregion[1].'</label>';
} // Now, go back and do it for the other values of $regions. (FOREACH)

hah, deep breaths indeed! and yes, trying to populate the fields of the second form with the info of the movie selected in the first form.

thank you very much for that - I will work on it and report success and/or failure. :smiley:

appreciate it. :slight_smile:

Well I actually got it working on region and status, woo! you are awesome.

It also made me realize that the way I chose to do the genre was broken, but I fixed that and it works now, thanks to your skills. :slight_smile:

I’m not sure how to go about doing the wishlist section though,


<label for="wishlist"><strong>Wishlist</strong></label><br/>
<input type="checkbox" name="wishlist" value="1"/>

Do I use the same method, just making a smaller array?

I thought I felt dumb for asking that because, hello, it’s a pretty easy solution:


<label for="wishlist"><strong>Wishlist</strong></label><br/>
<input type="checkbox" name="wishlist" value="<?php echo $wishlist; ?>"/>

but it’s not working. will continue playing, for some reason I believe that should be working? heh

oh of course that won’t work portem, value doesn’t set whether or not the box is checked! trying another approach. :wink:

solved it:


if ($row['wishlist'] == 1) { $wishlist = '<input type="checkbox" name="wishlist" value="1" checked/>'; }
if ($row['wishlist'] == 0) { $wishlist = '<input type="checkbox" name="wishlist" value="1"/>'; }

//in the form
<?php echo $wishlist; ?>

or probably better


if ($row['wishlist'] == 1) { $wishlist = ' checked'; }
if ($row['wishlist'] == 0) { $wishlist = ''; }

//in the form
<input type="checkbox" name="wishlist" value="1"<?php echo $wishlist; ?>/>

ahh it’s fun being a clueless newbie. hah. very fist-pump-satisfying even when you work out something so ‘duh’ :slight_smile: