0

I've been trying to figure out how to insert data from a modal into a database after hitting submit. The issue I have right now is it just keeps refreshing the page but nothing is being added please help.

here's my modal call:

  <button class="btn btn-default" data-toggle="modal" data-target="#loginModal">Login</button>

<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Login</h4>
            </div>

            <div class="modal-body">
            <?php include("new.php");?>


            </div>
        </div>
    </div>
</div>

here's my new.php:

<?php
 function renderForm($user, $rank, $position, $error)
 {
?>
 <?php 
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
<form action="" method="post">
  <div class="form-group">
    <label for="username">Username</label>
    <input id="username" class="form-control" type="text" name="user" placeholder="Username" value="<?php echo $user; ?>" />
  </div>
  <div class="form-group">
    <label for="rank">Rank</label>
     <select class="form-control" name="rank">
 <option value="recruit">recruit</option>
 <option value="officer">officer</option>
 <option value="leader">leader</option>

</select>
  </div>
  <div class="form-group">
    <label for="position">Position</label>
    <input id="position" class="form-control" type="text" name="position" placeholder="Leader" value="<?php echo $position; ?>" />
  </div>
    <div class="form-group">
    <label for="Date">Date</label>
    <input id="Date" class="form-control" type="text" name="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>" />
  </div>
    <div class="form-group">
    <label for="Tag">Tag</label>
    <input id="Tag" class="form-control" type="text" name="tag" value="<?php echo $tag; ?>" />
  </div>
    <div class="form-group">
    <label for="AiT">AiT</label>
    <input id="AiT" class="form-control" type="text" name="ait" value="<?php echo $ait; ?>" />
  </div>
    <div class="form-group">
    <label for="ServiceStripes">Service Stripes</label>
    <input id="ServiceStripes" class="form-control" type="text" name="ss" value="<?php echo $ss; ?>" />
  </div>
    <div class="form-group">
    <label for="Notes">Notes</label>
    <input id="Notes" class="form-control" type="text" name="notes" placeholder="Notes" value="<?php echo $notes; ?>" />
  </div>
  <button type="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
<?php 
}

 include('classes/connect-db.php');

 if (isset($_POST['submit']))
 { 

 $user = mysql_real_escape_string(htmlspecialchars($_POST['user']));
 $rank = mysql_real_escape_string(htmlspecialchars($_POST['rank']));
 $position = mysql_real_escape_string(htmlspecialchars($_POST['position']));
 $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
 $tag = mysql_real_escape_string(htmlspecialchars($_POST['tag']));
 $ait = mysql_real_escape_string(htmlspecialchars($_POST['ait']));
 $ss = mysql_real_escape_string(htmlspecialchars($_POST['ss']));
 $notes = mysql_real_escape_string(htmlspecialchars($_POST['notes']));

 if ($user == '' || $rank == '' || $date == '' || $tag == '')
 {
 $error = '<center>ERROR: Please fill in all required fields!</center>';

 @renderForm($user, $rank, $position, $error);
 }
 else
 {
 mysql_query("INSERT players SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', ait='$ait', ss='$ss', notes='$notes'")
 or die(mysql_error()); 

 }
 }
 else

 {
 @renderForm('','','');
 }?>

This is like the breaking bone of this project. Been working on this for a while now and I'm trying to move away from child windows and into modals but I can't figure out how to do it.

2
  • Would it not be easy to bind (via jQuery) to catch the submit event and just ajax it to a seperate script? Commented Dec 19, 2014 at 3:18
  • I don't know how to do that. I'm not really good with jquery/ajax. Commented Dec 19, 2014 at 3:21

1 Answer 1

4

The reason why it's not working is because of your conditional statement:

if (isset($_POST['submit'])){...}

There is no element named "submit" in your code, which I suspect should be used in conjunction with your submit button:

<button type="submit" class="btn btn-default" value="Submit">Submit</button>

Modify it to:

<button type="submit" name="submit" class="btn btn-default" value="Submit">Submit</button>

Having error reporting on, would have signaled an "Undefined index" notice.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

Consider moving over to a safer MySQL API:

Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Sign up to request clarification or add additional context in comments.

6 Comments

Plus 1, you're the winner Fred!
@Fred-ii- I have another issue. Now after the info is submitted the form closes but when I opens it doesn't show the form. Also, everytime I refresh page it enters form info again doubling it in the database.
Try using a header to redirect upon successful query. @Riley
@Fred-ii- I have another issue regarding another part of my form. This too is trying to get something in a modal. I also have an issue where I can't have 2 differen't modals on the same page or they won't open. Can I open up another question and you help me with it or is there another way I could contact you.
@Riley Try opening a new question, because TBH I may not be able to be able to help. Otherwise, I would have helped you without having to open a new question.
|

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.