Beginner help

software development

#1

I’m not a programmer, which is why I’m trying to use PHP and not Javascript or something. However, I can’t seem to get even the most basic PHP page to work on Dreamhost. I did install one of the PHP “One Click Installs” which I believe is all that’s needed to install the PHP functionality for my site. For a test, I’m just taking a form on page1 that collects (1) Name, and (2) age.

Then page2 is supposed to display them back. I got the code from a code site, so I can’t even think that I may have mistyped. Why aren’t variables being passed from 1 page to another? Any ideas?


#2

You don’t need to install any of the one-click-installs to enable PHP for your domain, it is enabled by default.

I am afraid it is going to be rather difficult for anyone to help without seeing the actual code.

Mark


Save [color=#CC0000]$50[/color] on DreamHost hosting using promo code [color=#CC0000]SAVEMONEY[/color] ( Click for promo code details )


#3

Normally when creating a php script you would save the file using a php extention instead of the normal html extention.
Passing varables like you are discribing are passed using one of two methods that are assigned when using a form. They are get method or post method.
Each item in your form is assigned a name, this name is your varable name that the php script will look at.

Take a look at this partial script. This type of script uses one page instead of two, because, I state the action to be itself.
As you will see each form object has a name including the submit button, these values are passed back to it self when the submit button is pressed. The script first checks to see if the submit button was pressed, if it was not it displays the form, otherwise it will verify the contents of the form.
I hope this code will help you see how it works, just to remind you that this is a partial code.
Silk

[code]if ($_POST[‘B3’]==false) {
print <<< LOGIN

PASSWORD PROTECTED - Please type in your username and password

Username: < br > Password:

LOGIN; } if (($_POST['username']!=$user) or ($_POST['password']!=$pass)) { if ($_POST['B3']==true) { print "

Incorrect user name or password

"; } exit; } [/code][url=http://www.silkrooster.com]My website[/url]

#4

Wow, that code looks much more complicated than I expected. Really, all I’m trying to do is pass user entries from page to page, and that’s it. My needs are to let my site visitors enter some contact information and answer a few questions on dinner choices before being passed to Paypal so that they can send our group the money for an annual dinner. Nothing fancy, and no login/passwords. So I’m starting simple, and here is the code that I’m borrowing from a website for testing the concept. If I remove the ‘method=“post”’ line then I see the user entries in the URL so page 1 seems to work just fine:

page1.html:

<?php session_start(); ?> Test Page

Data Collection

Name:
Age:

then, page2.php:

<?php session_start(); print "Your name is " .$_SESSION['Name']; print "
"; print "You are " .$_SESSION['Age']. " years old"; print "
"; $old = 25 + $Age; print "In 25 years you will be " . $old . " years old"; ?>

#5

Oh OK,
You are doing Sessions. Different story.
Now on page two, I think part of your problem is you did not assign your strings to equal the value of the session. As in $age=$_SESSION[‘age’];
I could be wrong, but I think that may be your problem.
Also with sessions you can pass strings from page to page using:
session_register(‘name of string’); right after session start.
Silk

My website


#6

[quote]Now on page two, I think part of your problem is you did not assign your strings to equal the value of the session. As in $age=$_SESSION[‘age’];
I could be wrong, but I think that may be your problem.
Also with sessions you can pass strings from page to page using:
session_register(‘name of string’); right after session start.[/quote]
Sorry Silk, but that advice has too much of a PHP4 flavor about it. There is no need for the session_register() function; however, you will need to harvest the form variables by referring to their superglobals. Therefore, your “page2.php” script should look like this:

<?php session_start(); $_SESSION['Name'] = $_POST['Name']; $_SESSION['Age'] = $_POST['Age']; print "Your name is " .$_SESSION['Name']; print "<br />"; print "You are " .$_SESSION['Age']. " years old"; print "<br />"; $old = 25 $Age; print "In 25 years you will be " . $old . " years old"; ?>Also, you will need to give “page1.html” a .php extension if you intend to use session_start() on there, although there is no reason to have the PHP at all.


Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#7

Simon, I think a lot of us have some re-learning to do…i still have to “look it up” with some of these PHP4 PHP5 differences…We are counting on you to “edumacate” us!

–rlparker


#8

The method I described above should work fine in PHP4 as well, it is just that the session_register() function will only work if register_globals is enabled; therefore, the old method will not work in DH’s PHP5 setup.


Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#9

Simon,

Thanks, this works! And I was even able to correct the type-o on the line that adds 25 years, so I may get this page done yet. Thanks.

Trent


#10

Simon,
Thanks for the heads up. That I did not know.
Silk

My website


#11

Just wanted to make a quick clarification… :wink:

PHP is considered a scripting language to many, but it’s (arguably) a programming language in reality, especially with PHP5’s new object model. PHP is robust indeed.

Cheers!

  • Alar