Reputation: 153
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">×</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.
Upvotes: 0
Views: 10670
Reputation: 74232
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.
Upvotes: 4