PHP contact form with custom answer based on checkbox - php

I'm trying to build a php contact form (to be used into a wordpress custom page) that show a different "thank you message" based on the checkbox selection.
There will be three checkbox, every checkbox in used will make appear a download link under the "thank you" message.
<form method="post" action="php/contact-send.php" id="contact_form">
<p><label class="form_label" for='form_name'>Name</label>
<input id='form_name' type='text' name='name' class="textbox" value='' /></p>
<p><label class="form_label" for='form_email'>E-mail</label>
<input id='form_email' type='text' name='email' class="textbox" value='' /></p>
<p>Download1 <input id='form_name' type='checkbox' name='Download1' class="textbox" value='Download1' /></p>
<input id='form_submit' type='submit' name='submit' value='Send message' />
<!-- To Email -->
<input id='to_email' type='hidden' name='to_email' value='<?php echo "";?>' />
<!-- hidden input for basic spam protection -->
<div class='hide'>
<label for='spamCheck'>Do not fill out this field</label>
<input id="spamCheck" name='spam_check' type='text' value='' />
</div>
</form>
<!-- contact form ends here-->
<!-- This div will be shown if mail was sent successfully -->
<div class="hide success">
<p><?php echo thanks_message;?></p>
<p>
<?php
if(isset($_POST['Download1'])) {
// The checkbox was enabled...
echo "Link download 1";}
else { echo "much wrong, wow";}
?>
</p>
The problem is i can't get it to work and detect when the first checkbox is selected, it alway give me the else message even if i select it. I'm kinda new with php and i don't really get what i'm doing wrong,
Thanks in advance for any help

Related

php pre-populating text area

The following php code works for prepopulating an input box, but doesn't work for a text area. The issue is that the standard input box is small in height so the text is displayed on one line. I tried to use the text area instead but it appears blank when rendered. The code shows both methods, the working method, followed by the text area method.
Can anyone advise?
<?php
echo "<form method='POST' action='edit.php'>
<fieldset>
<div class='form-group'>
<label for='descriptionField'>Description</label>
**<input type='text' value='$description' class='form-control' name ='nameField'>**
**<textarea class='form-control'value='$description' input type='text' rows='10' name ='descriptionField'></textarea>**
<input type='hidden' value='$id_data' name='idField'>
<br>
<input class='btn btn-success' type='submit' value='update'>
</div>
</fieldset>
</form>";
}
?>
Did you try like this?
<?php
echo "<form method='POST' action='edit.php'>
<fieldset>
<div class='form-group'>
<label for='descriptionField'>Description</label>
**<input type='text' value='$description' class='form-control' name ='nameField'>**
**<textarea class='form-control' input type='text' rows='10' name ='descriptionField'>htmlspecialchars($description);</textarea>**
<input type='hidden' value='$id_data' name='idField'>
<br>
<input class='btn btn-success' type='submit' value='update'>
</div>
</fieldset>
</form>";
}
?>
If you want to set a text areas input, you can simply put in in between the <textarea> tags like so in your example:
<textarea class='form-control' input type='text' rows='10' name ='descriptionField'>
$description
</textarea>
Also refer to the w3schools article on the textarea tag for further details.

Adding a subject line to PHP form

So I've put a contact form into my site but I can't seem to add a subject line. (I'm SUPER new to PHP so I'm only about 40% sure of what I'm doing. The rest of the time I'm just learning through trial and error).
Here's what I have for the form:
<?PHP
require_once("./include/fgcontactform.php");
$formproc = new FGContactForm();
$formproc->AddRecipient('email address');
$formproc->AddSubject('Website Communication:');
$formproc->SetFormRandomKey('boQQEtSLenwppBa');
if(isset($_POST['submitted']))
{
if($formproc->ProcessForm())
{
$formproc->RedirectToURL("thank-you.php");
}
}
?>
<!-- Form Code Start -->
<form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>Contact us</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
<input type='text' class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />
<div class='short_explanation'>* required fields</div>
<div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='email' >Email Address*:</label><br/>
<input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
<span id='contactus_email_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='message' >Message:</label><br/>
<span id='contactus_message_errorloc' class='error'></span>
<textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
</div>
<div class='container'>
<input type='submit' name='Submit' value='Submit' />
</div>
</fieldset>
</form>
I added the $formproc->AddSubject('Website Communication:'); segment but it doesn't seem to be working and I'm wondering if there's something I'm missing?
What you're really asking is how to use a specific library class that you've downloaded from the Internet! If you look into the code of the class you're including at the top (which I assume is the same as this?) then you'll see it's automatically generating a subject line here:
$this->mailer->Subject = "Contact form submission from $this->name";
The reason your AddSubject function doesn't work is because that behaviour doesn't exist within the class.
If you want to change this behaviour, you'll have to change the class. You'll want to change the above line to something like
$this->mailer->Subject = $this->getSubject();
and implement a getSubject() method, similar to the GetFromAddress() one.
Better yet, create your own mailer class! Look into the PHP mail function to get started!

how to validate a form that uses 'action' by php server-side

I uses this from to submit some information to '2co' as a payment method some of this information I need to validate it by PHP server-side and in the same time send a notification to the admin by mail to tell him that someone has been paid.
now this is the form.
<form method="post" action="https://sandbox.2checkout.com/checkout/purchase">
<div class="body">
<div class="half f_right">
<label for="card_holder_name" class="width_100per"><span class="fontRed2">*</span>card_holder_name
</label>
<input type="text" name="card_holder_name" id="card_holder_name"
class="form_textarea_rec"/>
</div>
<div class="half">
<label for="country" class="width_100per"><span class="fontRed2">*</span>country</label>
<select name="country" id="country" class="form_select_rec">
<option value="EGY" title="Egypt">Egypt</option>
<option value="SaudiArabia" title="saudi Arabia">Any other</option>
</select>
</div>
<div class="half">
<label for="street_address" class="width_100per"><span class="fontRed2">* </span>street_address </label>
<textarea name="street_address" id="street_address"
class="form_textarea_rec height_50px"></textarea>
</div>
<div class="half">
<label for="street_address2" class="width_100per"> address2</label>
<textarea name="street_address2" id="street_address2"
class="form_textarea_rec height_50px"></textarea>
</div>
<div class="half f_right">
<label for="city" class="width_100per"><span class="fontRed2">* city</span>
</label>
<input id="city" type="text" name="city" class="form_textarea_rec"/>
</div>
<div class="half f_right">
<label for="state" class="width_100per"><span class="fontRed2">*</span>
States</label>
<input id="state" type="text" name="state" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="email" class="width_100per"> <span class="fontRed2">*</span>Email</label>
<input id="email" type="text" name="email" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="zip" class="width_100per"> <span class="fontRed2">*</span>Zip</label>
<input id="zip" type="text" name="zip" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="phone" class="width_100per"> <span class="fontRed2">*</span>
Phone</label>
<input id="phone" type="text" name="phone" class="form_textarea_rec"/>
</div>
</div>
<?php
$fullName = #$_POST['card_holder_name'];
?>
<input type='hidden' name='sid' value='*****'/>
<input type='hidden' name='mode' value='2CO'/>
<input type='hidden' name='li_0_type' value='product'/>
<input type='hidden' name='li_0_name' value='<?php echo $planName ?> '/>
<input type='hidden' name='li_0_price' value='<?php echo $planPrice ?> '/>
<input type='hidden' name='card_holder_name' value='<?php echo $fullName ?>'/>
<input type='hidden' name='street_address' value='<?php #$_POST['street_address']; ?>'/>
<input type='hidden' name='street_address2' value='<?php #$_POST['street_address2']; ?>'/>
<input type='hidden' name='city' value='<?php #$_POST['city']; ?>'/>
<input type='hidden' name='state' value='<?php #$_POST['state']; ?>'/>
<input type='hidden' name='zip' value='<?php #$_POST['zip']; ?>'/>
<input type='hidden' name='country' value='<?php #$_POST['country']; ?>'/>
<input type='hidden' name='email' value='<?php #$_POST['email']; ?>'/>
<div class="footer">
<input name="submit" type="submit" value="Continue" class="s_btn"/>
</div>
</form>
now I need to validate this fields and send the form and I need to send notes to the admin
I know about make it like this
<?php
if(isset($_POST['submit'])){
//remove the action from the form
//write the validation here ...
//and then send the mail
//but how to post all this in the end to '2co'??
}
?>
You could use curl to finally post it to 2co http://php.net/manual/en/book.curl.php
Sending mail is as simple as using mail function http://php.net/manual/en/function.mail.php
<?php
if(isset($_POST['submit'])){
//Do validation
//send mail
//post to 2co via curl
}
?>
As said in this question there are non curl ways to do so How do I send a POST request with PHP?
you are asking the wrong question, you should have asked "how to post data to another server using php?"
there are many answers here are some :
How do I send a POST request with PHP?
http://www.lornajane.net/posts/2010/three-ways-to-make-a-post-request-from-php
You need to do it by JavaScript on page load event.
Changes you need to do:
change action to same php page OR different AND check submit action by if(isset($_POST['submit'])) as you said.
validate all data, send mail, set php var $validated=true else false AND set some session variable which you should validate to confirm, on response from payment server. Ex: payment_id, user_id | these should also be sent to payment gateway and gateway returns on response, in your case I think sid
if($validated) echo the JavaScript code to create a hidden form with all information in hidden input field and submit form automatically on page load event.
on response from payment server, which will come on your specific php page validate session/user info.
If you are using jquery the autosubmit code will be like following:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- if you are not using jQuery and want to then use above code -->
<?php if($validated) { ?> <script type="text/javascript">
$(function(){
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", link);
form.attr("style","display: none");
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", "post_var1");
field.attr("value", "val_1");
form.append(field); //echo values by php <?php echo $value1; ?> like:
var field1 = $('<input />');
field1.attr("type", "hidden");
field1.attr("name", "li_0_name");
field1.attr("value", "<?php echo $planName ?>");
form.append(field1);
//do same for all fields
$(document.body).append(form);
form.submit();
}
</script> <?php } ?>
For non jQuery (Pure JavaScript) code you can search OR ask me i'll edit the answer.

reset form php value back to original value php

how to reset value with jquery form..my input text value with php. when I edit text and I reset, then back again to the value php..I've tried with jquery reset but input text
lost value.
<form>
<input type='text' id='chk' value='<?php echo $item.$i?>'/><br/>
<input type='text' value='<?php echo $item.$i?>'/><br/>
<input type='text' value='<?php echo $item.$i?>'/><br/>
<input type='reset' id='reset_button' value='reset'/>
</form>
when i edit input text and then i click reset.input text losing value?how to input text back to original value?
thanks..
You could use a form reset button: <input type='reset' value='Reset'/>
If you don't want the button to be visible, you could hide the button and call the click() method to reset the form.
Example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>With button:</h2>
<form>
<input type='text' value='Foo'/><br/>
<input type='text' value='Bar'/><br/>
<input type='reset' value='Reset'/>
</form>
<br/>
<br/>
<h2>Without button:</h2>
<form>
<input type='text' id='chk' value='1'/><br/>
<input type='text' value='Foo'/><br/>
<input type='text' value='Bar'/><br/>
<input type='reset' id='reset_button' style='display: none;'/>
<input type='submit' onclick="if ($('#chk').val() == '0') $('#reset_button').click(); return false;" value='Check if 1 (reset if 0)'/>
</form>

Rerender Html page after PHP function call

I currently have this login page.
<form action='http://localhost:8080/iSchedj/API/api.php' method='post'>
<fieldset>
<p>
<label for='username'>Username</label>
<input type='text' id='username' name='username' value='' maxlength='20' />
</p>
<p>
<label for='password'>Password</label>
<input type='password' id='password' name='password' value='' maxlength='40' />
</p>
<p>
<input type='hidden' name='form_token' value='<?php echo $form_token; ?>' />
<input type='submit' name='login' id='login' value='Login' />
</p>
</fieldset>
</form>
On submit, I have a function on the server-side where I analyze the data and check if there are any issues.
Problem is, I would like to send a response back to the client and re render the login page if any issues occurred with a response saying 'Error on login'.
I am having an issue with the response part.
On the server side, I just run this when an error is found in the form
header( 'Location: http://localhost:8080/iSchedj/index.php');
echo 'Incorrect Length for Password';
which redirects the client to the login page. But how do I send the error message to the page as well? The echo message is no where to be found.
Add it to the URL and display your error message via JavaScript.
header( 'Location: http://localhost:8080/iSchedj/index.php#error=1');
Your JS script will need to look at the document.location.hash and behave accordingly.

Categories