I am new to Wordpress.I stuck in a problem where i want to Submit a form and also want data insertion in the wpdb in the same page.
Now i have two different pages one is for submit the form and another is to insert the data in the mysql db respectively.
I want to do the above process of form submission and data insertion in the same page.
Below are the codes:
Below is the code for a form which will redirect to another page for data insertion.
<!DOCTYPE html>
<html>
<style>
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
<body>
<h3>Web Design On-line Requisition Form</h3>
<div>
<form action="example.com?page_id=345" method="post" name="webForm">
<label for="cname">Company/Institute Name</label>
<input type="text" id="cname" name="cname">
<label for="pname">Your Name</label>
<input type="text" id="pname" name="pname">
<label for="webtype">Type of Website</label>
<select id="webtype" name="webtype">
<option value="Personal(Static Pages)">Personal(Static Pages)</option>
<option value="Personal(Dynamic Page)">Personal(Dynamic Page)</option>
<option value="Commercial(Static Pages)">Commercial(Static Pages)</option>
<option value="Commercial(Dynamic Page)">Commercial(Dynamic Page)</option>
<option value="Online Shopping Site">Online Shopping Site</option>
<option value="Educational Site">Educational Site</option>
<option value="Other">Other</option>
</select>
<label for="contact">Contact No.</label>
<input type="text" id="contact" name="contact">
<label for="email">Email ID</label>
<input type="text" id="email" name="email">
<label for="address">Address</label>
<input type="text" id="address" name="address">
<label for="pappdate">Preferred Appointment Date</label>
<input type="date" name="pappdate">
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
This is another page where i wrote the code for data insertion to the mysql using wpdb
[insert_php]
$cname=$_POST['cname'];
$pname=$_POST['pname'];
$webtype=$_POST['webtype'];
$contact=$_POST['contact'];
$email=$_POST['email'];
$address=$_POST['address'];
$pappdate=$_POST['pappdate'];
global $wpdb;
$wpdb->insert(
'sometable',
array(
'cname' => $cname,
'pname' => $pname,
'webtype' => $webtype,
'contact' => $contact,
'email' => $email,
'address' => $address,
'pappdate' => $pappdate
),
array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
[/insert_php]