Jump to content

[SOLVED] Autofill a form element by clicking on a link?


madhattan

Recommended Posts

Hey guys,

 

I am new to both php and this forum, but hope to learn enough to help someone out on here one day.

 

I recently made a site that contains a contact form. It has a drop-down list of recipients that the user can choose.

 

Every page has a sidebar with a list of recipients. I would like for someone to click on a recipient and be taken to the contact page with the chosen recipient pre-filled into the dropdown box.

 

I changed all of the links to end with name=x, where x is the case # that corresponds to the person in the contact engine.

 

The site is http://www.depaulaclark.com

 

Here is the contact engine php:

switch($_POST['emailto'])
{
case '1':
$EmailTo = 'cynthia^depaulaclark.com';
break;

case '2':
$EmailTo = 'lily^depaulaclark.com';
break;

case '3':
$EmailTo = 'gina^depaulaclark.com';
break;

case '4':
$EmailTo = 'sarwar^depaulaclark.com';
break;

case '5':
$EmailTo = 'nathan^depaulaclark.com';
break;

case '6':
$EmailTo = 'jay^depaulaclark.com';
break;

case '7':
$EmailTo = 'tommy^depaulaclark.com';
break;

default:
$EmailTo = $_GET['name'];
}

 

And here is the nav on all of the pages:

 

<li><a href="http://www.depaulaclark.com/contact.php?name=1">Cynthia DePaula</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=2">Lily Clark</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=3">Gina Lasko</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=4">Sarwar Chowdhury</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=5">Nathan Sherwoon</a></li>
            <li><a href="http://www.depaulaclark.com/contact.php?name=6">Jay Rahman</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=7">Tommy Allen</a></li>

 

And here is the contact form:

 

<table>
				<tr>
					<td class="left"><label for="Name">Name:</label></td>
					<td><input type="text" name="Name" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Company">Company:</label></td>
					<td><input type="text" name="Company" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Email">Email:</label></td>
					<td><input type="text" name="Email" /></td>
				</tr>
                    <tr>
                    	<td class="left"><label for="To">To:</label></td>
                        <td><select name='emailto' id='emailto'>
							<option value="1">Cynthia DePaula</option>
							<option value="2">Lily Clark</option>
							<option value="3">Gina Lasko</option>
                                <option value="4">Sarwar Chowdury</option>
                                <option value="5">Nathan Sherwood</option>
                                <option value="6">Jay Rahman</option>
                                <option value="7">Tommy Allen</option>
						</select></td>
                    </tr>
				<tr>
					<td class="left"><label for="Message">Message:</label></td>
					<td><textarea name="Message" rows="20" cols="20"></textarea></td>
				</tr>
			</table>

 

 

Any input or feed back would be extremely helpful. I am learning a lot through this project and can't wait to see where the problem is.

Link to comment
Share on other sites

OK, tried adding to the form, but still no dice. "whatever" should be "name" right?

 

 <td><select name='emailto' id='emailto' value="<?php echo "name"; ?>">
			<option value="1">Cynthia DePaula</option>
			<option value="2">Lily Clark</option>
			<option value="3">Gina Lasko</option>
                                <option value="4">Sarwar Chowdury</option>
                                <option value="5">Nathan Sherwood</option>
                                <option value="6">Jay Rahman</option>
                                <option value="7">Tommy Allen</option>
						</select></td>

 

 

Link to comment
Share on other sites

also, here is the navigation if that helps...I wrote out the links so I wouldnt have to method get someones entire form.

 

<li><a href="http://www.depaulaclark.com/contact.php?name=1">Cynthia DePaula</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=2">Lily Clark</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=3">Gina Lasko</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=4">Sarwar Chowdhury</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=5">Nathan Sherwoon</a></li>
            <li><a href="http://www.depaulaclark.com/contact.php?name=6">Jay Rahman</a></li>
    		<li><a href="http://www.depaulaclark.com/contact.php?name=7">Tommy Allen</a></li>

 

 

Link to comment
Share on other sites

firstly

switch($_POST['emailto'])
//sseems like it should be
switch($_GET['name'])

 

also, when you have a drop down like that, if you want one selected, you have to add a selected attribute to the options.

<td><select name='emailto' id='emailto' >
            <option value="1"<?php echo ($_GET['name'] == 1) ? "Selected='selected'" : "";>Cynthia DePaula</option>

Link to comment
Share on other sites

Fixed the line and now there is no error yay! But the field is still not auto changing...

 

As of now, the form is:

<form method="post" action="contactengine.php">
			<table>
				<tr>
					<td class="left"><label for="Name">Name:</label></td>
					<td><input type="text" name="Name" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Company">Company:</label></td>
					<td><input type="text" name="Company" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Email">Email:</label></td>
					<td><input type="text" name="Email" /></td>
				</tr>
                    <tr>
                    	<td class="left"><label for="To">To:</label></td>
                        <td><select name='emailto' id='emailto' value="<?php echo "name"; ?>" />
							<option value="1" <?php echo ($_GET['name'] == 1) ? "Selected='selected'" : ""; ?>>Cynthia DePaula</option>
							<option value="2"><?php echo ($_GET['name'] == 2) ? "Selected='selected'" : ""; ?>Lily Clark</option>
							<option value="3"><?php echo ($_GET['name'] == 3) ? "Selected='selected'" : ""; ?>Gina Lasko</option>
                                <option value="4"><?php echo ($_GET['name'] == 4) ? "Selected='selected'" : ""; ?>Sarwar Chowdury</option>
                                <option value="5"><?php echo ($_GET['name'] == 5) ? "Selected='selected'" : ""; ?>Nathan Sherwood</option>
                                <option value="6"><?php echo ($_GET['name'] == 6) ? "Selected='selected'" : ""; ?>Jay Rahman</option>
                                <option value="7"><?php echo ($_GET['name'] == 7) ? "Selected='selected'" : ""; ?>Tommy Allen</option>
						</select></td>
                    </tr>
				<tr>
					<td class="left"><label for="Message">Message:</label></td>
					<td><textarea name="Message" rows="20" cols="20"></textarea></td>
				</tr>
			</table>

 

And here is the engine:

 

switch($_GET['name'])
{
case '1':
$EmailTo = 'cynthia@depaulaclark.com';
break;

case '2':
$EmailTo = 'lily@depaulaclark.com';
break;

case '3':
$EmailTo = 'gina@depaulaclark.com';
break;

case '4':
$EmailTo = 'sarwar@depaulaclark.com';
break;

case '5':
$EmailTo = 'nathan@depaulaclark.com';
break;

case '6':
$EmailTo = 'jay@depaulaclark.com';
break;

case '7':
$EmailTo = 'tommy@depaulaclark.com';
break;

default:
$EmailTo = $_GET['name'];
}

 

 

Link to comment
Share on other sites

well firstly, that echo must be inside the option tags, like

<option value="2"><?php echo ($_GET['name'] == 2) ? "Selected='selected'" : ""; ?>Lily Clark</option>

should be

<option value="2" <?php echo ($_GET['name'] == 2) ? "Selected='selected'" : ""; ?>>Lily Clark</option>

 

secondly, I don't really see where that switch gets used for anything... nor the variable $emailTo, or why that forms action is set to contactengine.php.... what page sets the get variable 'name'?

Link to comment
Share on other sites

OK, so now the links are changing the field like they should, but the form will no longer submit.

Upon submitting, the user is sent to an error.htm page.

 

The uses recaptcha to block spam.

 

Here is the current full code for the contact form...

<form method="post" action="contactengine.php">
			<table>
				<tr>
					<td class="left"><label for="Name">Name:</label></td>
					<td><input type="text" name="Name" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Company">Company:</label></td>
					<td><input type="text" name="Company" /></td>
				</tr>
				<tr>
					<td class="left"><label for="Email">Email:</label></td>
					<td><input type="text" name="Email" /></td>
				</tr>
                    <tr>
                    	<td class="left"><label for="To">To:</label></td>
                        <td><select name='emailto' id='emailto' value="<?php echo "name"; ?>" />
							<option value="1"<?php echo ($_GET['name'] == 1) ? "Selected='selected'" : ""; ?>>Cynthia DePaula</option>
							<option value="2"<?php echo ($_GET['name'] == 2) ? "Selected='selected'" : ""; ?>>Lily Clark</option>
							<option value="3"<?php echo ($_GET['name'] == 3) ? "Selected='selected'" : ""; ?>>Gina Lasko</option>
                                <option value="4"<?php echo ($_GET['name'] == 4) ? "Selected='selected'" : ""; ?>>Sarwar Chowdury</option>
                                <option value="5"<?php echo ($_GET['name'] == 5) ? "Selected='selected'" : ""; ?>>Nathan Sherwood</option>
                                <option value="6"<?php echo ($_GET['name'] == 6) ? "Selected='selected'" : ""; ?>>Jay Rahman</option>
                                <option value="7"<?php echo ($_GET['name'] == 7) ? "Selected='selected'" : ""; ?>>Tommy Allen</option>
						</select></td>
                    </tr>
				<tr>
					<td class="left"><label for="Message">Message:</label></td>
					<td><textarea name="Message" rows="20" cols="20"></textarea></td>
				</tr>
			</table>

<div id="captcha-area">
			<?php
			require_once('recaptchalib.php');
			$publickey = "-------------------------------------------------";
			$privatekey = "-----------------------------------------------";

			# the response from reCAPTCHA
			$resp = null;
			# the error code from reCAPTCHA, if any
			$error = null;

			# are we submitting the page?
			if ($_POST["submit"]) {
			  $resp = recaptcha_check_answer ($privatekey,
											  $_SERVER["REMOTE_ADDR"],
											  $_POST["recaptcha_challenge_field"],
											  $_POST["recaptcha_response_field"]);

			  if ($resp->is_valid) {
				echo "You got it!";
				# in a real application, you should send an email, create an account, etc
			  } else {
				# set the error code so that we can display it. You could also use
				# die ("reCAPTCHA failed"), but using the error message is
				# more user friendly
				$error = $resp->error;
			  }
			}
			echo recaptcha_get_html($publickey, $error);
			?>

</div>

			<input type="submit" name="submit" value="Submit" class="submit-button" />
		</form>

 

and here is the code for the contactengine.php...

 

<?php

require_once('recaptchalib.php');
$privatekey = "6LcqrwcAAAAAANr7jMoEZ56UajiN08yZ4ouOCUKV";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}

switch($_GET['name'])
{
case '1':
$EmailTo = 'cynthia^depaulaclark.com';
break;

case '2':
$EmailTo = 'lily^depaulaclark.com';
break;

case '3':
$EmailTo = 'gina^depaulaclark.com';
break;

case '4':
$EmailTo = 'sarwar^depaulaclark.com';
break;

case '5':
$EmailTo = 'nathan^depaulaclark.com';
break;

case '6':
$EmailTo = 'jay^depaulaclark.com';
break;

case '7':
$EmailTo = 'tommy^depaulaclark.com';
break;

default:
$EmailTo = $_GET['name'];
}

$EmailFrom = "contact^depaulaclark.com";
$Subject = "D & C Contact Form Submission";
$Name = Trim(stripslashes($_POST['Name'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

 

 

Link to comment
Share on other sites

recaptcha and the email was working before I changed the form code to

 

<option value="1"<?php echo ($_GET['name'] == 1) ? "Selected='selected'" : ""; ?>>Cynthia DePaula</option>

etc

 

the recaptcha code and the contact engine code have not been changed since they worked last.

 

This is being triggered now in the contact engine and Im not sure why...

 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

 

because after you submit the form on depaulaclark.com it goes to the error.htm page (which is 404 cuz i havent made it...

Link to comment
Share on other sites

This is being triggered now in the contact engine and Im not sure why...

 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

I don't see how that's being triggered, the if statement will always be false!

which means the email is failing!

 

in the email part, change

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

to

print "<meta http-equiv=\"refresh\" content=\"0;URL=error2.htm\">";

and see if it goes to error2.htm,

Link to comment
Share on other sites

Okay you have a problem with your email, first correct the email address, to valid emails, you may want to echo our the $Body to check that as well

 

you might as well comment out or remove

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

as it's not doing anything

 

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.