Jump to content

learning php


j05hr

Recommended Posts

  • Replies 115
  • Created
  • Last Reply

I asked my provider if i had web hosting with php to which he replied.

 

Yes, PHP is supported on your plan and you do not have to do anything as I have installed the PHP valueapp for you.

 

So does this mean i can just load the php file into my ftp? And if so where does the data go when someone submits a form?

 

PHP files should go into your web root folder, which is typically named something along the lines of public_html.  As a general rule of thumb, if you're supposed to put HTML files somewhere, then you can put PHP files there as well.

 

Form data is delivered to the script named with the action attribute.  Example:

<form name="form1" action="processForm.php" method="post">
<!-- bunch of inputs and stuff -->
</form>

 

The data entered into that form would be sent to the file processForm.php.  If you want to save form data, you'll need to either save it to a database or write it to a file.  Using my example above, you'd have to write that code within processForm.php.

 

One of the cool things about PHP is how easy it is to create sticky forms.  These are forms that 'remember' what the user previously entered if the user entered invalid/bad/wrong data.  The general form of it is:

if (data has been submitted)
{
   process it, then redirect the user
}
else
{
   show the form with any previously entered values in the form's fields
}

 

A simple (i.e., not for production use) example would be:

<?php
require_once("dbconnect.php");

if(isset($_POST['submit'])) //has data been submitted?
{
   if(isset($_POST['name'])) //has a name been submitted?
   {
      $name = $_POST['name'];
   }
   else
   {
      $name = false;
   }

   if(isset($_POST['email'])) //has an e-mail address been submitted?
   {
      $email = $_POST['email'];
   }
   else
   {
      $email = false;
   }

   if($name && $email) //if everything's been submitted correctly
   {
      $query = "INSERT INTO myUsers (name, email) VALUES ('$name', '$email')";
      $result = mysql_query($query);
      header("Location: index.php"); //redirect the user back home
   }
}
//if things aren't right, display the form
?>

<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; /* use THIS script to process the form data */ ?>" method="post">
   Name: <input name="name" type="text" value="<?php if(isset($name)){ echo $name; } ?>" /><br />
   E-mail: <input name="email" type="text" value="<?php if(isset($email)){ echo $email; } ?>" /><br />
   <input name="submit" type="submit" value="Submit" />
</form>

Link to comment
Share on other sites

but what would you type in to login in to the database like if its localhost you use root? or would it still be the same? and i'm still not sure where i would be access a form that someone submitted for example my website is www.jasongold.org and on there is a contact page and that is what i'm wanting to turn into a php form

Link to comment
Share on other sites

but what would you type in to login in to the database like if its localhost you use root? or would it still be the same? and i'm still not sure where i would be access a form that someone submitted for example my website is www.jasongold.org and on there is a contact page and that is what i'm wanting to turn into a php form

 

I think you're over-thinking this.  Let's start at the beginning and work from there.

 

When dealing with the database, file location doesn't really matter so long as the database and file you want to have access it are on the same server.  In fact, localhost means 'local to this host.'  Your contact page could be buried in nested directories, like say public_html/new/autumn/contact.php.  You would still access the database by specifying localhost as the host.

 

With your contact page in particular, you need to use a form element in order to handle the info the user types into the fields.  This is a standard HTML element, and is something you'll need to get comfortable with whenever you need to get user-inputted info.  A form element looks like this:

<form name="form1" action="process.php" method="post">
</form>

 

The form's attributes are VERY important:

Name - the name of your form.  Useful when using JavaScript

 

Action - the name of the file that processes the data entered into the form.  This is the name of the PHP file you want to use to deal with the form data

 

Method - how that data is actually transfered.  There are two options: get and post.  Get passes the data as name/value pairs within the URL itself (also known as a query string).  Ever been to a site that has an address like www.somesite.com/index.php?action=comment&user=1432 ?  Everything after the first '?' is a query string.  Post passes the data behind the scenes, so it's not visible in the URL.

 

Within the form element should be your inputs.  These are the form fields and/or buttons that the user can interact with.  So, with these additions, you'd have something like:

<form name="form1" action="process.php" method="post">
   Name: <input name="name" type="text" /><br />
   E-mail: <input name="email" type="text" /><br />
   <input name="submit" type="submit" value="Submit" />
</form>

 

Any inputs with the type "submit" tell the browser to submit whatever info is in the other inputs (if any) to the file specified with the action attribute (in our case, process.php) when that button is clicked.  It quite literally tells the browser "we're now submitting all the data associated with this form."  The form element is the glue that holds it all together.  It groups all of the inputs inside of it as the data needing to be sent, then sends only that data to the server when the submit button is pressed.

 

It's then up to you to actually write process.php to handle that incoming data.  Let me know if you need help handling that data now that you know how to send it to a PHP script.

Link to comment
Share on other sites

@Nightslyr:  Just letting you know that you don't really use name="" as much as you use id="" these days, because IDs can function as anchors and can easily be worked with in Javascript and CSS.

 

Is this for the form element itself, or everything including the inputs?  I ask because I think the superglobals only use the input names as the keys.  I'd be more than happy if they recognized ids, too.

 

I agree regarding the form element itself, though.  I've been playing with ASP.NET too much lately...you do NOT want to know how it handles the ids of anything told to run on the server.  Makes CSS a pain in the ass.

Link to comment
Share on other sites

thanks you been really helpful is there any chance you can incorporate the form name into my html code as mine's all over the place and i'll probably get it wrong, i think i will also need help handling the data if that's ok?

 

Okay, first, let's build the form.  Before we begin, though, a caveat:

 

Programs like the one you used to construct your site tend to write bad code.  It's just the nature of how they work.  So, what I'm going to show you isn't necessarily the best way to do things overall, but it should be the easiest to implement as it won't really change the meat of the page's layout.  Like anything in computing, there's a trade-off.  In this case, it's a trade-off in efficiency for you in the short term vs. shortening your site's code and making things more modular in the long term.

 

Go to line 117, and replace the line there with the following:

<td rowspan="2" bgcolor="#000000"><form id="myForm" action="processContact.php" method="post"><table width="740" height="169" border="0">

 

This will begin the form.

 

The good news is that you already have all of your inputs created.  You just need to rename them so they're easier to deal with when you finally process their info.

 

On line 128, replace that line with:

<td><input name="name" type="text" size="25" maxlength="30" /></td>

 

Replace 142 with:

<td><input name="email" type="text" size="25" maxlength="50" /></td>

 

Replace 156 with:

<td><input name="dayPhone" type="text" size="25" maxlength="12" /></td>

 

Replace 170 with:

<td><input name="nightPhone" type="text" size="25" maxlength="12" /></td>

 

Replace lines 184 - 190 with:

<td><select name="howToReach">
   <option value="email">E-mail</option>
   <option value="dayPhone">Telephone Day</option>
   <option value="nightPhone">Telephone Night</option>
</select></td>

 

And yes, I do want you to remove the form element from that block of code.  It's unnecessary now that we have all of this within a form element.

 

Replace 204 with:

<td><input name="eventType" type="text" size="25" maxlength="30" /></td>

 

Replace 218 with:

<td><input name="eventDate" type="text" size="25" maxlength="20" /></td>

 

Replace 232 with:

<td><input name="budget" type="text" size="25" maxlength="10" /></td>

 

Replace 246 with:

<td><input name="howDidYouFind" type="text" size="25" maxlength="50" /></td>

 

Replace 260 with:

<td><textarea name="comments" cols="25" rows="3"></textarea></td>

 

Replace 274 and 275 with:

<td>            <input type="submit" name="submit" value="Submit" />
  <input type="reset" name="reset" value="Clear" /></td>

 

Finally, replace 286 with:

</table></form></td>

 

That should build a capable form without forcing you to rewrite your entire page.

 

Now, before we get to processing all this info, what, exactly, do you want to do with it?  I can't help you unless I know how you want to process it.

Link to comment
Share on other sites

I copy and pasted on the lines you said and the forms came out a little mental. I saved it so you could view as you might be able to tell whats wrong www.jasongold.org/contact1

 

as for the way i want to process it i'm not sure the different ways of doing it, which way do you find best? and what are the options?

 

 

Link to comment
Share on other sites

I copy and pasted on the lines you said and the forms came out a little mental. I saved it so you could view as you might be able to tell whats wrong www.jasongold.org/contact1

 

as for the way i want to process it i'm not sure the different ways of doing it, which way do you find best? and what are the options?

 

Hmm...try removing lines 287 - 289.  It looks like it's trying to end a few elements that don't actually exist.  That should, hopefully, move your Flash banner up a bit.

 

As far as processing the info goes, you do have options.  The easiest thing I could do is set it up so the script e-mails you the info after each submission.  I could also save it to a database, but you'd need to have another script to access and display that info.  Another option is to save it to a text file.  There's also the option of combining some of these ideas.  You could get an e-mail from the site telling you someone submitted info, but have the actual info saved to the DB or file.

Link to comment
Share on other sites

i fixed the problem with the banner now, it was because you said delete code that was 6 lines long and replaced it with 4 lines and then when it came to taking out the rest of the code i was doing it 2 lines down from where i should of been taking it out from.

 

i like the idea of the last option for submitting the form if that's not too much hassle? The option being getting an email from the site saying someone has submitted info but getting it sent to the database or file.

Link to comment
Share on other sites

i fixed the problem with the banner now, it was because you said delete code that was 6 lines long and replaced it with 4 lines and then when it came to taking out the rest of the code i was doing it 2 lines down from where i should of been taking it out from.

 

i like the idea of the last option for submitting the form if that's not too much hassle? The option being getting an email from the site saying someone has submitted info but getting it sent to the database or file.

 

Okay, cool.  I'll see if I can write something up for you by the end of the day.

Link to comment
Share on other sites

773408_database.JPG

i've got a database but i'm not sure all the types are right?

 

Looks pretty good.  I'd change the phone numbers to VARCHAR's, though, as that way you can save any hyphens or parentheses that the user enters in order to divide up the area code from the rest of the number.  Either that, or we could change your form so that each component of the telephone numbers is their own field, then create the phone number from that.  In other words:

Day phone: <input type="text" name="dayPhone[]" /> - <input type="text" name="dayPhone[]" /> - <input type="text" name="dayPhone[]" />

 

I'm not sure if that code snippet will make much sense for you, as I'm not sure how much PHP (if any) you know.  What that bit of code does is store each phone number component (area code, etc.) as a different element of an array.  Inside of the script that will process this info, we can just go through each element and construct our phone number:

$dayPhone = "({$_POST['dayPhone'][0]}) {$_POST['dayPhone'][1]}-{$_POST['dayPhone'][2]}";

 

If none that makes any sense, then don't worry about it.

 

To get you started on your way, create a file with notepad named dbconnect.php.  Then, write the following inside:

<?php
   DEFINE ('HOST', 'localhost');
   DEFINE ('USER', 'user');
   DEFINE ('PASS', 'password');
   DEFINE ('MYDB', 'mydb');
?>

 

And replace the lowercase values of 'user', 'password', and 'mydb' with your database login info (your actual database username and password) and the name of the database you're trying to connect to (in your case, most likely jason_gold).  Once that's written, upload it to your web host, but put it in the directory above your web root, if possible.  This will help secure this critical information from prying eyes.

 

Okay, the next step is to actually do some more web design.  You probably want to have a page saying something along the lines of "Thank you for submitting your information" along with a way for them to continue to navigate through your site.  So, create that page like you normally would, but name the file processContact.php.  This page will be where all the magic happens.

 

Let me know when you're ready for the next step.

Link to comment
Share on other sites

i follow what you're saying with the phone number and changed it to varchar.

 

About uploading it to my webhost, normall i put my html files in /var/www/html so do you mean put it in /var/www?

 

Actually, you should probably put it in /var, just to be safe.  I have the feeling that /var/www is still publicly readable.

Link to comment
Share on other sites

i've done that just about to make the html page saying thanks for submitting the form.

 

is there a way to change my password in phpMyAdmin or does it have to be done in mySQL? i stupidly made my password the same thing for everything else and as i have no experience with php another time i could come along and put it in a readable place.

Link to comment
Share on other sites

i've done that just about to make the html page saying thanks for submitting the form.

 

is there a way to change my password in phpMyAdmin or does it have to be done in mySQL? i stupidly made my password the same thing for everything else and as i have no experience with php another time i could come along and put it in a readable place.

 

Hmm... I think it may depend on how your hosting is set up.  Let me check with my phpMyAdmin.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.