Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL jQuery Forms
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.
 
<!--Top of second page within multi page layout containing form-->
<!-- The POST A JOURNEY page where user creates a journey to be seen by others-->
    <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">
 
<!--Top of multipage layout-->
<?php
require 'database.php';
?>
 
Updated
 
<?php
 
//get the journey data
	$radUserType = intval($_POST['radUserType']);
	$pjFrom = $_POST['pjFrom'];
	$pjTo = $_POST['pjTo'];
	$radioJourneyType = intval($_POST['radioJourneyType']);
	$departDate = $_POST['departDate'];
	$returnDate = $_POST['returnDate'];
	$textareanotes = $_POST['textAreaNotes'];
 
//check all values from $_POST
$canSubmitForm = false;
$isFormEmpty = false;
 
if (empty($pjFrom) || empty($pjTo))
{
	$isFormEmpty = true;
}
 
//check for your data here
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';
 
//will only get executed if true
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');
}
//Display the Product List page
?>
Posted 25-Mar-13 5:57am
Edited 25-Mar-13 10:38am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try modifying your if condition as shown below
if ((isset($_POST['radUserType']) && !empty($_POST['radUserType'])) && (isset($_POST['pjFrom']) && !empty($_POST['radUserType'])) && (isset($_POST['pjTo']) && !empty($_POST['pjTo'])) && (isset($_POST['radioJourneyType']) && !empty($_POST['radioJourneyType'])) && (isset($_POST['departDate']) && !empty($_POST['departDate'])) && (isset($_POST['returnDate']) && !empty($_POST['returnDate'])) && (isset($_POST['textAreaNotes']) && !empty($_POST['textAreaNotes'])))
 
isset alone won't work as blank values will also get sent as part of POST request and isset only checks whether a variable or array element is set or not. Which in this case will always return true. Adding !empty will ensure that the value is also present.
 
Regards,
  Permalink  
Comments
Colin Roe at 25-Mar-13 15:45pm
   
Thanks Prasad. I updated my php. Can I ask if you know why after I click/tap "Submit" on my form, if the insert occurs, I am redirected to the index page with the url (http://localhost/share/add_journey.php) while if there is an error with the form, I am directed to the same url (http://localhost/share/add_journey.php), but is blank. Only if I refresh the page, do I see the error.php page.
Prasad Khandekar at 26-Mar-13 4:45am
   
In case of error the php execution halts and hence you don;t get to see the page. If you do a view source from browser you might as weel see the partial html code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 240
2 DamithSL 200
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100