11
I have the following question. I have a simple record and I want you to tell me when registering a new record:
If the field is empty it shows me the message "Fill in the fields";
If the form field already exists in the database it shows that "Duplicate value";
If there is not yet, he will save in the bank;
Good, but I wanted to do this modal check. I did the following tests:
INDEX.PHP
<html>
<title>Modal</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style>
.modal-header, h4, .close {
background-color: #5cb85c;
color:white !important;
text-align: center;
font-size: 30px;
}
.modal-footer {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<form action="teste.php" method="post">
Nome: <input type="text" name="username" > <input type="submit" name="submit" value="Abrir sem modal" />
<div class="container">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Abrir com modal</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Teste Modal</h4>
</div>
<div class="modal-body">
<p><?php include 'teste.php'; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
PHP TEST.
<?php
require("conexao.php");
$nome = $_POST['username'];
if ($nome == "") {
echo "Preencha o campo";
} else {
$sql = mysql_query("SELECT nome FROM tb_visitas WHERE nome='$nome'");
if (mysql_num_rows($sql) > 0) {
echo "Valor duplicado";
} else {
echo "Gravando registro";
}
}
?>
On the "no modal" button it does the correct check, it is possible to do the same with the modal?
you will need to use AJAX to be able to manipulate the data only by modal.
– RFL
Yes the question is "the code". But they answered me on the American website. For those who need the same thing, it is really necessary to use Ajax, because it is not performed 'Submit' then it is necessary the following code:
– netovgs
<script> $('form'). Submit(Function() { $.post("test.php", {username: $(this)[0].username.value}, Function (d) { $('#myModal').find(".modal-body").html(d); $('#myModal').modal('show'); }); turn false; }); </script>
– netovgs