0

Firstly I have searched previous answers and (as far as I'm aware) none of the answers seemed to fit my question - though I'm sure it's been answered somewhere in the past - so forgive me.

I have 3 pages, which work together to submit a HTML form and save it in a MySQL database.

The pages are as follows;

  • idea.php (contains the form, includes the below idea_js.js file in a <script></script> tag).

  • idea_js.js (checks the submitted form for errors and submits via ajaxSubmit to ideaSubmit.php)

  • ideaSubmit.php (assigns POST'd variables and submits to database.)

My problem is that when the form is submitted, ideaSubmit recieves no POST'd variables.

I've come to this conclusion with a check;

 if (!empty($_POST)){
 echo "empty!"; }

and my page loads (with a bunch of unassigned variable errors and the message);

empty!

Can you fill me in as to why this is happening? Included are the three files.

idea.php:

<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript" src="js/idea_js.js"></script>

<!DOCTYPE html>
<html>
    <head>
        <?php include 'headIncludes.php'; ?>
    </head>
    <body>
        <?php include 'includes/userInterface.php'; ?>
        <div id="mainContent">
            <div class="mainContentContainer">
                <div id="home" >
                    <section id="content-wrapper">
                        <br>
                        <br>
                        <form id="submit_idea" action ="submitIdea.php" method="POST">
                            <fieldset> 
                                <legend>Idea submission</legend>
                                <label for="title">Title</label>
                                <input type="text" name="title"/>
                                <br>
                                <label for="brief">Brief</label>
                                <input type="text" name="brief"/>
                                <br>
                                <label for="problem">Problem</label>
                                <input type="text" name="problem"/>
                                <br>
                                <label for="solution">solution</label>
                                <input type="text" name="solution"/>
                                <br>
                                <label for="audience">audience</label>
                                <input type="text" name="audience"/>
                                <br>
                                <label for="prediction">prediction</label>
                                <input type="text" name="prediction"/>
                                <br>
                                <label for="constraints">constraints</label>
                                <input type="text" name="constraints"/><br>

                                <button type="submit" onclick="processIdea();">Submit</button>
                                <div style="clear:both;"></div>
                            </fieldset>
                        </form>



                    </section>  

                </div>
            </div>

        </div>
    </body>
</html>

idea_js.js;

$("document").ready(function() {
 $("#submit_idea").submit(function() {
  processIdea();
  return false;
 });
});

function processIdea() {
 var errors = '';

 // Validate title
 var title = $("#submit_idea [name='title']").val();
 if (!title) {
  errors += ' - Please enter a title\n';
 }
 // Validate brief
 var brief = $("#submit_idea [name='brief']").val();
 if (!brief) {
  errors += ' - Please enter a short idea brief\n';
 }
 // Validate Problem
 var problem = $("#submit_idea [name='problem']").val();
 if (!problem) {
  errors += ' - Please discribe the problem you want to solve\n';
 }
 //Validate Solution
  var solution = $("#submit_idea [name='solution']").val();
 if (!solution) {
  errors += ' - Please discribe your solution to the above problem\n';
 }
 //Validate Audience
  var audience = $("#submit_idea [name='audience']").val();
 if (!audience) {
  errors += ' - Please discribe the audience your solution targets\n';
 }
  //Validate Prediction
  var prediction = $("#submit_idea [name='prediction']").val();
 if (!prediction) {
  errors += ' - Please discribe the prediction you want to solve\n';
 }
  //Validate constraints
  var constraints = $("#submit_idea [name='constraints']").val();
 if (!constraints) {
  errors += ' - Please discribe the constraints of your solution\n';
 }

 if (errors){
  errors = 'The following errors occurred:\n' + errors;
  alert(errors);
  return false;
 } else {
  // Submit our form via Ajax and then reset the form
  $("#submit_idea").ajaxSubmit({success:showResult});
  return false;
 } 
}

function showResult(data) {
 if (data == 'save_failed') {
  alert('Form save failed, please contact your administrator');
  return false;
 } else {
  $("#submit_idea").clearForm().clearFields().resetForm();
  alert('Form save success');
  return false;
 }
}

submitIdea.php;

<?php
//Starts session
include_once '/includes/db_connect.php';
include_once '/includes/functions.php';
sec_session_start(); 

if(login_check($mysqli) == true) {

// Retrieve form data
if (!empty($_POST)){
 echo "empty!"; }

if(isset($_POST['submit_idea'])){
if(isset($_POST['title'])){ $title = $_POST['title']; } 
if(isset($_POST['brief'])){ $brief = $_POST['brief']; } 
if(isset($_POST['problem'])){ $problem = $_POST['problem']; } 
if(isset($_POST['solution'])){ $solution = $_POST['solution']; } 
if(isset($_POST['audience'])){ $audience = $_POST['audience']; } 
if(isset($_POST['prediction'])){ $prediction = $_POST['prediction']; } 
if(isset($_POST['constraints'])){ $constraints = $_POST['constraints']; } 


if (!$title || !$brief || !$problem || !$solution || !$audience || !$prediction || !$constraints) {
    echo "save_failed";
    return;
}

// Clean variables before performing insert
$clean_title = mysql_real_escape_string($title);
$clean_brief = mysql_real_escape_string($brief);
$clean_problem = mysql_real_escape_string($problem);
$clean_solution = mysql_real_escape_string($solution);
$clean_audience = mysql_real_escape_string($audience);
$clean_prediction = mysql_real_escape_string($prediction);
$clean_constraints = mysql_real_escape_string($constraints);
// $clean_categories_list = mysql_real_escape_string($categories_list);
} 
else {
// Perform insert
$now = time();
$user_id = $_SESSION['user_id'];
$mysqli->query("INSERT INTO idea_thread (user_id, time, title, Brief, problem, solution, audience, prediction, constraints) VALUES ('{$user_id}', '{$now}', '{$clean_title}', '{$clean_brief}', '{$clean_problem}', '{$clean_solution}', '{$clean_audience}', '{$clean_prediction}', '{$clean_constraints}')");

// if (@mysql_query($sql, $link)) {
 echo "success";
}
// return;
//} else {
// echo "save_failed";
// return;
//}
} else { 
    echo "How did you get here? Please log in first!";
    header("Location: ../signup.php");
    exit;
}
?>

Thanks, all help appreciated!

Phil
  • 157,677
  • 23
  • 242
  • 245
Tainted
  • 237
  • 1
  • 5
  • 17
  • 3
    I don't see the inclusion of any plugin that gives you `ajaxSubmit`, and it is not native to jQuery's core – Ohgodwhy Mar 03 '14 at 00:26
  • 2
    and also, as `GET` is the default method for jQuery's Ajax API - perhaps you'll need to set that method to `POST` in your `ajaxSubmit` function – scrowler Mar 03 '14 at 00:27
  • Thanks guys! Attempting both suggestions now, I wasn't aware ajaxSubmit wasn't core Jquery (hurr). Will update you both in a couple minutes. – Tainted Mar 03 '14 at 00:30
  • Question seems fine to me. Side note: "discribe" is misspelled; it should be "describe". – halfer Mar 03 '14 at 00:38
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) - FYI, you're using `mysql_real_escape_string` with mysqli. This won't work. You should also be using a prepared statement with bound parameters. See http://php.net/manual/mysqli.quickstart.prepared-statements.php – Phil Mar 03 '14 at 00:40
  • Your code echo's 'empty!' if it is filled, use `if(empty())` instead of `if(!empty())` Second, with the if statements containing !$title etc... how do you know $title is set? It could not be set if $_POST['title'] is not set. – Akshay Kalose Mar 03 '14 at 00:48
  • Thanks @Phil and Akshay! Much appreciated. Making tweaks now. – Tainted Mar 03 '14 at 00:51

0 Answers0