Yeah goot point Jamie, of you need to return a form variable you would need $_GET or $_REQUEST and if you are evaluating the submit option (which would take the form action of POST) then you could use $_POST
Yeah goot point Jamie, of you need to return a form variable you would need $_GET or $_REQUEST and if you are evaluating the submit option (which would take the form action of POST) then you could use $_POST
nvening (02-08-2007)
yep, it displayed that so it must be being submitted properly?
EDIT:
This is what i have atm:
no errors but nothing is added to the databasePHP Code:
<?php
//Include a file include.php which is above the current directory (so can't be accessed directly from a browser = more security)
require_once('include.php');
if(isset($_REQUEST['posted'])) {
echo 'this is displayed because the form has been submitted';
//Put ID in URL into $id variable
$id = $_REQUEST['id'];
$name_rev=$_POST['name'];
$email_rev=$_POST['email'];
$rating_rev=$_POST['rating'];
$review_rev=$_POST['review'];
//Check if the form was filled out correctly...if not print an error and change $error variable to true
$error = false;
if (empty($name_rev)){echo 'You forgot to enter your name'; $error = true;}
if (empty($email_rev)){echo 'You forgot to enter your email'; $error = true;}//remove this line if the email isn't necessary
if (empty($review_rev)){echo 'You forgot to write a review'; $error = true;}
//Only continue if the form was filled out correctly
if ($error = false){
//Do some basic handling of submitted stuff:
//Make sure the rating is a number
$rating_rev=doubleval($rating_rev);
//Add slashes to the text to prevent problems occuring [remember to use stripslashes(...) when printing the review]
$email_rev=addslashes(email_rev);
$review_rev=addslashes(review_rev);
$name_rev=addslashes(name_rev);
//Open database connection by calling function in include.php
$dbh=db_connect();
$sql = "INSERT INTO restreview VALUES
('$id','$name_rev','$email_rev','$rating_rev','$review_rev')";
print($sql);
mysql_query($sql) or die ('I cannot update the database because: ' . mysql_error());
mysql_close();
}//end if ($error = false){...
}
?>
<h1>Your Details:</h1>
<form action="resturant.php?id=<? echo $id; ?>" method="post">
<span class="det">Name:</span> <input style="margin-left: 5px;" type="text" name="name"><br/>
<span class="det">Email:</span> <input style="margin-left: 7px;" type="text" name="email"> <span class="det">(will not be displayed)</span><br/>
<span class="det">Rating:</span> <select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br/>
<h1>Review:</h1>
<textarea rows="5" cols="50" name="review">
Enter your review here
</textarea>
<input type="hidden" name="validation" value="1"/>
<p style="margin-left: 50px;" >
<input type="submit" name="posted" value="Submit" >
<input type="reset" value="Reset">
</p>
</form>
Last edited by nvening; 02-08-2007 at 05:58 PM.
(\__/)
(='.'=)
(")_(")
Ahhh, I've spotted a problem:
Change
toPHP Code:
if ($error = false){
PHP Code:
if ($error == false){
nvening (02-08-2007)
ah, top man, now we have "INSERT INTO restreview VALUES ('1','name_rev','email_rev','3','review_rev')I cannot update the database because: Column count doesn't match value count at row 1" XD - hmmmmmm
(\__/)
(='.'=)
(")_(")
Try changing it to read this assuming that the columns in your mysql database are named id, name, email, rating and review.
PHP Code:
INSERT INTO restreview (id, name, email, rating, review) VALUES ('$id','$name_rev','$email_rev','$rating_rev','$review_rev');
nvening (02-08-2007)
Great, after some tweaking and some extra code ive got it working!
heres the code:
thanks for the help guys!!PHP Code:
<?php
//Include a file include.php which is above the current directory (so can't be accessed directly from a browser = more security)
require_once('include.php');
if(isset($_REQUEST['posted'])) {
//Put ID in URL into $id variable
$id = $_REQUEST['id'];
$name_rev=$_POST['name'];
$email_rev=$_POST['email'];
$rating_rev=$_POST['rating'];
$review_rev=$_POST['review'];
//Check if the form was filled out correctly...if not print an error and change $error variable to true
$error = false;
if (empty($name_rev)){echo '<h2 style="color: red; ">ERROR: You forgot to enter your name</h2>'; $error = true;}
if (empty($email_rev)){echo '<h2 style="color: red; ">ERROR: You forgot to enter your email</h2>'; $error = true;}//remove this line if the email isn't necessary
if (empty($review_rev)){echo '<h2 style="color: red; ">ERROR: You have not entered your review!</h2>'; $error = true;}
if (!$email_rev == "" && (!strstr($email_rev,"@") || !strstr($email_rev,"."))){echo '<h2 style="color: red; ">ERROR: Your email address is not valid</h2>'; $error = true;}
//Only continue if the form was filled out correctly
if ($error == false){
//Do some basic handling of submitted stuff:
//Make sure the rating is a number
$rating_rev=doubleval($rating_rev);
//Add slashes to the text to prevent problems occuring [remember to use stripslashes(...) when printing the review]
$email_rev=addslashes($email_rev);
$review_rev=addslashes($review_rev);
$name_rev=addslashes($name_rev);
//Open database connection by calling function in include.php
$dbh=db_connect();
$sql = "INSERT INTO restreview (id, name, email, rating, review) VALUES ('$id','$name_rev','$email_rev','$rating_rev','$review_rev')";
mysql_query($sql) or die ('I cannot update the database because: ' . mysql_error());
$conf = '<h2>Thank you, your review has been submitted and will be displayed shortly</h2>';
mysql_close();
}//end if ($error = false){...
}
?>
<h1>Your Details:</h1>
<form action="resturant.php?id=<? echo $id; ?>" method="post">
<span class="det">Name:</span> <input style="margin-left: 5px;" type="text" name="name"><br/>
<span class="det">Email:</span> <input style="margin-left: 7px;" type="text" name="email"> <span class="det">(will not be displayed)</span><br/>
<span class="det">Rating:</span> <select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br/>
<h1>Review:</h1>
<textarea rows="5" cols="50" name="review">
Enter your review here
</textarea>
<input type="hidden" name="validation" value="1"/>
<p style="margin-left: 50px;" >
<h1>Please ensure your detials are corrent and check through your review before submission</h1>
<input type="submit" name="posted" value="Submit" >
<input type="reset" value="Reset">
</p>
</form>
<? echo $conf; ?>
(\__/)
(='.'=)
(")_(")
Ok, the database remembers line breaks however how do i get them to be displayed on the website??
Thanks
(\__/)
(='.'=)
(")_(")
$output_string = nl2br($output_string);
The above function nl2br() will convert the line breaks to <br> html tags.
aha, well, thats absolutely fantastic, i think ive finished the system!!
Thanks so much to everyone who has helped once again.
Now, tomorrow, search XD
Hopefully i should be able to find some code to adapt though as i imagine its a pretty common use!!
(\__/)
(='.'=)
(")_(")
Ok, so far the search script is working well, however there are a couple of things which i would like to add on which im not sure how to do.
1) two checkboxes which will narrow the results down by whether they are ticked or not, producing a yes if ticked which will only select the rows which have a yes for the option. ie Takeaway *checkbox* - if ticked it will give it a yes value and so only the resturants which offer takeaway will be displayed.
2) how to modify it so when you press search you are taken to another page where the results are displayed, so that i can include the search box in other areas of the site.
Last edited by nvening; 02-05-2011 at 01:52 PM.
(\__/)
(='.'=)
(")_(")
To submit to a different page you will need to change your 'action' command within the form concerned to the page you wish to send the user to.
e.g:
<form name="search" method="post" action="index.php?page=search_results">
(this assumes you are taking a variable called 'page' from the query string to determine which page to display (ask for details))
The page you send the user to will have to call in the posted variables using $_GET or $_REQUEST as you would do if the script posted to the same page.
The other piece is more complex...It's a friday afternoon....
details?
lol
and dw about the other thing, i can carry on and add it later
(\__/)
(='.'=)
(")_(")
What do you mean by details?!
in the short term, you can just use something like action="search_results.php"
Assuming the page you want to send people to is called search_results.php
The logic behind using a variable to decide which page to display is as follows:
include (literally include!) a file into your index file called something like 'page_selection.php'
In this page, put a switch statement, which evaluates a variable (usually) passed in the query string in the format index.php?page=news or something like that.
You switch would then use $_REQUEST[page] and based on the value, include a different page (if that makes sense)
This way you can have a template, and just use the page_selection.php include to decide which content to display in the middle.
Well actually if ive understood properly ive already done the inverse of that as i have the template split into top and bottom files which i then just include at the top and bottom of each page - im sure your way is better but if it aint fixed...
(\__/)
(='.'=)
(")_(")
The method I'm suggesting can be used to complement your header and footer files.
What is enables you to do, is have a single index.php and have all the content displayed through that. It enables you to only require one template page.
Traditional web design has historically has seperate pages eg. news.html, products.html etc
This method is often transposed on to scripting languages e.g. home.php, contactus.php etc
What you can do, is simply have one master page, e.g. index.php and then have the links to other pages in the format index.php?page=home, index.php?page=news etc etc
Inside index.php, you can put a little logic, so it knows which page to include when the page variable is set, and voila a single page with all other content plugged into the middle.
Example:
index.php
navcontroller.phpPHP Code:
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
</head>
<?php
//by using one index.php file you can call your main script library at the start, and
//setup a DB connection only once
require_once("scripts/library.php");
connectToDB();
?>
<body>
<div id="header"></div>
<div id="content">
<div id="innercontent">
//here you include your navcontroller file which dictates what content is
//included in the page
<?php include("includes/navcontroller.php");?>
</div>
</div>
<div id="footer">
</div>
</body>
</html>
In this example you would need files inside the 'includes' directory which would contain you content. home.php and portfolio.php in this example.PHP Code:
<?
if(isset($file))
{
switch ($file)
{
case "home":
include "includes/home.php";
break;
case "portfolio":
include "includes/portfolio.php";
break;
//the default situation in a switch catches the situation where the file variable
//has been set to something stupid, like file=jhasdkjf
default:
print("<h1>site error</h1>");
print("<hr/>");
print("<h2>No Page Found</h2>");
print("<p class=\"content\">
The link you have chosen has not found a valid page use the link below to
return to the home page</p><p class=\"content\">
Please check the link if you typed it yourself, use the <a href=\"index.php?file=contact\">
contact</a> form to inform the webmaster of a site error.</p>");
print("<hr/>");
}
}
// This catches the default situation where no 'file' variable has been set
else
{
include "includes/home.php";
}
?>
I hope that clarifies things
There are currently 1 users browsing this thread. (0 members and 1 guests)