The contact form has several text fields and several dropdowns with the ability to select multiple values. The question is how to send these values by e-mail? Text values of type string (one word at a time) are sent fine. But how to send arrays containing multiple values? How to collect them using the $_POST method and put them in a letter?
Form:
<section class="post-content-area pt-20">
<div class="container">
<div class="row">
<div class="col-lg-8 posts-list">
<div class="card card-signin my-5">
<div class="card-body">
<h5 class="card-title text-center" style="font-size: 26px">Test contact form</h5>
<form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Name" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="surname" id="surname" class="form-control" placeholder="Surame" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone" >
<div id="subjectError" style="color: red;font-size: 14px;display: none">subjectError</div>
</div>
<div class="form-group">
<label>First Level Category</label><br />
<select id="first_level" name="first_level[]" multiple class="form-control">
<?php
foreach($result as $row)
{
echo '<option value="'.$row["first_level_category_id"].'">'.$row["first_level_category_name"].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Second Level Category</label><br />
<select id="second_level" name="second_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<label>Third Level Category</label><br />
<select id="third_level" name="third_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<input type="file" name="attachment" id="attachment" class="form-control">
<div id="attachmentError" style="color: red;font-size: 14px;display: none">attachmentError</div>
</div>
<div class="submit">
<center><input type="submit" name="submit" onclick="return validateEmailSendForm();" class="btn btn-success" value="SUBMIT"></center>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
Emailing script:
https://pastebin.com/3SR67MUP
There are multiple ways on how to send them via email.
The easiest way is getting them on your form action page and sending them with the php mail() function.
To make the email look better you can create an HTML email template file.
Then get the file data with file_get_contents() and resetting strings with your collected data with the str_replace function and putting the data into your mail funtion.
Hope it helps!
Related
I am working on invoice where I have to save form's data in database and at the same time it should be print. So, I have 3 pages: invoice.php, invoice_print.php and insert_data.php
invoice_print.php is a html form that should be printed.
Now, user will go to first invoice.php then he will fill details and either he will click on submit button to save data in db or else he will click on print button to print that invoice.
Now, lets come to the second part: If user will select print button then data will go to database first and then it will go to invoice_print.php with same data that he filled.
How to do this? What logic should I use to save data and capture that data's id in button and then send that id on another page to display?
invoice.php:
<form id="demo-form2" action="insert_data.php" method="post" data-parsley-validate class="form-horizontal form-label-left">
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
</form>
invoice_print.php:
<div class="col-md-12">
<div class="col-md-6">
<h2>DIGILIFE BIZCARE SOLUTIONS</h2>
<p>414, Vashi Infotech Park,Maharashtra</p>
</div>
<div class="col-md-6">
<h2>BILL OF SUPPLY</h2>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>GSTIN</label>
<input type="text" name="gstin" value="">
<label>Serial No & Date of Invoice</label>
<input type="text" name="serialNo">
</div>
<div class="col-md-6">
</div>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>Mode of Transport</label><br>
<label>Vehicle No</label><br>
<label>Date & Time of Supply</label><br>
<label>Place Of Supply</label>
</div>
<div class="col-md-6s">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
</div>
</div>
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
I would put all of the code for a form into one file, structured thus:
<?php
$formDone=!empty($_POST['formDone']);
$printReq=!empty($_POST['printReq']);
if ($formDone)
{
// perform validation
}
if ($formDone && $validationPassed)
{
// write to database using $_POST data
}
if ($formDone && $databaseWriteSuccess && $printReq)
{
// print using $_POST data
}
// end of PHP
?>
<form method="post" action="<?php echo $PHP_SELF ?>">
<fieldset>
<input type="hidden" name="formDone" value="1" />
</fieldset>
<!-- field forms -->
<input type="submit" value="Save to db" />
<input type="submit" name="printReq" value="Print Invoice" />
</form>
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="store.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
Here, in this code there are more input types, I just skipped those. When I will press submit button this will take me to 'store.php' file where informations will be stored in database. And redirect me to another form and there are also some input type but I need a information like "reg_no" from the previous form in the current form. How can I get this?
NOTE : I am storing the inputs of first form in a database table. Then redirected to other form and the action of this form is another PHP file like 'store2.php' and it is also using to store informations to other table of database.
You could pass the value of reg_no as a
$_SESSION Variable
to the second form and get it there
You said that you are already passing the information from your current form into a database. When rendering your second form, you could grab whatever value you need from your database, and then add a value attribute to your input.
So for example if you have a form that looks like this in your second form:
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
You can add that class to Pre-Fill the form when the page loads. Lets say you want it to show "12345":
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="12345">
I don't know the details of exactly how you are serving your site through PHP, but you could use some kind of string placeholder, or templating engine to put in this attribute.
Maybe this can help you :
this called form.php :
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No.</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
this process of form.php, called form_p.php :
<?php
$reg_date = $_POST['reg_date'];
$reg_no = $_POST['reg_no'];
$submit = $_POST['submit'];
if (isset($submit)) {
$url = 'form_next.php?reg_date=' . $reg_date . '®_no=' . $reg_no;
header('Location:' . $url);
}
this next form, called form_next.php :
<?php
$reg_date = $_GET['reg_date'];
$reg_no = $_GET['reg_no'];
?>
<form class="form-horizontal" enctype="multipart/form-data" method="POST" action="form_p_next.php">
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION DATE AGAIN</label>
<div class="col-sm-8">
<input type="date" class="form-control1" name="reg_date" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_date; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">REGISTRATION No. AGAIN</label>
<div class="col-sm-8">
<input type="text" class="form-control1" name="reg_no" id="focusedinput" placeholder="Default Input" value="<?php echo $reg_no; ?>">
</div>
<div class="col-sm-2">
<p class="help-block">text!</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</div>
</div>
</form>
and this is process of form_next.php, called form_next_p.php
<?php
//define your code
The code work because I passing reg_date and reg_no from form_p.php to form_next.php and I use $_GET to get reg_date and reg_no and print it in input value.
This is help you?
I've read the other answers to my question regarding combining both the select and text fields, but when I try to apply any of the solutions to my form it does not work.
Can someone modify my form so that you end up with a dropdown containing both the select options and a text input field?
Please include the appropriate jquery script as well.
Here is my form code:
<form id="ajax-contact-form" action="drumming_for_jared_registration.php" method="post">
<div class="row">
<div class="span12">
<div class="control-group">
<label class="control-label" for="inputName">Date:</label>
<div class="controls">
<input class="span4" type="text" id="inputName" name="Date" placeholder="Date:" style="color:white" onBlur="if(this.value=='') this.value=''" onFocus="if(this.value =='Date:' ) this.value=''">
</div>
</div>
</div>
<div class="row"></div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputName">New To Studio?</label>
<div class="controls">
<select input class="span14" type="text" id="inputLevel" name="New_to_Studio">
<option>New To Studio?</option>
<option>Yes</option>
<option>No</option>
</select>
</div>
</div>
</div>
<div class="row"></div>
</div>
<button type="submit" class="submit">Send</button>
</form>
I am working on a website which I didn't create and to which I do not have full access (I can create a page and add html scripts but not much more)
I am trying to add a form which will send data to be processed by a third-party servlet.
I am trying to include in this form the client ID, which I saw can be retrieved using the php functionsession_id().
My question is, how to send this information along with the rest of the input data.
my form for now is this:
<form class="form-horizontal" action = "url" method="POST" target="_blank">
<fieldset>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Culture</label>
<div class="col-md-4">
<select id="Crop" name="Crop" class="form-control">
<option value="1">Maïs</option>
<option value="2">Ble</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Produit</label>
<div class="col-md-4">
<select id="Product" name="Product" class="form-control">
<option value="Iodure d'azote">Iodure D'azote</option>
<option value="Desherbant">Desherbant</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="area">Surface</label>
<div class="col-md-4">
<input id="Area" name="Area" type="text" placeholder=" " class="form-control input-md" required="">
<span class="help-block">en Ha</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dose">Dosage</label>
<div class="col-md-4">
<input id="Dose" name="Dose" type="text" placeholder="" class="form-control input-md" required="">
<span class="help-block">en L/Ha</span>
</div>
</div>
<div class="form-group">
<!--need to get the actual id-->
<input type="hidden" value="1" name = "ID"/>
<input type="submit" value="Ajouter" />
</div>
</fieldset>
</form>
Since this is my first time handling web apps, is there something obvious i'm missing?
OK i was lacking basic understanding of php. sorry for bothering,
the answer was here
i just had to write
<input type="hidden" value="<?php echo session_id();?>" name = "ID"/>
instead of
<input type="hidden" value="1"name = "ID"/>
I have a form like below:
<form id="bookForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-1 control-label">Book</label>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0].title" placeholder="Title" />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0].isbn" placeholder="ISBN" />
</div>
<div class="col-xs-2">
<input type="text" class="form-control" name="book[0].price" placeholder="Price" />
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
</div>
</div>
How to get value of this form in php $_Post because the name of fields are like <br/>name="book[0].title"**<br/>?
and there are many dynamic different name input fields.
If you can use for multiple books then use book[0].title like below,
<label class="col-xs-1 control-label">Book</label>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0][title]" placeholder="Title" />
</div>
If you want to use same form then change name of all textbox like,
<form id="bookForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-1 control-label">Book</label>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0][title]" placeholder="Title" />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="book[0][isbn]." placeholder="ISBN" />
</div>
<div class="col-xs-2">
<input type="text" class="form-control" name="book[0][price]" placeholder="Price" />
</div>
<div class="col-xs-1">
<button type="submit" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
</div>
</div>
And get value of this form like echo $_POST['book'][0]['title'];
I hope this can help you.
You need to change name attribute,
<div class="col-xs-4">
<input type="text" class="form-control" name="book_title" placeholder="Title" />
</div>
book[0].title, this is not valid name.
You can now get something like this,
$_POST['book_title']