Reputation: 45
I am new to PHP MYSQL JQUERY and I have a Jquery Mobile Form that i want to submit the form data to mySql then change to #pageTwo of the mobile page. This seems like it should be easy to do but i cant seem to figure it out.
<body>
<div data-role="page" id="pageone">
<?php include 'connectDB.php' ?>
<div data-role="header">
<h1>Order Request Form</h1>
</div>
<div data-role="main" class="ui-content">
<div class="ui-field-contain">
<form method="post" id="ACform" name="ACform">
<div>
<label for="onSiteName" class="ui-hidden-accessible">On Site Contact:</label>
<input type="text" name="onSiteName" id="onSiteName" data-clear-btn="true" placeholder="On Site Contact name..." required>
</div>
<div>
<label for="onSiteNumber" class="ui-hidden-accessible">On Site Contact Number:</label>
<input type="tel" name="onSiteNumber" id="onSiteNumber" data-clear-btn="true" placeholder="On Site Contact number..." required>
</div>
<div>
<label for="adminContactName" class="ui-hidden-accessible">Administrator Contact:</label>
<select name="adminContactDropDown" id="adminContactDropDown" required>
<option value="">Admin Contact</option>
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="Other">Other</option>
</select>
</div>
<div>
<input type="text" name="adminContactName" id="adminContactName" data-clear-btn="true" placeholder="Administrator Contact name..." required>
<label for="adminContactNumber" class="ui-hidden-accessible">Administrator Contact Number:</label>
<input type="tel" name="adminContactNumber" id="adminContactNumber" data-clear-btn="true" placeholder="Administrator Contact number..." required>
</div>
<script>
$('#ACform').validate({
messages: {
onSiteName: {required: "Please enter the on-site contact name"},
onSiteNumber: {required: "Please enter the on-site contacts number"},
adminContactDropDown: {required: "Please select an admin contact"},
adminContactName: {required: "Please enter an admin contact name"},
adminContactNumber: {required: "Please enter an admin contact number"},
},
errorPlacement: function (error, element) {
error.insertAfter($(element).parent());
},
submitHandler: function (form) {
form.submit();
':mobile-pagecontainer'.pagecontainer('change', '#pagetwo', {
reload: false
});
return false;
}
});
</script>
<?php include 'submit.php' ?>
<div>
<input type="reset" name="reset" value="Reset" id="clear">
<input type="submit" name="submit" value="Submit"><br/>
</div>
</form>
</div>
</div>
<div data-role="footer">
<h1><img src="logo.png" align="middle" width="160" height="26"/></h1>
</div>
</div>
<!--Page two-->
<div data-role="page" data-dialog="true" id="pagetwo">
<div data-role="header">
<h1>A/C Order Request Sent!</h1>
</div>
<div data-role="main" class="ui-content">
<?php echo $output; ?>
<p>Thank you for submitting your order request. Your request will be reviewed by one of our sales representatives and you will be contacted shortly.</p>
<div align="right"><a href="#pageone" class="ui-btn ui-btn-inline ui-shadow ui-corner-all ui-icon-arrow-r ui-btn-icon-right">Place another order</a></div>
</div>
<div data-role="footer">
<h1><img src="logo.png" align="middle" width="160" height="26"/></h1>
</div>
</div>
</body>
</html>
submit.php
<?php $jobName = $delDate = $delTimeFrom = $useDate = $startTime = $puDate = $puTimeFrom = $address = $address2 = $zip = $city = $state = $delArea = $onSiteName = $onSiteNumber = $adminContactName = $adminContactNumber = $ACtotal = '';
if ($_POST) {
$onSiteName = test_input($_POST["onSiteName"]);
$onSiteNumber = test_input($_POST["onSiteNumber"]);
$adminContactName = test_input($_POST["adminContactName"]);
$adminContactNumber = test_input($_POST["adminContactNumber"]);
$sql = "INSERT INTO tbl_orders (onSiteName , onSiteNumber , adminContactName , adminContactNumber)
VALUES ('$onSiteName' , '$onSiteNumber' , '$adminContactName' , '$adminContactNumber')";}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;}
$output = '';
if ($conn->query($sql) === TRUE) {
$output .= "New record created successfully";
} else {
$output .= "Error: " . $sql . "<br>" . $conn->error;
}
?>
I have tried to remove the submitHandler, it submits the form properly but it doesnt change the page and disrupts all the other javascript on the page (not shown) when it reloads. But when i have the submitHandler in, the page changes but the form doesn't submit.
Based on Michaels suggestion, i tried the below code which gave me an "error loading page" error:
submitHandler: function (form) {
$(':mobile-pagecontainer').pagecontainer('change', '#pagetwo', {
reload: false
});
return false;
}
form.submit();
});
I also tried:
submitHandler: function (form) {
$(':mobile-pagecontainer').pagecontainer('change', '#pagetwo', {
reload: false
});
return false;
form.submit();
}
});
which took me to pageTwo but did not submit the form
Upvotes: 0
Views: 140
Reputation: 2046
When you use the submitHandler
method, you then need to submit the form. Check here: http://jqueryvalidation.org/documentation/.
$("#myform").validate({
submitHandler: function(form) {
form.submit();
}
});
EDIT
For sake of clarity, I've removed the first edit.
EDIT #2
Here's the HTML:
<html>
<!-- Added the head just to make sure everything is included -->
<head>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<!-- Page One -->
<div data-role="page" id="pageone">
<div data-role="header">
<h1>Order Request Form</h1>
</div>
<div data-role="main" class="ui-content">
<div class="ui-field-contain">
<form method="post" id="ACform" name="ACform">
<div>
<label for="onSiteName" class="ui-hidden-accessible">On Site Contact:</label>
<input type="text" name="onSiteName" id="onSiteName" data-clear-btn="true" placeholder="On Site Contact name..." required>
</div>
<div>
<label for="onSiteNumber" class="ui-hidden-accessible">On Site Contact Number:</label>
<input type="tel" name="onSiteNumber" id="onSiteNumber" data-clear-btn="true" placeholder="On Site Contact number..." required>
</div>
<div>
<label for="adminContactName" class="ui-hidden-accessible">Administrator Contact:</label>
<select name="adminContactDropDown" id="adminContactDropDown" required>
<option value="">Admin Contact</option>
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="Other">Other</option>
</select>
</div>
<div>
<input type="text" name="adminContactName" id="adminContactName" data-clear-btn="true" placeholder="Administrator Contact name..." required>
<label for="adminContactNumber" class="ui-hidden-accessible">Administrator Contact Number:</label>
<input type="tel" name="adminContactNumber" id="adminContactNumber" data-clear-btn="true" placeholder="Administrator Contact number..." required>
</div>
<div>
<input type="reset" name="reset" value="Reset" id="clear">
<input type="submit" name="submit" value="Submit"><br/>
</div>
</form>
</div>
</div>
<div data-role="footer">
<h1>
<img src="logo.png" align="middle" width="160" height="26"/>
</h1>
</div>
</div>
<!--Page two-->
<div data-role="page" data-dialog="true" id="pagetwo">
<div data-role="header">
<h1>A/C Order Request Sent!</h1>
</div>
<div data-role="main" class="ui-content">
<div id="dbOutput"></div> <!-- Added as placeholder for DB output -->
<p>Thank you for submitting your order request. Your request will be reviewed by one of our sales representatives and you will be contacted shortly.</p>
<div align="right">
<a href="#pageone" class="ui-btn ui-btn-inline ui-shadow ui-corner-all ui-icon-arrow-r ui-btn-icon-right">Place another order</a>
</div>
</div>
<div data-role="footer">
<h1>
<img src="logo.png" align="middle" width="160" height="26"/>
</h1>
</div>
</div>
</body>
</html>
And the JavaScript:
<script>
$('#ACform').validate({
messages: {
onSiteName: {required: "Please enter the on-site contact name"},
onSiteNumber: {required: "Please enter the on-site contacts number"},
adminContactDropDown: {required: "Please select an admin contact"},
adminContactName: {required: "Please enter an admin contact name"},
adminContactNumber: {required: "Please enter an admin contact number"},
},
errorPlacement: function (error, element) {
error.insertAfter($(element).parent());
},
submitHandler: function (form) {
var data = $(form).serialize();
$.ajax({
type: 'POST',
url: 'submit.php',
data: data,
success: function (response, status){
console.log(response); //This should show the $output variable from submit.php
$('#dbOutput').innerHTML(response); //This will input the response into the div#dbOutput
$(':mobile-pagecontainer').pagecontainer('change', '#pagetwo'); //According to samples, this should be correct code... But again, I've never actually used pagecontainer
}
}
});
}
});
</script>
The connectDB.php file:
<?php
$config = [
'host' => 'localhost',
'username' => 'joe',
'password' => 'thisismypassword',
'dbname' => 'databasename'
];
$db = new PDO('mysql: host='.$config['host'].';dbname='.$config['dbname'],$config['username'],$config['password'];
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttrebute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
The database table:
+--------------------+
| tbl_orders |
+--------------------+
| id |
| onSiteName |
| onSiteNumber |
| adminContactName |
| adminContactNumber |
+--------------------+
The submit.php
file:
<?php
include 'connectDB.php';
$jobName = $delDate = $delTimeFrom = $useDate = $startTime = $puDate = $puTimeFrom = $address = $address2 = $zip = $city = $state = $delArea = $onSiteName = $onSiteNumber = $adminContactName = $adminContactNumber = $ACtotal = '';
//Check to make sure the there are values and that they aren't false values before proceeding
if (isset($_POST['onSiteName']) && !empty($_POST['onSiteName'])) {
$onSiteName = test_input($_POST["onSiteName"]);
$onSiteNumber = test_input($_POST["onSiteNumber"]);
$adminContactName = test_input($_POST["adminContactName"]);
$adminContactNumber = test_input($_POST["adminContactNumber"]);
//Preparing the query to avoid SQL injection
$sql = $db->prepare("INSERT INTO tbl_orders (onSiteName , onSiteNumber , adminContactName , adminContactNumber)
VALUES (:onSiteName , :onSiteNumber , :adminContactName , :adminContactNumber)";
//Binding the parameters
$sql_params = [
':onSiteName' => $onSiteName,
':onSiteNumber' => $onSiteNumber,
':adminContactName' => $adminContactName,
':adminContactNumber' => $adminContactNumber
];
$output = '';
if ($sql->execute($sql_params) === TRUE) {
$output .= "New record created successfully";
} else {
$output .= "Error: " . $sql . "<br>" . $conn->error;
}
echo $output;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlentities($data); //changed from htmlspecialchars to make sure most symbols are encoded properly for storage in DB
return $data;
}
?>
EDIT
I've updated the answer to include all necessary components. Also, I don't know how you're connecting to your database, but I use PDO to connect to databases, so if you use something other than that, you'll need to adapt the code. Basically, you will post the form using AJAX to your submit.php script which then processes and tries to insert into the DB. Then it echo
s the $output
variable which AJAX will catch as the response
and will log it in your console as well as change it in your new div titled #dbOutput
. Basically, the output wouldn't change because the PHP had already served to the page, so it was done. Using AJAX will allow you to, without reloading the page, dynamically change an element with results from the DB. You'll have to comb through carefully as there were several minor corrections/edits. Hope that helps!
Upvotes: 1