Author Topic: Need some php help  (Read 1068 times)

0 Members and 1 Guest are viewing this topic.

DevilsRefugee

  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 1099
  • Karma: +7/-1
  • Damn, I'm Good.
    • View Profile
    • James Wylie | Media Designer
Need some php help
« on: June 09, 2010, 21:50:44 PM »
Hi
I am doing web design again and for my first client he wants a contact form, and thanks to the interwebs thats not a problem.
This is what i have so far: http://www.jameswylie.co.uk/pigeons/contact.php

What i want to be able to do (and im willing to offer a small token payment from the cost of the site to whoever can help me) is i want the form to be on the actual contact page and not open in a new window.

If more info is needed (ie source pages or anything i will be happy to provide) im desperate, the interwebs is failing me...

cheers for reading :)
My Youtube page of goodies!
-------------------------------------

Up

  • Registered Forum Member
  • *
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Need some php help
« Reply #1 on: June 09, 2010, 22:32:31 PM »
Not an expert or anything but have you just tried
Code: [Select]
include "/pigeons/contact/contact-form.php";?

Humbug

  • Community Leaders
  • Registered Forum Member
  • ******
  • Posts: 4416
  • Karma: +36/-6
    • View Profile
Re: Need some php help
« Reply #2 on: June 09, 2010, 23:19:01 PM »
you could just include the contact form as suggest but it depends on how the form is handled. ie the target page for the form may forward to the contact form page (which would make sense) and not to contact us page which in it's self isn't a problem it all depends on how you want it to look and feel and what code changes need applying.
Cheers and Good Game

Humbug


DevilsRefugee

  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 1099
  • Karma: +7/-1
  • Damn, I'm Good.
    • View Profile
    • James Wylie | Media Designer
Re: Need some php help
« Reply #3 on: June 09, 2010, 23:33:33 PM »
well at the moment the link is on the contact.php (its just html inside)
the new page opens with the contact form in it (and all the php handling code) then once a successful submission occurs, the page then goes to thankyou.html which is just a 'form sucessfully submitted' kinda thing.

what i want is for the form to be on the contact page itself so i would have thought i need to basically copy the contact form page code into the intial contact page, but im not sure where all the code goes.

i have included the code from the page with the form on below.

Code: [Select]
/*******************************************************************************
 * Script configuration - Refer README.txt
*******************************************************************************/

/* Email address where the messages should be delivered */
$to = 'info@jameswylie.co.uk';

/* From email address, in case your server prohibits sending emails from addresses other than those of your
own domain (e.g. email@yourdomain.com). If this is used then all email messages from your contact form will appear
from this address instead of actual sender. */
$from = '';

/* This will be appended to the subject of contact form message */
$subject_prefix = 'New Message from Jonespigeons.co.uk Contact Form';

/* Security question and answer array */
$question_answers = array (
'Moon is red or white' => 'white',
'Sun is blue or yellow' => 'yellow',
'Fire is hot or cold' => 'hot',
'Icecream is hot or cold' => 'cold'
);

/* Form header file */
$header_file = 'form-header.php';

/* Form footer file */
$footer_file = 'form-footer.php';

/* Form width in px or % value */
$form_width = '70%';

/* Form background color */
$form_background = '#F7F8F7';

/* Form border color */
$form_border_color = '#CCCCCC';

/* Form border width */
$form_border_width = '1px';

/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = 'solid';

/* Form cell padding */
$cell_padding = '5px';

/* Form left column width */
$left_col_width = '25%';

/* Form font size */
$font_size = '12px';

/* Empty/Invalid fields will be highlighted in this color */
$field_error_color = '#FF0000';

/* Thank you message to be displayed after the form is submitted. Can include HTML tags. Write your message
between <!-- Start message --> and <!-- End message --> */
$thank_you_message = <<<EOD
<!-- Start message -->
<p>We have received your message. If required, we'll get back to you as soon as possible.</p><br /><br /><br /><br /><br /><br /><br /><br />
<!-- End message -->
EOD;

/* URL to be redirected to after the form is submitted. If this is specified, then the above message will
not be shown and user will be redirected to this page after the form is submitted */
/* Example: $thank_you_url = 'http://www.yourwebsite.com/thank_you.html'; */

$thank_you_url = 'http://www.jameswylie.co.uk/pigeons/thankyou.html';

/*******************************************************************************
 * Do not change anything below, unless of course you know very well
 * what you are doing :)
*******************************************************************************/

$name = array('Name','name',NULL,NULL);
$email = array('Email','email',NULL,NULL,NULL);
$subject = array('Subject','subject',NULL,NULL);
$message = array('Message','message',NULL,NULL);
$security = array('Security question','security',NULL,NULL,NULL);

$error_message = '';

if (!isset($_POST['submit'])) {

  showForm();

} else { //form submitted

  $error = 0;
 
  if(!empty($_POST['check'])) die("Invalid form access");
 
  if(!empty($_POST['name'])) {
  $name[2] = clean_var($_POST['name']);
  if (function_exists('htmlspecialchars')) $name[2] = htmlspecialchars($name[2], ENT_QUOTES);
  }
  else {
    $error = 1;
    $name[3] = 'color:#FF0000;';
  }
 
  if(!empty($_POST['email'])) {
  $email[2] = clean_var($_POST['email']);
  if (!validEmail($email[2])) {
    $error = 1;
    $email[3] = 'color:#FF0000;';
    $email[4] = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
  }
  }
  else {
    $error = 1;
    $email[3] = 'color:#FF0000;';
  }
 
  if(!empty($_POST['subject'])) {
  $subject[2] = clean_var($_POST['subject']);
  if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES); 
  }
  else {
  $error = 1;
    $subject[3] = 'color:#FF0000;';
  } 

  if(!empty($_POST['message'])) {
  $message[2] = clean_var($_POST['message']);
  if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES);
  }
  else {
    $error = 1;
    $message[3] = 'color:#FF0000;';
  }   

  if(empty($_POST['security'])) {
    $error = 1;
    $security[3] = 'color:#FF0000;';
  } else {
 
    if($question_answers[$_POST['question']] != strtolower(clean_var($_POST['security']))) {
      $error = 1;
      $security[3] = 'color:#FF0000;';   
      $security[4] = '<strong><span style="color:#FF0000;">Wrong answer</span></strong>';
    }
  }

  if ($error == 1) {
    $error_message = '<span style="font-weight:bold;font-size:90%;">Please correct/enter field(s) in red.</span>';

    showForm();

  } else {
 
  if (function_exists('htmlspecialchars_decode')) $name[2] = htmlspecialchars_decode($name[2], ENT_QUOTES);
  if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
  if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES); 
 
    $body = "$name[0]: $name[2]\r\n";
    $body .= "$email[0]: $email[2]\r\n\r\n";
    $body .= "$message[0]:\r\n$message[2]\r\n";
   
    if (!$from) $from_value = $email[2];
    else $from_value = $from;
   
    $headers = "From: $from_value" . "\r\n";
    $headers .= "Reply-To: $email[2]" . "\r\n";
   
    mail($to,"$subject_prefix - $subject[2]", $body, $headers);
   
    if (!$thank_you_url) {
   
      include $header_file;
      echo $GLOBALS['thank_you_message'];
      echo "\n";
      include $footer_file;
  }
  else {
  header("Location: $thank_you_url");
  }
       
  }

} //else submitted



function showForm()

{
global $name, $email, $subject, $message, $security, $question_answers, $header_file, $footer_file, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size;
include $header_file;
$question = array_rand($question_answers);
echo $GLOBALS['error_message']; 
echo <<<EOD

<form method="post" class="cForm">
<table style="width:{$form_width}; background-color:{$form_background}; border:{$form_border_width} {$form_border_style} {$form_border_color}; padding:10px; font-size:{$font_size};" class="contactForm">
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$name[3]}">{$name[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$name[1]}" value="{$name[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$email[3]}">{$email[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$email[1]}" value="{$email[2]}" /> {$email[4]}</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$subject[3]}">{$subject[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$subject[1]}" value="{$subject[2]}" size="40" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$message[3]}">{$message[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><textarea name="{$message[1]}" cols="40" rows="6">{$message[2]}</textarea></td>
</tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$security[3]}">{$question}?</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$security[1]}" value="" size="10" /> {$security[4]}
<br /><br />We are sorry but please answer the above question to prove you are a real visitor and not a spam bot.</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding}; font-size:90%; font-weight:bold;">All fields are required.</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding};"><input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" /></td>
</tr>
</table>
<input type="hidden" name="question" value="{$question}">
<input type="hidden" name="check" value="">
</form>
 
EOD;

include $footer_file;
}

function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
  return $variable;
}

/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && function_exists('checkdnsrr'))
      {
      if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
         // domain not found in DNS
         $isValid = false;
       }
      }
   }
   return $isValid;
}

?>


My Youtube page of goodies!
-------------------------------------

Up

  • Registered Forum Member
  • *
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Need some php help
« Reply #4 on: June 10, 2010, 00:02:06 AM »
I'm pretty sure you could just replace
Code: [Select]
<p>Click here to open our <a href="contact/contact-form.php"> contact form</a>.</p>with
Code: [Select]
<? include "/pigeons/contact/contact-form.php"; ?>
Just test it on your local server first before making it live though. xxx

Humbug

  • Community Leaders
  • Registered Forum Member
  • ******
  • Posts: 4416
  • Karma: +36/-6
    • View Profile
Re: Need some php help
« Reply #5 on: June 10, 2010, 00:47:28 AM »
yeah, i think up is correct in saying you could just _include_ the form.  for look and feel i would include it below the info for adrress etc but to try it out then do what up suggested just to see if it works.

im assuming you aint doing these changes on a live system :)
Cheers and Good Game

Humbug


DevilsRefugee

  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 1099
  • Karma: +7/-1
  • Damn, I'm Good.
    • View Profile
    • James Wylie | Media Designer
Re: Need some php help
« Reply #6 on: June 10, 2010, 01:14:32 AM »
ok something doesnt work (on the link the / before pigeons isnt in, but i did try it with that and got the same problem)

http://img824.imageshack.us/f/pigeoncontactform.png

tried:
"<? include "/pigeons/contact/contact-form.php"; ?>"
"<? include "pigeons/contact/contact-form.php"; ?>"
"<? include "contact/contact-form.php"; ?>"

thinking it may be something the way the page calls on the page with the form in, but cant seem to work out what's going wrong.  :-\

Thanks so far, sure its something or nothing..any ideas?

and yeah testing online, but only readers of this thread know it exists :P

*should really install php on this comp*
« Last Edit: June 10, 2010, 01:17:22 AM by DevilsRefugee »
My Youtube page of goodies!
-------------------------------------

Up

  • Registered Forum Member
  • *
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Need some php help
« Reply #7 on: June 10, 2010, 10:56:56 AM »
Due to the way your site is structured, I think that
Code: [Select]
<? include "/contact/contact-form.php"; ?>should work.

As a failsafe,
Code: [Select]
<? include "http://www.jameswylie.co.uk/pigeons/contact/contact-form.php"; ?>should (hopefully :P) definitely work.

frymaster

  • Community officer
  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 2190
  • Karma: +44/-7
  • Your argument is invalid, you are a squirrel
    • View Profile
Re: Need some php help
« Reply #8 on: June 10, 2010, 11:45:43 AM »
don't forget you'll have to remove a lot of stuff from that page you're including to make it work (like html headers etc.) - tbh in this case I'd just copy the form html and form handling php from that page and paste it into the page, rather than include'ing the file (the main use for includes is when you want to include the same stuff on every page)

Humbug

  • Community Leaders
  • Registered Forum Member
  • ******
  • Posts: 4416
  • Karma: +36/-6
    • View Profile
Re: Need some php help
« Reply #9 on: June 10, 2010, 11:56:24 AM »
yeah that's the best way of doing it or.. and i think this might be easier.... you add the contact info (address etc) to the contact form page.  That'll be a simple case of adding html output to that page an then change the link on the contact link to straight to the form page.
Cheers and Good Game

Humbug


fruitbaticus

  • Game Server Admins
  • Registered Forum Member
  • ***
  • Posts: 113
  • Karma: +3/-1
    • View Profile
Re: Need some php help
« Reply #10 on: June 10, 2010, 11:57:46 AM »
I, like fry, would copy the contact form stuff out of the second contact.php file and put it in your main one.

However, to make it work with includes, you should refer to it like so:

<?php include 'contact/contact-form.php'; ?>

It looks like you've tried this before, but perhaps the single quotes may make a difference (I also like to specifically reference php with: "<?php ..." )

You also really want to make sure that everything is in the same case in your source files, and that it matches the actual files, usually you just make everything lower case, this may not be a problem now, but it's good form, especially if the site gets ported at a later date.

Not sure if i've actually said anything which will make it work, but hopefully some more playinbg around will sort it.  :D

DevilsRefugee

  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 1099
  • Karma: +7/-1
  • Damn, I'm Good.
    • View Profile
    • James Wylie | Media Designer
Re: Need some php help
« Reply #11 on: June 10, 2010, 11:57:57 AM »
Due to the way your site is structured, I think that
Code: [Select]
<? include "/contact/contact-form.php"; ?>should work.

As a failsafe,
Code: [Select]
<? include "http://www.jameswylie.co.uk/pigeons/contact/contact-form.php"; ?>should (hopefully :P) definitely work.


thanks but no joy :(

http://www.jameswylie.co.uk/pigeons/contact.php

hmm

don't forget you'll have to remove a lot of stuff from that page you're including to make it work (like html headers etc.) - tbh in this case I'd just copy the form html and form handling php from that page and paste it into the page, rather than include'ing the file (the main use for includes is when you want to include the same stuff on every page)


i thought about that that but i dont know where all the code should be placed. Any suggestions welcome :)

the contact form has 3 pages in the contact folder:
contact-form.php
form-footer.php
form-header.php

I tried copying the contact-form.php code into the initial contact page but it displayed with errors all over the place.
« Last Edit: June 10, 2010, 12:00:52 PM by DevilsRefugee »
My Youtube page of goodies!
-------------------------------------

Humbug

  • Community Leaders
  • Registered Forum Member
  • ******
  • Posts: 4416
  • Karma: +36/-6
    • View Profile
Re: Need some php help
« Reply #12 on: June 10, 2010, 12:00:36 PM »
like i said i wouldnt bother copying the php stuff out of the form page.. i would use the form page as the base and add the contact info onto it.  would be far far easier
Cheers and Good Game

Humbug


DevilsRefugee

  • Contributor
  • Registered Forum Member
  • ***
  • Posts: 1099
  • Karma: +7/-1
  • Damn, I'm Good.
    • View Profile
    • James Wylie | Media Designer
Re: Need some php help
« Reply #13 on: June 10, 2010, 12:04:51 PM »
like i said i wouldnt bother copying the php stuff out of the form page.. i would use the form page as the base and add the contact info onto it.  would be far far easier
yeah but i dont want the contact form opening a new page, and if i "had" to go that route, then i would like to make the form page uniform to the site (but thats the last resort)

would it be possible that when the form opens in a new window and is then submitted the page closes and the website redirects to the thankyou page?


dammit wish i saved my old webby, i had a php contact form on there and i dont remember being this hard to get right  ;D
My Youtube page of goodies!
-------------------------------------

Humbug

  • Community Leaders
  • Registered Forum Member
  • ******
  • Posts: 4416
  • Karma: +36/-6
    • View Profile
Re: Need some php help
« Reply #14 on: June 10, 2010, 12:46:43 PM »
can you paste the original code you had on the contact.php page..

edit..

your not using target="_blank" in the link are you?  your probably not but just a question.

also your missing a
Code: [Select]
</a> on your designed by link :)
« Last Edit: June 10, 2010, 13:00:10 PM by Humbug »
Cheers and Good Game

Humbug