SitePoint Sponsor |
|
User Tag List
Results 1 to 20 of 20
Thread: Taking 3 pages into one page
-
Aug 23, 2005, 08:13 #1
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Taking 3 pages into one page
I have three pages in PHP that I would like to make into one. The first page takes the users info and submits it to the second page. The Second page takes the users info and displays it for their conformation. They user accepts the info and the info is sent to the third page which adds it to the MySQL database. I know its not the cleanest code you have ever seen.
Below is the code from the pages:
addmain.php
PHP Code:<?php
//check for admin session
//if no admin session, login
session_start();
if (!(isset($_SESSION['id']))) {
header ("Location:login.php");
}
?>
<!-- End check user login status-->
<?php
require ('includes/header.inc.php');
?>
<?php
require ('includes/nav.inc.php');
$usersubmit = $_REQUEST['usersubmit'];
if ($usersubmit == 1) {
$heading = $_POST['heading'];
$content = $_POST['content'];
$enter = 1;
}else{
// Get values from session variables
session_start();
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
}
if ($enter == 1) {
$newPage = "addmainconfirm.php";
$newPage .= "?heading=$heading";
$newPage .= "&content=$content";
echo "<script type\"text/javascript\">document.location = \"$newPage\"</script>";
}
?>
<div id="adminContent">
<form action="addmain.php?usersubmit=1" method="post">
<h3>Heading</h3>
<input name="heading" type="text" size="30" maxlength="100" />
<h3>Content</h3>
<textarea name="content" cols="60" rows="20"></textarea>
<input name="submit" type="submit" value="Add Main Topic" />
</form>
</div>
<?php
require ('includes/footer.inc.php');
?>
PHP Code:<?php
//check for admin session
//if no admin session, login
session_start();
if (!(isset($_SESSION['id']))) {
header ("Location:login.php");
}
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
// Set local vars to the query string info
$heading = $_REQUEST['heading'];
$content = $_REQUEST['content'];
// Record user info in sessions
session_start();
$_SESSION['s_heading'] = $heading;
$_SESSION['s_content'] = $content;
?>
<div id="adminContent">
<?php
print "<h2>Confirmation</h2>";
print "<p>This is the information you have submitted</p>";
$addMainConfirm = "<p>Heading: $heading <br>\n";
$addMainConfirm .= "Content: $content <br>\n";
print $addMainConfirm;
print "<p><a href=\"addmain.php\">Make changes to the content</a><br />";
print "<a href=\"addmaindb.php\">Add this content now</a></p>";
?>
</div>
<?php
require ('includes/footer.inc.php');
?>
PHP Code:<?php
//check for admin session
//if no admin session, login
session_start();
if (!(isset($_SESSION['id']))) {
header ("Location:login.php");
}
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
// Set local vars to the query string info
$heading = mysql_real_escape_string($_REQUEST['heading']);
$content = mysql_real_escape_string($_REQUEST['content']);
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
?>
<div id="adminContent">
<?php
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) values ('$heading', '$content')";
if (!($res = mysql_query($sql))) {
echo mysql_error();
}
?>
</div>
<?php
require ('includes/footer.inc.php');
?>
Thanks for your help in advance.
If you need a live link to see how it works, let me know.
-
Aug 23, 2005, 08:53 #2
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Untested, but try this, call the script addmain.php
PHP Code:<?php
//check for admin session
//if no admin session, login
session_start();
if (!(isset($_SESSION['id']))) {
header ("Location:login.php");
}
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
if (isset('db')):
$heading = mysql_real_escape_string($_REQUEST['heading']);
$content = mysql_real_escape_string($_REQUEST['content']);
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
echo '<div id="adminContent">';
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) values ('$heading', '$content')";
if (!($res = mysql_query($sql))) {
echo mysql_error();
}
echo '</div>';
require ('includes/footer.inc.php');
endif;
$usersubmit = $_REQUEST['usersubmit'];
if ($usersubmit == 1) {
$heading = $_POST['heading'];
$content = $_POST['content'];
$enter = 1;
}else{
// Get values from session variables
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
}
if ($enter == 1) {
// Record user info in sessions
$_SESSION['s_heading'] = $heading;
$_SESSION['s_content'] = $content;
echo '<div id="adminContent">';
print "<h2>Confirmation</h2>";
print "<p>This is the information you have submitted</p>";
$addMainConfirm = "<p>Heading: $heading <br>\n";
$addMainConfirm .= "Content: $content <br>\n";
print $addMainConfirm;
print "<p><a href=\"addmain.php\">Make changes to the content</a><br />";
print "<a href=\"addmain.php?db\">Add this content now</a></p>";
echo '</div>';
exit;
}
?>
<div id="adminContent">
<form action="addmain.php?usersubmit=1" method="post">
<h3>Heading</h3>
<input name="heading" type="text" size="30" maxlength="100" />
<h3>Content</h3>
<textarea name="content" cols="60" rows="20"></textarea>
<input name="submit" type="submit" value="Add Main Topic" />
</form>
</div>
<?php
require ('includes/footer.inc.php');
?>A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 09:00 #3
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
All I got was a blank page. Thanks for your help though
As far as I can see, there is no syntax errors. Any others out there can lend a hand? Thanks again!
-
Aug 23, 2005, 09:04 #4
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Did the 3 scripts work together when you gave them to me ?
Have you changed all references to all 3 scripts to addmain.php ?A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 09:10 #5
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
OK try that found 1 mistake in your original files, and one of mine.
PHP Code:<?php
// ************admain.php***********
//check for admin session
//if no admin session, login
session_start();
if (!isset($_SESSION['id'])) {
header ("Location:login.php");
}
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
if (isset($_GET['db'])):
$heading = mysql_real_escape_string($_REQUEST['heading']);
$content = mysql_real_escape_string($_REQUEST['content']);
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
echo '<div id="adminContent">';
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) values ('$heading', '$content')";
if (!($res = mysql_query($sql))) {
echo mysql_error();
}
echo '</div>';
require ('includes/footer.inc.php');
endif;
$usersubmit = $_REQUEST['usersubmit'];
if ($usersubmit == 1) {
$heading = $_POST['heading'];
$content = $_POST['content'];
$enter = 1;
}else{
// Get values from session variables
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
}
if ($enter == 1) {
// Record user info in sessions
session_start();
$_SESSION['s_heading'] = $heading;
$_SESSION['s_content'] = $content;
echo '<div id="adminContent">';
print "<h2>Confirmation</h2>";
print "<p>This is the information you have submitted</p>";
$addMainConfirm = "<p>Heading: $heading <br>\n";
$addMainConfirm .= "Content: $content <br>\n";
print $addMainConfirm;
print "<p><a href=\"addmain.php\">Make changes to the content</a><br />";
print "<a href=\"addmain.php?db\">Add this content now</a></p>";
echo '</div>';
}
?>
<div id="adminContent">
<form action="addmain.php?usersubmit=1" method="post">
<h3>Heading</h3>
<input name="heading" type="text" size="30" maxlength="100" />
<h3>Content</h3>
<textarea name="content" cols="60" rows="20"></textarea>
<input name="submit" type="submit" value="Add Main Topic" />
</form>
</div>
<?php
require ('includes/footer.inc.php');
?>A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 09:18 #6
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I dont think I asked the correct question.
I want to take those 3 pages i have(The already work 100% to add to the database) and turn it into one page that the actions of the forms will submit to itself.
-
Aug 23, 2005, 09:26 #7
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by sputza
It works here, but not having your database I can't test the posting bit !!A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 10:09 #8
- Join Date
- Aug 2004
- Location
- Chicago
- Posts
- 296
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ala mostly tested:
PHP Code:<?php
// Start the session for everything
// Only 1 call is needed up here
session_start();
// Check the login
if (!isset($_SESSION['id'])) {
header('Location: login.php');
exit;
}
echo '<!-- End check user login status-->'."\r\n";
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
// Get the submit var from either the post or get (I have problems with $_REQUEST)
$submit = (int) (isset($_POST['submit']) ? $_POST['submit'] :
(isset($_GET['submit']) ? $_GET['submit'] : 0));
if ($submit == 0) { // This is the original form
// Account for changing the content
if (isset($_GET['change']) && $_GET['change'] == 1) {
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
} else {
$heading = '';
$content = '';
} ?>
<div id="adminContent">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" id="submit" name="usersubmit" value="1" />
<h3>Heading</h3>
<input name="heading" type="text" size="30" maxlength="100" value="<?php echo $heading; ?>" />
<h3>Content</h3>
<textarea name="content" cols="60" rows="20"><?php echo $content; ?></textarea>
<input name="submit" type="submit" value="Add Main Topic" />
</form>
</div>
<?php } elseif ($submit == 1) { // This is the confirmation page
// Set the vars, and put them in the session
$heading = $_POST['heading'];
$content = $_POST['content'];
$_SESSION['s_heading'] = $heading;
$_SESSION['s_content'] = $content;
?>
<div id="adminContent">
<h2>Confirmation</h2><p>This is the information you have submitted</p>
<p>Heading: <?php echo $heading; ?><br />
Content <?php echo $content; ?><br />
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?submit=0&change=1">Make changes to the content</a><br />
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?submit=2">Add this content now</a></p>
<?php } elseif ($submit == 2) { // Add to database
// Escape the strings
$heading = mysql_real_escape_string($_SESSION['s_heading']);
$content = mysql_real_escape_string($_SESSION['s_content']);
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) VALUES ('".$heading."', '".$content."');";
if (!$res = mysql_query($sql)) {
$result = 'MySQL Error: '; $result .= mysql_error();
} else {
$result = 'Added Content';
} ?>
<div id="adminContent">
<?php echo $result; ?>
</div>
<?php }
require('includes/footer.inc.php');
// And done ;)
?>Why's (Poignant) Guide to Ruby
learn ruby with foxes, wizards, and chunky bacon
-
Aug 23, 2005, 12:09 #9
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No luck. Was I supposed to change anything in that code?
Your code looks great but I can’t seem to find the problem. What happens when the user clicks submit to take them to the confirm section, it just refreshes the page without showing the data they sent.
Can you find the problem in the script? Thanks in advance!
-
Aug 23, 2005, 12:13 #10
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Are you refering to my code or bjcffnets ?
A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 12:16 #11
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Mandes
-
Aug 23, 2005, 13:00 #12
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
No problem, still don't see the difference between the two codings, but, no problem.
A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 13:26 #13
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
mandes,
I am open to your method. As far as I can see they work almost the same.
There must be something wrong with the code. When I use it, it never gets to the submit=1 part. Here is what happens:
User fills out addmain.php and hits submit.
taken to addmain.php?usersubmit=1 but forwarded to the old forward page.
Im going to restart the server and see if that fixes the problem. Its looks like a chache problem to me. Something to do with the javascript redirect I was using I think. I will let you know how it goes. Thanks again!
-
Aug 23, 2005, 13:32 #14
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by sputza
PHP Code:<?php
// ************admain.php***********
//check for admin session
//if no admin session, login
session_start();
if (!isset($_SESSION['id'])) {
header ("Location:login.php");
}
require ('includes/header.inc.php');
require ('includes/nav.inc.php');
if (isset($_GET['db'])):
$heading = mysql_real_escape_string($_REQUEST['heading']);
$content = mysql_real_escape_string($_REQUEST['content']);
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
echo '<div id="adminContent">';
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) values ('$heading', '$content')";
if (!($res = mysql_query($sql))) {
echo mysql_error();
}
echo '</div>';
require ('includes/footer.inc.php');
endif;
$usersubmit = $_REQUEST['usersubmit'];
if ($usersubmit == 1) {
$heading = $_POST['heading'];
$content = $_POST['content'];
$enter = 1;
}else{
// Get values from session variables
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
}
if ($enter == 1) {
// Record user info in sessions
session_start();
$_SESSION['s_heading'] = $heading;
$_SESSION['s_content'] = $content;
echo '<div id="adminContent">';
print "<h2>Confirmation</h2>";
print "<p>This is the information you have submitted</p>";
$addMainConfirm = "<p>Heading: $heading <br>\n";
$addMainConfirm .= "Content: $content <br>\n";
print $addMainConfirm;
print '<p><a href="' . $_SERVER['PHP_SELF'] . '">Make changes to the content</a><br />';
print '<a href="' . $_SERVER['PHP_SELF'] . '?db">Add this content now</a></p>';
echo '</div>';
exit;
}
echo '<div id="adminContent">';
echo '<form action="' . $_SERVER['PHP_SELF'] . '?usersubmit=1" method="post">';
echo '<h3>Heading</h3>';
echo '<input name="heading" type="text" size="30" maxlength="100" />';
echo ' <h3>Content</h3>';
echo '<textarea name="content" cols="60" rows="20"></textarea>';
echo '<input name="submit" type="submit" value="Add Main Topic" />';
echo '</form>';
echo '</div>';
require ('includes/footer.inc.php');
?>
but obviously falls when it trys to input to the databaseA Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Aug 23, 2005, 13:50 #15
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Mandes. There was some sort of caching problem. After I reset the server it works 100%! Im sorry I didn't try the reset sooner. Thanks again.
-
Aug 23, 2005, 15:02 #16
- Join Date
- Aug 2004
- Location
- Chicago
- Posts
- 296
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorry. I changed usersubmit to submit x-(
Why's (Poignant) Guide to Ruby
learn ruby with foxes, wizards, and chunky bacon
-
Aug 23, 2005, 15:03 #17
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bjcffnet
-
Aug 24, 2005, 17:16 #18
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The only thing I had to add was a break. Thanks again guys!
PHP Code:if (isset($_GET['db'])):
$heading = mysql_real_escape_string($_REQUEST['heading']);
$content = mysql_real_escape_string($_REQUEST['content']);
$heading = $_SESSION['s_heading'];
$content = $_SESSION['s_content'];
echo '<div id="adminContent">';
require ('../includes/dbconnect.inc.php');
$sql = "INSERT INTO main (heading, content) values ('$heading', '$content')";
if (!($res = mysql_query($sql))) {
echo mysql_error();
}
echo '</div>';
require ('includes/footer.inc.php');
break;
endif;
-
Aug 24, 2005, 17:22 #19
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
One question i have is; how can I stop the information from being resent to the database after the user has already added it. Currently, if the user hits refresh after they have submitted it will add a double post to the database.
I cant kill the session because that will make them re-login. Any suggestions?
-
Aug 25, 2005, 07:29 #20
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Moved the new question to its own thread:
http://www.sitepoint.com/forums/show....php?p=2124056Last edited by sputza; Aug 25, 2005 at 08:14.
Bookmarks