Jump to content

Pls help a php newb (form related)


xzased

Recommended Posts

Hi y'all. I've been required to setup a form... but I know nothing of php. So far I only have the code (I dont even know if its ok). Then I just copy/paste it on the website. Im using phpmyadmin btw. When I hit submit, there is an error saying the page is not found and the browser displays "http://www.mypage.com/?php echo $_SERVER['localhost']; ?  .Here is the code:

 

 

<?php

 

if (isset($_POST['submitted'])) {

 

  $LastName = trim(strip_tags($_POST['LastName']));

 

  $Street1 = trim(strip_tags($_POST['Street1']));

 

  $Street2 = trim(strip_tags($_POST['Street2']));

 

  $Phone = trim(strip_tags($_POST['Phone']));

 

  $Email = trim(strip_tags($_POST['Email']));

 

$FirstName = trim(strip_tags($_POST['FirstName']));

 

  $City = trim(strip_tags($_POST['City']));

 

  $State = trim(strip_tags($_POST['State']));

 

  $Zip = trim(strip_tags($_POST['Zip']));

 

  $Comments = trim(strip_tags($_POST['Comments']));

 

  $dbid = mysql_connect ('db1118.perfora.net(server)', 'xxxusername', 'xxxpassword');

 

  mysql_select_db("databasename",$dbid)

 

    or die ("Cannot find database");

 

  $query = "INSERT INTO `tablename` (`FirstName`, `LastName`, `Street1`, `Street2`, `Phone`, `Email`, `City`, `State`, `Zip`, `Company`, `Comments` ) VALUES (1, `$FirstName`, `$LastName`, `$Street1`, `$Street2`, `$Phone`, `$Email`, `$City`, `$State`, `$Zip`, `$Company`, `$Comments`)";

 

  $result = mysql_query($query,$dbid)

 

    or die("INSERT error:".mysql_error());

 

  echo 'Row inserted';

 

  exit;

 

}

 

?>

 

<HTML> <BODY>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['localhost']; ?>">

 

 

<div class="fieldframe"><table border="0" cellpadding="2" cellspacing="1" width="100%"><tbody><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">First name:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="FirstName" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Last name:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="LastName" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Phone:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Phone" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Email:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Email" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Street 1:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Street1" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Street 2:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Street2" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">City:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="City" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">State:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="State" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Zip:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Zip" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Company:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" name="Company" size="35" maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 200px;">Comments/ Questions:</td><td class="input" style="width: 70%; min-width: 70%;"><textarea class="inputform" cols="35" name="Comments" rows="7"></textarea></td></tr></tbody></table></div></td></tr></tbody></table></div>

 

 

 

<br><input type="submit" name="submitted" value="Submit" ></form><br>

 

 

 

</body>

 

</html>

 

Link to comment
Share on other sites

Your problem is is the html form, where it says action="<?php echo $_SERVER['localhost']; ?>"

 

That is not a page that deals with the script, all that is the server hosting your script, you should just point it to something like index.php or whatever you have.

 

By the way you can condense your script a bit here :

<?php
if (isset($_POST['submitted'])) {

foreach($_POST as $k => $v) {

$array[$k] = trim(strip_tags($v));

}

$dbid = mysql_connect ('db1118.perfora.net(server)', 'xxxusername', 'xxxpassword');

  mysql_select_db("databasename",$dbid);

etc...
?>

 

That will put all your vals in a nice little array to work with, look in the manual for foreach and arrays to see how they work, happy proggin

Link to comment
Share on other sites

Thanks Mr.Rock, that was fast... unfortunately, it shows the same error. I dont know what Im missing. I created the table on phpmyadmin (version 4 btw). I have checked all names for them to match the ones in the table. But still nothing.

 

Scottybwoy, thanks for the tip. I'll be trying that  ;)

Link to comment
Share on other sites

Hi there,

You might replace  action="<?php echo $_SERVER['localhost']; ?>" with  action="<? echo $_SERVER['PHP_SELF']; ?>"

Shouldn't get that error this way. Anyways, here is a complete tutorial of how to post a form to a database using php+mysql  - http://php.assistprogramming.com/how-to-post-a-form-to-a-database-using-php.html

 

Shows the same error  :'( Thanks for the tutorial though

Link to comment
Share on other sites

Here is my contact form which you may want to style with your css

 

<?
// Change to your own email address
$your_email = "youremail@yourdomain.com";

// This is what is displayed in the email subject line
// Change it if you want
$subject = "Message via your contact form";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

$self = $_SERVER['REQUEST_URI'];

$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$message = $_POST['txtMessage'];
$send = $_POST['send'];

$msg="<p>Please fill in this form if you have any queries or suggestions.</p>";

echo ($msg);

$form = "
    <form method=\"post\" action=\"$self\">

    <p><label for=\"txtName\">Name:</label>
    <input type=\"text\" title=\"Please enter your name\" id=\"txtName\" name=\"txtName\" size=\"40\" value=\"$name\" /></p>

    <p><label for=\"txtEmail\">Email:</label>
    <input type=\"text\" title=\"Please enter your email address\" id=\"txtEmail\" name=\"txtEmail\" size=\"40\" value=\"$email\"/></p>

    <p><label for=\"txtMessage\">Comments:</label>
    <textarea title=\"Please enter your message\" id=\"txtMessage\" name=\"txtMessage\" rows=\"20\" cols=\"45\">$message</textarea></p>

    <p>Please enter the text that you see in the image into the box below. You are asked to do this in order to verify that this message is not being performed by an automated process.</p>
    
    <p><label> </label></p>
    <div class=\"captcha\"><img src=\"includes/captcha.php\" alt=\"security image\" /></div>

    <p><label for=\"verify\">Anti-Spam key:</label>
    <input type=\"text\" title=\"Please enter the anti-spam key\" name=\"verify\" id=\"verify\" size=\"40\" /></p>

    <p><label> </label>
    <input type=\"submit\" class=\"sendbutton\" name=\"send\" value=\"Submit\" /></p>

</form>";

if($send)
{
    $valid=true;

if( !$name )
{ 
    $errmsg.="Please enter your name:<br />";
    $valid=false;
}

if( !$email )
{
    $errmsg.="Please enter your email address:<br />";
    $valid=false;
}
else
{
	$email = trim($email);
	$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
	$_host = "([-0-9A-Z]+\.)+";
	$_tlds = "([0-9A-Z]){2,4}$/i";

	if( !preg_match($_name."@".$_host.$_tlds,$email))
	{
		$errmsg.="Email address has incorrect format!<br />";
		$valid=false;
	}
}

if( !$message )
{ 
    $errmsg.="Please enter your message:<br />";
    $valid=false; 
}

if (empty($_POST['verify']) && $_POST['verify'] == $_SESSION['captchstr'])
{
    $errmsg.="Please enter security image:<br />";
    $valid=false;
}
}

if( $valid !=true )
    {
echo( "<span style=\"font-weight: bold; color:red;\">".$errmsg."</span>" . $form );
    }

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.<br />";
echo "If you are behind a firewall please check your referrer settings.";
        exit;
    }

    // The URLs matched so send the email
    if( mail($your_email, $subject, $message, "From: $name <$email>"));
    {
    	// Display the thankyou message
    	echo $thankyou_message;
    }
}
?>

 

To create the captcha image (if you want to use it) you need to download arial.ttf and upload it in the same place as captcha.php (I have them both in a folder called includes)

 

This captcha.php

<?php
session_start();
$strlength = rand(4,7);
$captchastr = "";
for($i=1;$i<=$strlength;$i++)
{
$textornumber = rand(1,3);
if($textornumber == 1)
{
$captchastr .= chr(rand(49,57));
}
if($textornumber == 2)
{
$captchastr .= chr(rand(65,78));
}
if($textornumber == 3)
{
$captchastr .= chr(rand(80,90));
}
}
$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);

//initialize image $captcha is handle dimensions 200,50
$captcha = imageCreate(190,50);
$backcolor = imageColorAllocate($captcha, $randcolR, $randcolG, $randcolB);

$txtcolor = imageColorAllocate($captcha, ($randcolR - 60), ($randcolG - 60), ($randcolB - 60));
for($i=1;$i<=$strlength;$i++)
{

$clockorcounter = rand(1,2);
if ($clockorcounter == 1)
{
$rotangle = rand(0,45);
}
if ($clockorcounter == 2)
{
$rotangle = rand(315,360);
}

//$i*25 spaces the characters 25 pixels apart
imagettftext($captcha,rand(14,20),$rotangle,($i*25),30,$txtcolor,"/arial.ttf",substr($captchastr,($i-1),1));
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$txtcolor);
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$backcolor);
}
//Send the headers (at last possible time)
header('Content-type: image/png');

//Output the image as a PNG
imagePNG($captcha);

//Delete the image from memory
imageDestroy($captcha);

$_SESSION["captchastr"] = $captchastr;
?>

Link to comment
Share on other sites

What didn't work about the script...i got it working fone on my site.....did you get any errors?

 

It just showed a chunk of script instead of embedding to the site. But I've been working on my original and I think I almost got it. When I hit submit it says:

 

INSERT error:Unknown column 'LastName' in 'field list'

 

I just got that message. At least is not the "file not found 404", but I dont know if this is worse... I dont think so. Anyways, thank you very much for your help  ;) If you have any insight on this message please let me know.

Link to comment
Share on other sites

Can you post the whole script so I can have a look?

 

I just attached the script. What I did is save the whole thing as php then send it via ftp to the site. I've tried it saving just the html script as php and copy/paste the html+php script to the site but with no results.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I'll show you tonight. Thanks for your post. At first it couldnt find the database, but now everything seems fine... But I cant find the info on the database  :P In the mean time, I think this has been resolved  ;D Thanks guys. Mr. Rock, here is the script I have so far:

<?php

if (isset($_POST[`submitted`])) {

   $FirstName = trim(strip_tags($_POST[`FirstName`]));

$LastName = trim(strip_tags($_POST[`LastName`]));

  $Street1 = trim(strip_tags($_POST[`Street1`]));

  $Street2 = trim(strip_tags($_POST[`Street2`]));

  $Phone = trim(strip_tags($_POST[`Phone`]));

  $Email = trim(strip_tags($_POST[`Email`]));



  $City = trim(strip_tags($_POST[`City`]));

  $State = trim(strip_tags($_POST[`State`]));

  $Zip = trim(strip_tags($_POST[`Zip`]));

  $Comments = trim(strip_tags($_POST[`Comments`]));

$server= "host";    
          $user= "username";                  
          $password= "pass";       
          $database= "dbname";         
          $table= "tablename";                

         $dbid=MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database, $dbid) or die ( "<H3>database not existent</H3>");

  $query = "INSERT INTO `tablename` (FirstName, LastName, Street1, Street2, Phone, Email, City, 

State, Zip, Company, Comments) VALUES (`$FirstName`, `$LastName`, `$Street1`, `$Street2`, `$Phone`, 

`$Email`, `$City`, `$State`, `$Zip`, `$Company`, `$Comments`)";

  $result = mysql_query($query,$dbid)

    or die("INSERT error:".mysql_error());

  echo 'Row inserted';

  exit;

}

?>


<HTML> <BODY>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER_['PHP_SELF'] ;?>">

<div class="fieldframe"><table border="0" cellpadding="2" cellspacing="1" width="100%"><tbody><tr 

class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" 

width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 

200px;">First name:</td><td class="input" style="width: 70%; min-width: 70%;"><input 

class="inputform" name="FirstName" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Last name:</td><td class="input" 

style="width: 70%; min-width: 70%;"><input class="inputform" name="LastName" size="35" 

maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr 

class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" 

width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 

200px;">Phone:</td><td class="input" style="width: 70%; min-width: 70%;"><input class="inputform" 

name="Phone" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Email:</td><td class="input" style="width: 

70%; min-width: 70%;"><input class="inputform" name="Email" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Street 1:</td><td class="input" 

style="width: 70%; min-width: 70%;"><input class="inputform" name="Street1" size="35" 

maxlength="80" value="" type="text"></td></tr></tbody></table></div></td></tr><tr 

class="field"><td><div class="field"><table cellpadding="2" cellspacing="0" 

width="100%"><tbody><tr><td class="label" style="width: 30%; min-width: 50px; max-width: 

200px;">Street 2:</td><td class="input" style="width: 70%; min-width: 70%;"><input 

class="inputform" name="Street2" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">City:</td><td class="input" style="width: 

70%; min-width: 70%;"><input class="inputform" name="City" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">State:</td><td class="input" style="width: 

70%; min-width: 70%;"><input class="inputform" name="State" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Zip:</td><td class="input" style="width: 

70%; min-width: 70%;"><input class="inputform" name="Zip" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Company:</td><td class="input" style="width: 

70%; min-width: 70%;"><input class="inputform" name="Company" size="35" maxlength="80" value="" 

type="text"></td></tr></tbody></table></div></td></tr><tr class="field"><td><div 

class="field"><table cellpadding="2" cellspacing="0" width="100%"><tbody><tr><td class="label" 

style="width: 30%; min-width: 50px; max-width: 200px;">Comments/ Questions:</td><td class="input" 

style="width: 70%; min-width: 70%;"><textarea class="inputform" cols="35" name="Comments" 

rows="7"></textarea></td></tr></tbody></table></div></td></tr></tbody></table></div>



<br><input type="submit" name="submitted" value="Submit" ></form><br>



</body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.