0

I have used jquery to validate my form data. As shown bellow:

<form method="post" id="register-form" novalidate>
<table width='100%>
<tr class="dark">
<td> <strong>Event Date:</strong> </td>
<td> <input type="date" name="event_date" id="event_date"> </td>
</tr>

<tr class="light">
<td> <strong> Description </strong> </td>
<td> <input type="text" name="event_desc" id="event_desc"/> </td>
</tr>

<tr class="dark">
<td> <strong> Year </strong> </td>
<td> <input type="text" name="year" id="year"/> </td>
</tr>

</table>

<input type='submit' name='add' value='Save Event'>
</form>


<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

<link href="runnable.css" rel="stylesheet" />
<!-- Load jQuery and the validate plugin -->
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

<!-- jQuery Form Validation code -->
  <script>

  // When the browser is ready...
  $(function() {

    // Setup form validation on the #register-form element
    $("#register-form").validate({

        // Specify the validation rules
        rules: {
            event_date: "required",
            event_desc: "required",
            event_date: {
                required: true,
            },

        },

        // Specify the validation error messages
        messages: {
            event_date: "Please select a date",
            event_desc: "Please enter event description",
        },

        submitHandler: function(form) {
            form.submit();
        }
    });

  });

  </script>

Now my question is when the validation finishes where can I use the Insert query to insert the form data in my table. I am talking about this query:

<?php
if(isset ($_POST['add']))
{
     mysql_query("INSERT into events (event_date, event_desc, year)   
                    VALUES('".$_POST['event_date']."',
                           '".$_POST['event_desc']."',
                           '".$_POST['year']."') ");

       echo "<script> alert('1 Record inserted!') </script>"; 
}

So can someone help me how to do it?

2
  • 1
    Use ajax or submit your form to specific .php page and process in that .php file... Commented Feb 15, 2014 at 10:07
  • how to submit my form to a specific php page, give me an example if u can plz Commented Feb 15, 2014 at 10:12

1 Answer 1

1

please place your query in side the if(isset($_POST['add'])){} condtions to check for post data.

if(isset($_POST['add'])){
//...   some code

 mysql_query("INSERT into events (event_date, event_desc, year)   
                VALUES('".$_POST['event_date']."',
                       '".$_POST['event_desc']."',
                       '".$_POST['year']."') ");
//...   some code
}

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('YOUR_DB', $link);
$result = mysql_query("set names 'utf8'");
$result = mysql_query("INSERT into events (event_date, event_desc, year)   
                VALUES('".$_POST['event_date']."',
                       '".$_POST['event_desc']."',
                       '".$_POST['year']."') ")OR die(mysql_error());;
mysql_close($link);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

sorry I forgot to write if(isset($_Post...) in my question. The code I have contains it but it still doesn't work. No data inserts in table.
Where can I use IF condition to set a constraint that the "Year" textbox shouldn't accept string in above mention Javascript code????
first you check submited to this page and init $_POST['add'] to run your code, if not init this var please type or insert full your code page to help you!!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.