|
-
Nov 26th, 2006, 08:11 PM
#1
Thread Starter
Hyperactive Member
2 Very quick questions
Both unrelated but I thought it would not be worth putting them in separate threads:
1. I'm using this to get the date: date('Y-m-d');
What if I want to get the date in 30 days time?
2. One part of my code says:
if (!$title | !$info | !$email | !$biog ) {
die('You did not complete all of the required fields please <a href="">try again</a>');
When the user clicks on 'try again' I would like to send them to page they were just on (ie. addposts.php) however preferably without having to start typing everything up all over again. I know javascript could help with that but how can I tell the browser to go back one...
Cheers
-
Nov 26th, 2006, 08:14 PM
#2
Stuck in the 80s
Re: 2 Very quick questions
 Originally Posted by wwwfilmfilercom
1. I'm using this to get the date: date('Y-m-d');
What if I want to get the date in 30 days time?
Various ways, but I prefer:
Code:
date('Y-m-d', strtotime( "+30 days" ) );
As you can imagine, you have many options there. +1 week, +15 day, -4 hours, -30 seconds.
 Originally Posted by wwwfilmfilercom
2. One part of my code says:
if (!$title | !$info | !$email | !$biog ) {
die('You did not complete all of the required fields please <a href="">try again</a>');
When the user clicks on 'try again' I would like to send them to page they were just on (ie. addposts.php) however preferably without having to start typing everything up all over again...
Example:
Code:
<?php
$title = isset( $_POST[ "title" ] ) ? $_POST[ "title" ] : "";
$email = isset( $_POST[ "email" ] ) ? $_POST [ "email" ] : "";
?>
<form action="" method="post">
Title: <input type="text" name="title" value="<?php echo $title; ?>" />
Email: <input type="text" name="email" value="<?php echo $email; ?>" />
</form>
-
Nov 26th, 2006, 08:20 PM
#3
Re: 2 Very quick questions
For Q1 the below will work.
PHP Code:
echo "Date now = ".date("d/m/Y",time());
echo "<br>Date in 30 days is = ".date("d/m/Y",time() + 2592000);
//60 * 60 * 24 * 30
//60 secs in min
//60 mins in hour
//24 hours in day
//30 days you want to get
For Q2 - when you go "back" to your form add the variables that have been filled in to the relivant fields.
PHP Code:
<input name = "title" value ="<?php echo $title?>">//etc...
-
Nov 26th, 2006, 08:30 PM
#4
Re: 2 Very quick questions
if you're using die(), you will NOT be able to display the values of those inputs by the two methods described above, because if you click a link to go back OR hit the back button on your browser, that script acts as if it did before: the POST variables will not be set. you should, instead, make the script's action itself (ie: <form action="this_scripts_name.php" method="post">), and check on whether or not it was submitted. if it was, you can do error checks and things like that, if there are errors you can then display the form again highlighting each individual error, if you so choose. quick example:
register.php:
PHP Code:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$errors = array();
//error checking
if(isset($_POST['name']){ //put all of your form variables here, in this case I only have one
if($_POST['name'] == '')
$errors[0] = 'You must enter a name';
if(count($errors) == 0){
//do your stuff. send queries to database, etc
?>
<h1>you successfully submitted the form, <?php echo $_POST['name']; ?>!</h1>
<?php
}
}else
die('Invalid form input'); //in this case, someone is probably trying to unjustly access our form without the correct inputs
$displayform = (count($errors) > 0) ? true : false;
}
if($displayform){
?>
<form action="register.php" method="post">
Name: <input type="text" name="name"<?php if(isset($_POST['name'])) echo ' value="' . htmlentities($_POST['name']) . '"'; ?> /> <?php if(isset($errors[0])) echo $errors[0]; ?><br />
<input type="submit" value="submit!" />
</form>
<?php } ?>
-
Nov 26th, 2006, 08:33 PM
#5
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
Thx, I understand Q1.
Q2 is a little more complex because I have a form on addpost.php but I submit that to postme.php where I do all the checks and it do the table. If the validation fails on postme.php I want to go back to addpost.php yet fill in all the fields.
The problem I guess is that I'm not just filling in textinput but I have to set an option box, checkboxes and their corresponding texboxes!!
My form on addpost.php
Code:
<form action="postme.php" method="post">
<input type="hidden" name="userid" value="<? echo "$userid"; ?>"><br />
Title: <input type="text" name="title"><br />
Category: <select name="category">
<?php
....
$i=0;
while ($i < $num) {
$catid=mysql_result($result,$i,"Catid");
$catname=mysql_result($result, $i, "Catname");
echo "<option value='$catid'>$catname</option>";
$i++;
}
}
?>
</select>
<br />
Info: <textarea rows="10" cols="40" name="info"></textarea><br />
Requirements:<br />
<?php
$query="SELECT * FROM Skills";
$result=mysql_query($query) or die( "Unable to do query");
$num=mysql_num_rows($result);
if ($num==0) {
echo "The database contains no skills";
} else {
$i=0;
while ($i < $num) {
$skillid=mysql_result($result,$i,"Skillid");
$skillname=mysql_result($result,$i,"Skillname");
echo "<input name='skills[]' type='checkbox' onclick='this.form.txt$skillname.disabled = !this.checked' value='$skillid' checked>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";
$i++;
}
mysql_close();
}
?>
<br />
<input type="Submit" value="Post">
</form>
So as you can see that just fills out a form with the standard variables...
postme.php does this then:
Code:
$user=$_POST['userid'];
$title=$_POST['title'];
$category=$_POST['category'];
$dateadd=date('Y-m-d');
$datekill=date('Y-m-d', strtotime( "+30 days" ) );
$info=$_POST['info'];
$skills=$_POST['skills'];
$txt = $_POST[ "txt" ];
...
//This makes sure they did not leave any fields blank
if (!$title | !$info | !$email | !$biog ) {
die('You did not complete all of the required fields <a href=" "> try again</a>');
I'm not sure how to modify addposts.php to allow the variables to be added in if they were already added... Of course maybe there is a better method than having the form on one page and validation on the next but I don't know enough about this yet... (I will be adding JS validation later on however..)
Maybe I would be better off using just JS validation for the 'go back one page and have the form still filled in' bit??
Any help would be appreciated;
-
Nov 26th, 2006, 08:36 PM
#6
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
Kows - thanks for that advice it looks really interesting code, of course I haven't even touched html entities etc yet but I know I will have to! Hmm I think I will return to this in the morning, its starting to confuse me quite a bit lol...
Thx for the help guys!!!
-
Nov 26th, 2006, 08:41 PM
#7
Re: 2 Very quick questions
javascript can't do what you want to do.
I would say the best possibility is what I posted just above your post (not sure if you've seen it or not). For <select> and checkbox form fields, it will all depend on how you're building your form. Basically, for your <select>, you will do this:
PHP Code:
Category: <select name="category">
<?php
$i=0;
while($i < $num){
$catid=mysql_result($result,$i,"Catid");
$catname=mysql_result($result, $i, "Catname");
echo '<option value="$catid"';
if(isset($_POST['category']) && $_POST['category'] == $catid))
echo ' selected="selected"';
echo '>' . $catname . '</option>';
$i++;
}
?>
</select>
edit: The htmlentities() function just converts HTML entities, like dobule quotes, ampersands (&) and other things like that, to XHTML compliant characters, like & quot; and & amp;. It will make sure that your inputs do not incorrectly "break" because someone entered a quote in their text.
-
Nov 27th, 2006, 07:19 AM
#8
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
you should, instead, make the script's action itself (ie: <form action="this_scripts_name.php" method="post">), and check on whether or not it was submitted
Is this standard way that I should always handle forms? What I am doing at the moment is posting a forms variables to another php script which validates it then adds it in. Could this be all done in one form?
-
Nov 27th, 2006, 10:16 AM
#9
Re: 2 Very quick questions
yes. it can always be done from one form. doing two, in my opinion, is just a hassle, especially if you want complex error checking and the ability to reprint your form without actually having your form stored in two files. also, if you had 10 forms on your website, you would have 20 different files for all of them. that's a huge mess, when considering how big the rest of your site could be.
-
Nov 27th, 2006, 11:13 AM
#10
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
Good point, this is something I need to look into more.
I first started off my admin pages by designing the simple forms to add/change/delete stuff from certain tables...
What I wanted was to show my table in forms ie. instead of just printing out the names, id show them in textinputs. Then, if I wanted to I could write something else in the name textinput, press update and it would update.
What I ended up doing was having to make a page called edit which would show the form and then click again to update and take me back to the original table.
Are you saying I could do this:
I'd have a table and then load it up into a form, so eg. I could just load up textinputs for each record.... Then next to each record have an update button. Clicking on that would do a validation and update the form, showing the new record but still in 'form view'... lol hope that makes sense.... Basically I could do the job of 3 pages in 1...
If thats possible can you put an example here or link me to one, I kinda understand your code above but can you simplify for me?? Lol if thats possible...
-
Nov 27th, 2006, 05:44 PM
#11
Re: 2 Very quick questions
Your best option is to use sessions, this can be a standard way of doing it too. I also took the time to reply to one of your posts that asked a very similar question a few days ago.
http://www.vbforums.com/showpost.php...21&postcount=2
If there is anything here you don't understand. I would be happy to help.
-
Nov 27th, 2006, 05:47 PM
#12
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
Excellent thanks loads for that!!
Can you give me a quick idea of why sessions would be so good in this case?
Someone told me they are easier to use than cookies, and to be honest I found cookies quite easy to use
-
Nov 27th, 2006, 06:02 PM
#13
Re: 2 Very quick questions
 Originally Posted by kows
yes. it can always be done from one form. doing two, in my opinion, is just a hassle, especially if you want complex error checking and the ability to reprint your form without actually having your form stored in two files. also, if you had 10 forms on your website, you would have 20 different files for all of them. that's a huge mess, when considering how big the rest of your site could be.
There are many advantages in having two scripts and it may be a little more hassle at first but the benifits outweight this 100 fold:
- Firstly it clearly separates the purpose of the two scripts. The script that hands displaying the form and the script that verifys, validates and processes the input.
- Sessions solve the problem of passing variables between requests. Thery are not complex to use and have only slight perfomance impact. Again the benifits out weigh the drowbacks.
- In more complex scenarios, you can actually have a single script that receives input from all forms, validating an verifying the variables before passing the data onto a more specific script that actually processes the input.
- Pressing the back button in the browser after submitting the form causes the browser to attempt to repost the data and subsequently display an alert asking the user to confirm. If they have submitted the data once, you may not want them to submit it agian. Using a redirect would require that the user manually fille out and submit the form again.
- There is no reason for this approach to produce a "huge mess" of files. as mentioned above, a single script can act as input for several forms. Similarly, a single output sscript can act as a template that displays separate forms.
-
Nov 27th, 2006, 06:21 PM
#14
Re: 2 Very quick questions
 Originally Posted by wwwfilmfilercom
Excellent thanks loads for that!!
Can you give me a quick idea of why sessions would be so good in this case?
Someone told me they are easier to use than cookies, and to be honest I found cookies quite easy to use 
Cookies are quite easy to use in PHP, however they serve a different purpose to the session. A cookie is a small piece of data that the server requests the client to store (i.e: the web browser). The browser should (but doesn't always) store the cookie and send the cookie back with each request made.
The cookie is ideal for storing very small peices of data for a short or long period of time. For example, the number of posts the user likes to see on a page. The web browser will typically allow between only 10-20 cookies per site domain and each cookie typically no larger than 300 bytes.
The limitations on the number and size of cookies causes a problem when you want to store large amounts of data relating to the current user, such as the results of a form submission, the state of their shopping cart or a backup of the current post they are making.
A session is simply a file stored on the server that can contain any amount of data relating to a specific user. The file is identified using a session ID, that, funnily enough, PHP sends as a cookie to the web browser. Not only can you store any amount of data relating to the users current session, you can also store that data securely (provided you take the appropriate measures to prevent session hijacking). It is only ever visible to your script and only the data you wish is visible to the user.
Sessions are very easy to use:
PHP Code:
/* opens the session - creates one by sending a cookie if it doesn't exist */
session_start();
$_SESSION['myvar'] = 'myvalue'; // store a session variable
$otherVar = $_SESSION['otherVar']; // retrieve a session variable
Its as simple as that and you can store anything you would normally store in a standard PHP variable inside the $_SESSION super global array, including multi-dimensional arrays, objects, strings and numbers.
Sessions, by their nature are temporary and not ideal for storing user data over long periods. The Cookie sent by the browser is a session cookie (i.e: the browser should delete it once closed) and stale session files are regualrly removed by PHP after a certain period of inactivity. Large amounts of long term data should be stored somewhere like a database.
Of course, all this behaviour can be configured, including, the duration the cookie is stored, the name of the session, the path where session files are used and even a set of custom function callbacks to save a retrieve session data manually.
You can find all there documentation on the PHP web site. Including some very useful tips at the bottom in the user comments section:
http://www.php.net/session
-
Nov 27th, 2006, 06:45 PM
#15
Thread Starter
Hyperactive Member
Re: 2 Very quick questions
Thx that's ace!!! I was lookin through ur blog as well - u seem well smart!!! (Like most of the ppl on here) lol at least u know wat ur talkin about - Ill check out the link thanks!
-
Nov 27th, 2006, 06:56 PM
#16
Re: 2 Very quick questions
 Originally Posted by kows
edit: The htmlentities() function just converts HTML entities, like dobule quotes, ampersands (&) and other things like that, to XHTML compliant characters, like & quot; and & amp;.
Correct in principle, incorrect in terminology. htmlentities() converts characters that must be escaped as HTML entities, into their HTML entity representations. It has nothing to do with XHTML although it can and should be used with it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|