Jump to content


Photo

Cleaner more Organized PHP Scripts


  • Please log in to reply
4 replies to this topic

#1 jeliot

jeliot
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 02 April 2006 - 04:28 PM

Here I have a working php form script its nothing to write home about but it works


<head>
<title>Join Mailing List</title>
</head>


<?php
$db_host = "host name";
$db_user = "jeliot";
$db_pwd = "password";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$query = "INSERT INTO email_list (name,email) VALUES ('".$name."','".$email."')";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>


Here is my cleaner more organized script if have been woring with

<head>
<title>Join Mailing List</title>
</head>

<body>

<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>

<body>
<?php

if (!isset($_POST['submit'])) {
inlclude('./connect.php');
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO email_list (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.

}

?>

</head>
</html>

and here is my script to connect to my database

<html>
<head>

<title>Untitled</title>

</head>
<body>

<?
$db_host = "host name";
$db_user = "jeliot";
$db_pwd = "password";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

?>

</body>
</html>

I'm hoping for some different opinions on how i SHOULD be shorting this scipt to clean it up and descriptions on why those changes are better

ALL OPINIONS WELCOME

THANKS

#2 Desdinova

Desdinova
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 02 April 2006 - 05:04 PM

first of all, tab your code.

lets pretend '-' is a tab.

Do it like this:

if ($something){
- do this;
}

if ($somethingtwo){
- if ($somethingthree){
- - do that;
- }
}


this way you can see what is happening where. I suggest you download crimson editor, and go to options to add another vertical guideline halfway the screen. al the //comments and /* comments */ could be placed right next to it, giving you a short and brief overview.

another quick comment, I wouldn't suggest the submit button as an identification as to what process you should run. Somebody hitting enter in your form will not activate the button, thus not POSTing the submit button value. Rather place a hidden field with name submit and value set or something, so you really know what's going on. Unless you have multiple formbuttons with different option. Then you should use the submitname, and preferably disable the enter button.

The code itself looks ok. You could be able to connect your database using 2 lines
mysql_connect("localhost","username","pass");
mysql_select_db("dbname");

just 2 cents :)

mind the tabbing! You'll need it lateron. I promise.

#3 jeliot

jeliot
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 02 April 2006 - 06:10 PM

[!--quoteo(post=360910:date=Apr 2 2006, 12:04 PM:name=Desdinova)--][div class=\'quotetop\']QUOTE(Desdinova @ Apr 2 2006, 12:04 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
first of all, tab your code.

lets pretend '-' is a tab.

Do it like this:

if ($something){
- do this;
}

if ($somethingtwo){
- if ($somethingthree){
- - do that;
- }
}
this way you can see what is happening where. I suggest you download crimson editor, and go to options to add another vertical guideline halfway the screen. al the //comments and /* comments */ could be placed right next to it, giving you a short and brief overview.

another quick comment, I wouldn't suggest the submit button as an identification as to what process you should run. Somebody hitting enter in your form will not activate the button, thus not POSTing the submit button value. Rather place a hidden field with name submit and value set or something, so you really know what's going on. Unless you have multiple formbuttons with different option. Then you should use the submitname, and preferably disable the enter button.

The code itself looks ok. You could be able to connect your database using 2 lines
mysql_connect("localhost","username","pass");
mysql_select_db("dbname");

just 2 cents :)

mind the tabbing! You'll need it lateron. I promise.
[/quote]

Thanks the tabbing sounds like a good idea and i just downloaded crimson...........
i'm still at a loss for why that second scpript i posted isn't working in conjunction with my connect.php script to work like the first script does i must have somthing wrong somewhere...........

#4 Desdinova

Desdinova
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 02 April 2006 - 06:17 PM

Just keep the .connect with the PHP code. No need to include HTML.

.connect.php:

<?
$db_host = "host name";
$db_user = "jeliot";
$db_pwd = "password";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

?>



you have a typo in the code where the script gets included.

Other than that, change the include line to:
require (".connect.php");

require will kill the script when the file can't be found.
include will not.


#5 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 02 April 2006 - 10:38 PM

Heres how I would`ve done it

<?php

require ("./connect.php");

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
<html>
<head>
<title>Join Mailing List</title>
</head>
<body>
<form action="" method=\"post\">
Name: <input type=\"text\" name=\"name\" size=\"20\"><br />
Email: <input type=\"text\" name=\"email\" size=\"20\"><br />
<input type=\"submit\" name=\"submit\" value=\"Submit!\" />
</form>";

if (!isset($_POST["submit"])) 
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $query = "INSERT INTO email_list (name, email) VALUES ('".$name.",'".$email."')";
        $result = mysql_query($query) 
            or die(mysql_error());
    }

echo "</head></html>";

?>

<?php

$db_host = "host name";
$db_user = "jeliot";
$db_pwd = "password";
$db_name = "email";

mysql_connect($db_host, $db_user, $db_pwd) 
or die(mysql_error());

mysql_select_db($db_name) 
or die(mysql_error());

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users