guym Posted October 20, 2013 Share Posted October 20, 2013 Hello all, first I would like to say thank you to anyone that helps me out, I appreciate all the help I can get. I'm having issues on making a form that generates a new page with all of the information that has been put into the form.I've tried using file_put_contents but I don't know if that's the way to go, as the content variable would need to include php and html code. What do I need to do so that once a user clicks submit, a new page is generated from the data and the user is then taken to the newly created page? Here is all the code I have so far:addform.php <html> <head><title>Add Movie or Tv show</title></head> <body> <?php include_once("template_pageTop.php"); ?> <form enctype="multipart/form-data" id="myForm" action="$Movie_Name.php" method="post"> <table width="1233" height="582" border="0"> <tr bgcolor="#FFFFFF"> <th height="88" colspan="3" scope="col"><div align="left">Add Movie or TV Show to Database</div></th> </tr> <tr> <td width="317" height="477"><p> Movie Name:</p> <p> <input type="text" name="Movie_Name" /> <br /> </p> <p> <label> Poster<br> </label> <input type="FILE" name="Poster"> <br> </p> <p>Genre:</p> <p> <select multiple name="Genre[]" id="Genre"> <option value="All" style="text-indent: 0px;" selected="selected">All</option> <option value="Action" style="text-indent: 0px;">Action</option> <option value="Adventure" style="text-indent: 0px;">Adventure</option> <option value="Animation" style="text-indent: 0px;">Animation</option> <option value="Biography" style="text-indent: 0px;">Biography</option> <option value="Comedy" style="text-indent: 0px;">Comedy</option> <option value="Crime" style="text-indent: 0px;">Crime</option> <option value="Documentary" style="text-indent: 0px;">Documentary</option> <option value="Drama" style="text-indent: 0px;">Drama</option> <option value="Family" style="text-indent: 0px;">Family</option> <option value="Fantasy" style="text-indent: 0px;">Fantasy</option> <option value="Film-Noir" style="text-indent: 0px;">Film-Noir</option> <option value="History" style="text-indent: 0px;">History</option> <option value="Horror" style="text-indent: 0px;">Horror</option> <option value="Mystery" style="text-indent: 0px;">Mystery</option> <option value="Romance" style="text-indent: 0px;">Romance</option> <option value="Sci-Fi" style="text-indent: 0px;">Sci-Fi</option> <option value="Short" style="text-indent: 0px;">Short</option> <option value="Sport" style="text-indent: 0px;">Sport</option> <option value="Thriller" style="text-indent: 0px;">Thriller</option> <option value="War" style="text-indent: 0px;">War</option> <option value="Western" style="text-indent: 0px;">Western</option> </select> </p> <p> <label>IMDB Rating:</label> </p> <p> <select name="IMDB_Rating" id="IMDB_Rating"> <option value="0" style="text-indent: 0px;" selected="selected">All</option> <option value="9.9" style="text-indent: 0px;">10</option> <option value="9" style="text-indent: 0px;">9+</option> <option value="8" style="text-indent: 0px;">8+</option> <option value="7" style="text-indent: 0px;">7+</option> <option value="6" style="text-indent: 0px;">6+</option> <option value="5" style="text-indent: 0px;">5+</option> <option value="4" style="text-indent: 0px;">4+</option> <option value="3" style="text-indent: 0px;">3+</option> <option value="2" style="text-indent: 0px;">2+</option> <option value="1" style="text-indent: 0px;">1+</option> </select> </p> <p>Location: </p> <p> <input type="text" name="Location" value="file:///" /> <br /> <td width="900"><p> </p> <p>Quality: <label class="label">Quality:<br> </label> <select name="Quality" id="Quality"> <option value="All" style="text-indent: 0px;" selected="selected">All</option> <option value="1080p" style="text-indent: 0px;">1080p</option> <option value="3D" style="text-indent: 0px;">3D</option> <option value="480p" style="text-indent: 0px;">480p</option> <option value="720p" style="text-indent: 0px;">720p</option> <option value="DVD" style="text-indent: 0px;">DVD</option> <option value="HDRip" style="text-indent: 0px;">HDRip</option> </select> </p> <p> Year:</p> <p> <input type="text" name="Year" /> <br /> </p> <p>Trailer: </p> <p> <input type="text" name="Trailer" /> <br /> </p> <p>Synopsis:</p> <p> <textarea cols="50" rows="4" name="Synopsis"></textarea> </p></td> <td width="900"><img src="Reel.jpg" alt="" width="447" height="407" align="top"/></td> </tr> </table> <p> </p> <h1 align="left"> <button id="sub">Submit</button></h1></form> </body> </html> index.php <html> <head><title>Movies & TV Shows List/title></head> <body> <?php include_once("template_pageTop.php"); ?> <div align =right> <h3> <?php // Connection data (server_address, database, name, poassword) $hostdb = 'localhost'; $namedb = 'movies'; $userdb = 'root'; $passdb = 'spencer'; try { // Connect and create the PDO object $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); $conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8 } catch (PDOException $e) { $v_errormsg=$e->getMessage(); $error_str=<<<END_HTML <h2> Error connecting to database: Message: $v_errormsg <h2> END_HTML; echo $error_str; die; } $sql =<<<END_SQL SELECT Movie_Name, Poster, Genre, Quality, IMDB_Rating, Year, Synopsis FROM movieinfo; END_SQL; try { $sth = $conn->prepare($sql); $sth->execute(); } catch (PDOException $e) { $v_errormsg=$e->getMessage(); $error_str=<<<END_HTML <h2> Error selecting data: Message: $v_errormsg <h2> END_HTML; echo $error_str; die; } // ok output table $str_table_hdr=<<< END_HTML <h2> Film Listings </h2> <br> <h3><a href="addform.php">Add a film</a></h3> <br> <table id="film_table"> <thead> <th style="">ID</th> <th style="">Movie name</th> <th style="">Poster</th> <th style="">Genre</th> <th style="">Quality</th> <th style="">IMDB Rating</th> <th style="">Year</th> <th style="">Synopsis</th> <th style="width:43px;"> </th> </thead> <tbody> END_HTML; echo $str_table_hdr; $num_rows = 0; while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $movie_name = $row['Movie_Name']; $poster = $row['Poster']; $genre = $row['Genre']; $quality = $row['Quality']; $imdb_rating = $row['IMDB_Rating']; $year = $row['Year']; $synopsis = $row['Synopsis']; $str_table_row = <<<END_HTML <tr> <td></td> <td> $movie_name </td> <td> $poster </td> <td> $genre </td> <td> $quality </td> <td> $imdb_rating </td> <td> $year </td> <td> $synopsis </td> <td>   </td> </tr> END_HTML; echo $str_table_row; $num_rows += 1; } if ($num_rows == 0) { $str_table_row = <<<END_HTML <tr> <td colspan="100%" style="text-align:center;">No Data Found</td> </tr> END_HTML; echo $str_table_row; } $str_table_footer = <<<END_HTML </tbody> </table> END_HTML; echo $str_table_footer; ?> </body> </html> savetodb.php <?php include_once('db.php'); $Movie_Name = Trim(filter_var($_POST['Movie_Name'], FILTER_SANITIZE_STRING)); //Creat directory foler to hold movies files if (!file_exists("moviefolder/$Movie_Name")) { mkdir("moviefolder/$Movie_Name", 0755); } //upload and save poster $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["Poster"]["name"]); $extension = end($temp); if ((($_FILES["Poster"]["type"] == "image/gif") || ($_FILES["Poster"]["type"] == "image/jpeg") || ($_FILES["Poster"]["type"] == "image/jpg") || ($_FILES["Poster"]["type"] == "image/pjpeg") || ($_FILES["Poster"]["type"] == "image/x-png") || ($_FILES["Poster"]["type"] == "image/png")) && in_array($extension, $allowedExts)) { if ($_FILES["Poster"]["error"] > 0) { echo "Return Code: " . $_FILES["Poster"]["error"] . "<br>"; } else { if (file_exists("moviefolder/$Movie_Name/" . $_FILES["Poster"]["name"])) { } else { move_uploaded_file($_FILES["Poster"]["tmp_name"], "moviefolder/$Movie_Name/" . $_FILES["Poster"]["name"]); } } } else { echo "Invalid file"; } $Poster = mysql_real_escape_string($_FILES['Poster']['name']); if(isset($_POST['Genre'])) { $Genre = serialize($_POST['Genre']); } else { $Genre = ''; // this is in case user selects nothing (to be on the safe side) }; $IMDB_Rating = Trim(filter_var($_POST['IMDB_Rating'], FILTER_SANITIZE_STRING)); $Quality = Trim(filter_var($_POST['Quality'], FILTER_SANITIZE_STRING)); $Year = Trim(filter_var($_POST['Year'], FILTER_SANITIZE_STRING)); $Trailer = Trim(filter_var($_POST['Trailer'], FILTER_SANITIZE_STRING)); $Synopsis = Trim(filter_var($_POST['Synopsis'], FILTER_SANITIZE_STRING)); $Location = Trim(filter_var($_POST['Location'], FILTER_SANITIZE_STRING)); // the query $ins_sql = <<<END_SQL INSERT INTO movieinfo (ID, Movie_Name, Poster, Genre, IMDB_Rating, Quality, Year, Trailer, Synopsis, Location) VALUES ( 0, ?, ?, ?, ?, ?, ?, ?, ?, ? ) END_SQL; //$ins_field_array=array($Movie_Name, $Poster, $Genre, $IMDB_Rating, $Quality, $Year, $Trailer, $Synopsis, $Location); // //try { // $sth = $conn->prepare($ins_sql); // $sth->execute($ins_field_array); //} //catch (PDOException $e) { // $v_errormsg=$e->getMessage(); // $v_errorcode=$e->getCode(); // //$str_error = <<<END_HTML // <html> // <head> // </head> // <body> // <h1> Error inserting data. Code: $v_errorcode, Message: $v_errormsg </h1> // </body> // </html> //END_HTML; // echo $str_error; // die; //} // //$v_insert_id = $conn->lastInsertId(); // //$str_info = <<<END_HTML // <html> // <head> // </head> // <body> // <h1> Successfuly inserted movie with id = $v_insert_id </h1> // </bosy> // </html> //END_HTML; //echo $str_info; // //$sth->closeCursor(); $newpage="$Movie_Name.php"; $content =" "; file_put_contents($newpage, $content); header('location:http://localhost/movies/' .$newpage ); ?> as you can see my $content is empty because after trying to include html and php code inside the variable I came across many errors and just couldn't get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/283131-generate-new-page-with-form-data/ Share on other sites More sharing options...
Ch0cu3r Posted October 20, 2013 Share Posted October 20, 2013 You want to create a new php page for each movie that is added to the database? This is bad design. You should have one file that retrieves the movies data from the database and then displays it on the page. How you let the file knows what movie to retrieve is by passing in the url query string. For example site.com/movie.php?move=terminator Now in movie.php you'd have <?php // connect to database etc. mysql_connect('localhost', 'user'. 'pass'); mysql_select_db('movies'); // what movie is being requested if(isset($_GET['movie'])) { // the movie being requested $move = mysql_real_escape_string($_GET['movie']); // sanitize user input // query the database and get the requested movie $sql = "SELECT * from movies_table WHERE movie='$movie'"; if($result = mysql_query($sql)) { // did the query find the requested movie if(mysql_num_rows($result)) { // get the data from the result $row = mysql_fetch_assoc(); // display move template page include 'movie.template.php'; } } // movie was not found else { echo 'Sorry that movie cannot be found'; } } // user hasn't requested for a movie else { // either display movie index or an error here } ?> Now in movie.template.php you'd have your html layout, example coded for this <html> <head> <title>My Movie Site - <?php echo $row['movie']; ?></title> </head> <body> <h1><?php echo $row['movie']; ?></h1> <p><?php echo $row['description']; ?></p> <p>Actors: <?php echo $row['actors']; ?></p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/283131-generate-new-page-with-form-data/#findComment-1454687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.