I am making a jquery mobile web app and I am using a multi page layout. I connect to mySQL when the page loads and on the second div page, I have a form which using a php file sends the values in the form to mySQL database when submitted.
My problem is, when I click/tap the submit button it inserts into the database even if there is no data in the form elements and redirects to my localhost/share/add_journey.php. I'm relatively new to web app development and would like any help as to what I'm doing wrong.
Advice on how best to insert to a mySQL database through php on a multi-page layout would be great. Please let me know if I need to show more code.
<!--
<!--
<div data-role="page" id="postAJourney" data-add-back-btn="true" data-back-btn-text="back" data-rel="back" data-direction="reverse" data-transition="flip" data-theme="b" >
<div data-role="header" data-position="fixed">
<h1>Post A Journey</h1>
</div>
<div data-role="content">
<form action="add_journey.php" method="post" id="postAJourneyForm">
<!--
<?php
require 'database.php';
?>
Updated
<?php
$radUserType = intval($_POST['radUserType']);
$pjFrom = $_POST['pjFrom'];
$pjTo = $_POST['pjTo'];
$radioJourneyType = intval($_POST['radioJourneyType']);
$departDate = $_POST['departDate'];
$returnDate = $_POST['returnDate'];
$textareanotes = $_POST['textAreaNotes'];
$canSubmitForm = false;
$isFormEmpty = false;
if (empty($pjFrom) || empty($pjTo))
{
$isFormEmpty = true;
}
if(isset($radUserType) && isset($pjFrom) && isset($pjTo) && isset($radioJourneyType) && isset($departDate) && isset($returnDate) && isset($textareanotes))
{
$canSubmitForm = true;
}
$departTime = '11:12';
$returnTime = '11:16';
$seatcounter = '2';
if ($canSubmitForm)
{
if ($isFormEmpty == false)
{
require_once('database.php');
$query = "INSERT INTO journey
(from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type)
VALUES('$pjFrom','$pjTo','$radioJourneyType','$departDate','$departTime','$returnDate','$returnTime','$seatcounter','$textareanotes','$radUserType')";
$db->exec($query);
include('index.php');
}
}
else
{
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
}
?>