How can I show a variable in Bootstrap modal in PHP? - php

I have a form that makes a calculation, enter the database and returns the value through an alert.
I would like to replace this alert for modal.
HTML
<form method="post" action="#">
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Send">Send</button>
</form>
PHP
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
echo '<script>';
echo 'alert("'.$imc.'");';
echo '</script>';
}
I tried as follows:
- Added the php after the tag </html> code;
- Added the variable responsible for the calculation: $imc;
- I linked the button Send with modal.
<form method="post" action="#">
<input type="hidden" name="acao" id="acao" value="cadastrar"/>
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Enviar" data-toggle="modal" data-target="#myModal">Enviar</button>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<?php echo'.$imc.';?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
PHP
<?php
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
return $imc;
}
?>
Thus, when I click on Send button (with all the empty fields), it opens the modal and displays the '.$imc.' as String.
When I insert the data and click on the button Send a dims window (early effect of modal) and quickly back to normal. I believe the modal is being called, but some stretch it gives erros and closes.
How can I solve this?
AJAX, top of code:
<?php
$imc = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$imc = calcularImc($_POST['altura'], $_POST['peso']);
}
?>
End of body
<?php
if ($imc !== false){
echo "<script>$('#myModal').modal('show');</script>";
}
?>
Would it be this? I tried to add this code, but gave the same result. By submitting the form, the window dims and quickly back to normal.
---------------------------------UPDATED / I MANAGED TO DO THAT I WANTED-------------------------------
<form method="post" action="#">
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Send" data-toggle="modal" data-target="#myModal">Send</button>
</form>
<?php
$imc = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$imc = calcularImc($_POST['altura'], $_POST['peso']);
}
?>
<?php
if ($imc !== false){
echo '<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>';
echo '<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
echo "<script>$('#myModal').modal('show');</script>";
}
?>
</body>
</html>
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
echo '<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">';
echo $imc; // RESULT OF CALC MODAL INSIDE
echo '</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>';
}
?>

POST Method:
<?php
function calcularIMC($altura,$peso){
$imc = 0;
if($altura >0 && $peso >0){
$imc = $peso / ($altura * $altura);
}
return $imc;
}
$imc = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$imc = calcularImc($_POST['alt'], $_POST['peso']);
?>
<script type="text/javascript">
$(window).load(function(){
$('#myModal').modal('show');
});
</script>
<?php
}
?>
<form method="post" action="#">
<input type="hidden" name="acao" id="acao" value="cadastrar"/>
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Send">Send</button>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<?php echo $imc;?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
Errors you made:
Addres altura as $_POST['altura'] instead of $_POST['alt'] (as its name attribute is alt)
You started your modal.show script before the page was loaded. So the modal might not exist yet. Wrap it in $(window).load(function(){})
Printed the variable in the wrong way: echo'.$imc.' should have been echo $imc;
Even nicer would be to use AJAX. Then you don't have to refresh the page. If you want to do that, lookup AJAX. You could also avoid using PHP. #AceWebDesign Answer explains nicely how this can be done without PHP.

You could use jQuery for this.
instead of
echo '<script>';
echo 'alert("'.$imc.'");';
echo '</script>';
You could use this on the page. It may need some tweaking as it was quick and rough, and the modal was a little rusty.
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<form method="post" action="#">
<input type="hidden" name="acao" id="acao" value="cadastrar"/>
<input type="text" name="alt" id="altura" required><br><br>
<input type="text" name="peso" id="peso" required><br><br>
<button type="submit" id="Enviar" data-toggle="modal" data-target="#myModal">Enviar</button>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(document).off("click", ".btn-default").on( "click", ".btn-default", function(e) {
var altura = $('#altura').val();
var peso = $('#peso').val();
var imc =0;
if (altura > 0 )
{
var imc = peso / (altura * altura);
}
$(".modal-body").html(imc);
$('#myModal').modal('show');
});
return false;
});
</script>
</body>
</html>

Related

How to make the edit modal using ajax and php

Im having problem when implementing edit function where I can edit the role of the user
Here is the button that will open the modal
<button type="button" id="edit" name="edit" class="btn btn-outline-warning" data-id="'.$row["id"].'">EDIT</button>
Here is the modal code
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="form-group">
<label for="userName" class="col-form-label">Username:</label>
<input type="text" class="form-control border-danger" id="userName" readonly style="background-color: #2A3038">
</div>
<div class="form-group">
<label for="user_type" class="col-form-label">User Type:</label>
<select class="form-control border-success" id="user_type">
<option value="user">User</option>
<option value="contributor">Contributor</option>
</select>
</div>
</form>
</div>
<div class="modal-footer">
<input type="hidden" id="user_id" name="user_id">
<button type="submit" id="update" name="update" class="btn btn-success">Update</button>
<button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Ajax
$(document).on('click', '#edit', function(){
var user_id = $(this).attr("data-id");
$.ajax({
url:"/auth/action",
method:"POST",
data:{user_id:user_id},
dataType:"json",
success:function(data)
{
$('#editModal').modal('show');
$('#userName').val(data.userName);
$('#user_type').val(data.user_type);
$('#user_id').val(user_id);
}
})
});
PHP where the action happens
if($_POST["action"] == 'update')
{
$query = 'UPDATE `users` SET username = :username, user_type = :user_type WHERE id = :id';
$statement = $connect->prepare($query);
$statement->execute(
array(
':id' => $_POST['user_id'],
':username' => $_POST['userName'],
':user_type' => $_POST['user_type']
)
);
$result = $statement->fetchAll();
if(isset($result))
{
echo '<div class="alert alert-fill-warning" role="alert">User type changed!<div>';
}
}
and also I have a function for fetch which I named it load_user_data()
Here is the for the datatype json
if(isset($_POST["user_id"]))
{
$output = array();
$statement = $connect->prepare(
"SELECT * FROM users WHERE id = '".$_POST["user_id"]."' LIMIT 1"
);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$output["userName"] = $row["username"];
$output["user_type"] = $row["user_type"];
}
echo json_encode($output);
}
The problem Im having is that the PHP action code is not working or is there anything wrong with my code? but I dont have probelm with displaying the data in the modal except if i submit the change there is no function happening
Your ajax data property is missing a lot of parametars:
....ajax....
data: {user_id: user_id, userName: username here,user_type:set type here, action: 'update'}
You need to add edit id to your update button
Also you need to add userdata to your ajax response, currently you are using data.userName etc. but you didnt put that in the response
You can find more info on how to properly return json response here:
Returning JSON from a PHP Script
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="appenddata">
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" id="update" name="update" class="btn btn-success">Update</button>
<button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).on('click', '#edit', function(){
$('#editModal').modal('show');
var user_id = $(this).attr("data-id");
$.ajax({
url:"/auth/action",
method:"POST",
data:{user_id:user_id},
success:function(data)
{
$(".appenddata)".html(data);
}
})
});
</script>
<?php
if(isset($_POST["user_id"]))
{
$output = array();
$statement = $connect->prepare(
"SELECT * FROM users WHERE id = '".$_POST["user_id"]."' LIMIT 1"
);
$statement->execute();
$result = $statement->fetchAll();
$user_array=array("user","contributor");
?>
<input type="hidden" id="user_id" name="user_id" value="<?= $_POST["user_id"]; ?>">
<div class="form-group">
<label for="userName" class="col-form-label">Username:</label>
<input type="text" class="form-control border-danger" id="userName" value="<?= $result[0]['username']; ?>" readonly style="background-color: #2A3038">
</div>
<div class="form-group">
<label for="user_type" class="col-form-label">User Type:</label>
<select class="form-control border-success" id="user_type">
<?php
if($user_array!=NULL)
{
foreach($user_array as $data)
{
if($data==$result[0]['username'])
{
$selected='selected="selected"';
}
else
{
$selected='';
}
?>
<option value="<?= $data; ?>"><?= ucwords($data); ?></option>
<?php
}
}
?>
</select>
</div>
<?php
}

Ways to get id to use in modal for updating database in php

So my purpose is to get the ID from a database and making the id ready to be used for modal. I know that I can use input type=hidden but I don't know for sure it is safe because in inspect element, user can edit it. I'm thinking also of session but I don't have any idea how can I do it. So what are the ways I can do to make the id not editable after submitting? Or how can i put it in array and match the id? Here is my code I used
class.names.php
public function getAllNames()
{
$obj = new Db();
$stmt = $obj->connect()->query("SELECT * FROM persons");
while ($person = $stmt->fetch())
{
echo "<tr>";
echo "<td>".$person['first_name']."</td>";
echo "<td>".$person['last_name']."</td>";
echo "<td><a id=\"".$person['person_id']."\"type=\"button\" data-target-toggle=\"modal\" data-target=\"#edit-name-modal\" class=\"btn btn-danger edit_data\" href=\"#\">Update</a></td>";
echo "</tr>";
}
}
names.js
$(document).on('click', '.edit_data', function(){
var person_id = $(this).attr("id");
$.ajax({
url:"/data/updatename.php",
method:"POST",
data:{person_id:person_id},
dataType:"json",
success:function(data){
$('#first_name').val(data.first_name);
$('#last_name').val(data.last_name);
$('#person_id').val(data.person_id);
$('#edit-name-modal').modal('show');
}
});
});
updatename.php
<?php
include_once 'db.php';
if(isset($_POST["person_id"]))
{
$person_id = $_POST["person_id"];
$object = new Dbc();
$stmt = $object->connect()->prepare("SELECT * FROM persons WHERE person_id=?");
$stmt->execute([$person_id]);
$profile_info = $stmt->fetch();
echo json_encode($profile_info);
}
?>
namelist.php
<div class="modal fade" id="edit-name-modal" name="edit-name" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="POST" enctype="multipart/form-data" action="namelist.php">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Update Name's List</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>First Name</label>
<input type="text" id="first_name" name="first_name" class="form-control">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" id="last_name" name="last_name" class="form-control">
</div>
<input type="hidden" id="person_id" name="person_id">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="update" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>

Login and upload image in codeigniter

I am trying to create login form in the following code.
After successful login , I uploaded the image and submitted. I refreshed the page and I got the following error message:
A PHP Error was encountered Severity: Notice Message: Undefined
index: image Filename: controllers/home.php Line Number: 89 Backtrace:
File:
C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\application\controllers\home.php
Line: 89 Function: _error_handler File:
C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\application\controllers\home.php
Line: 82 Function: addimage File:
C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\index.php Line: 293
Function: include_once
What is the problem in this login code. Please provide solution for this issue:
Controller:
public function login()
{
$data=$this->data;
$email=$this->input->post('email');
$password=md5($this->input->post('password'));
$result=$this->user_model->login($email,$password);
if(count($result)!==0)
{
$this->session->set_userdata('user_id',$email);
$seid=$this->session->userdata('user_id');
if($seid=='') {
redirect(site_url());
}
else {
$this->load->view('display',$data);
}
} else
{
redirect('home/index');
}
}
public function view()
{
$data=$this->data;
$this->addimage();
}
public function addimage()
{
$path = $_FILES['image']['name'];
$imgext=strtolower(strrchr($path,'.'));
$imgname= $this->generateRandomString().$imgext;
if($path!='')
{
$im= $this->config->item('base_url').'/uploads'.'/'.$imgname;
$x=$this->do_upload($imgname);
$data['img']=$im;
$this->user_model->getimage($data['img']);
$this->load->view('register_view');
}
}
Model:
function login($email,$password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("user");
return $query->result_array();
}
public function getimage($data)
{
$var=array('img'=>$data);
$this->db->insert('login',$var);
$query = $this->db->get('login');
return $query;
}
This is the html code for display.php
View:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#mymodal">Add Image</button>
<!-- Modal -->
<div class="modal fade" id="mymodal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2>Upload image</h2>
<form action="<?=site_url('home/view')?>" method="post" enctype="multipart/form-data"/>
<input type="file" class="upload" name="image" id="file" required value="<?php echo set_value('image'); ?>" />
<input type="submit" name="log" value="Submit" />
</form>
</div>
</div>
</div>
This is the code for index.php
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#login-modal">Login</button>
<!-- Modal -->
<div class="modal fade" id="login-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2>Login</h2>
<form action="<?=site_url('home/login')?>" method="post">
<div>
<label>Email </label>
<input name="email" type="text" id="email" value="<?php echo set_value('email'); ?>" />
</div>
<div>
<label>Password</label>
<input name="password" type="password" id="mypassword" value="<?php echo set_value('password'); ?>" />
</div>
<button type="submit" class="btn btn-success">Sign in</button>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</form>
</div>
</div>
</div>
</div>

HTML Bootstrap PHP connection error

I want to write log in code for my index.html ' s form and i wrote a giris-yap.php file which is at below. i cant access my php file browser get alert only like localhost is waiting .
i tried to put action method in my form's submit button but it was not usefull.
giris-yap.php
<?php
require "connect.inc.php";
require "core.inc.php";
if(isset($_POST['exampleInputEmail1']) && isset($_POST['exampleInputPassword1']) ){
$mail=$_POST['exampleInputEmail1'];
$pass=$_POST['exampleInputPassword1'];
$password=md5($pass);
if($query_run=mysql_query("SELECT * FROM `users` WHERE `e-mail`= '".mysql_real_escape_string($mail)."' AND `sifre`='".mysql_real_escape_string($password)." ' ")){
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows==0){
echo 'Invalid';
}
else if($query_num_rows!=0){
$ad=mysql_result($query_run,0,'Ad');
$_SESSION['ad']=$ad;
$usersurname=mysql_result($query_run,0,'SoyAd');
$_SESSION['usersurname']=$usersurname;
$username=mysql_result($query_run,0,'e-mail');
$_SESSION['username']=$username;
header('Location: index.html');
}
}
else{
echo mysql_error();
}
}
else{echo 'error';}
/**
* Created by PhpStorm.
* User: bilsay
* Date: 21.05.2015
* Time: 10:35
*/
?>
index.html :
<div class="modal fade" id="login-modal-box" role="dialog" aria-labelledby="gridSystemModalLabel" aria-hidden="true">
<form action="#giris-kontrol" method="POST">
<div class="modal-dialog user-login-box-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">Kullanıcı Giriş Paneli</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group">
<label for="exampleInputEmail1">Eposta Adresiniz</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şifre</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Beni hatırla
</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" value="Giriş">Giriş</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</form>
</div><!-- /.modal -->
cant access my php file
You need to update your action as described in the other answer: https://stackoverflow.com/a/30377560/482256.
Then, note that this code here:
require connect.inc.php;
require core.inc.php;
Is the equivalent of doing this:
require 'connectincphp';
require 'coreincphp';
When you don't use quotes, PHP looks for constants, and when it doesn't find those it will assume the string, so connect becomes "connect". The period concatenates, so it combines "connect" with "inc" and you get "connectinc", etc.
The require should be causing a 500 error...and possibly an empty page depending on what your error output settings are.
Your code translated to PDO and BCrypt, because I just can't "fix" code and leave it insecure:
if(isset($_POST['exampleInputEmail1']) && isset($_POST['exampleInputPassword1']) ){
$pdo = new \PDO('mysql:dbname=dbName;host=localhost','username','password');
$mail = $_POST['exampleInputEmail1'];
$pass = $_POST['exampleInputPassword1'];
$userSql = $pdo->prepare("SELECT * FROM `users` WHERE `e-mail`=:email");
$userSql->execute(array('email'=>$mail));
$userData = $userSql->fetch(\PDO::FETCH_ASSOC);
if( $userData !== false && BCrypt::isValidPassword($pass, $userData['sifre']) ) {
$_SESSION['ad'] = $userData;
$_SESSION['usersurname'] = $userData['SoyAd'];
$_SESSION['username'] = $userData['username'];
header('Location: index.html');
}
else {
die("You have entered an invalid username or password");
}
}
else{
die("Username and Password must be submitted");
}
And your modified HTML. I fixed the action, turned your button into a real submit button, and added the name= attributes to your inputs:
<div class="modal fade" id="login-modal-box" role="dialog" aria-labelledby="gridSystemModalLabel" aria-hidden="true">
<form action="giris-yap.php" method="POST">
<div class="modal-dialog user-login-box-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">Kullanıcı Giriş Paneli</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group">
<label for="exampleInputEmail1">Eposta Adresiniz</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şifre</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="exampleInputPassword1" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Beni hatırla
</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" value="1" class="btn btn-primary" id="submit"> Giriş Yap</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</form>
</div><!-- /.modal -->
And the BCrypt class you will need. However, use password_hash and password_verify if you have PHP >= 5.5.
class BCrypt {
public static function hash( $password, $cost=12 ) {
$base64 = base64_encode(openssl_random_pseudo_bytes(17));
$salt = str_replace('+','.',substr($base64,0,22));
$cost = str_pad($cost,2,'0',STR_PAD_LEFT);
$algo = version_compare(phpversion(),'5.3.7') >= 0 ? '2y' : '2a';
$prefix = "\${$algo}\${$cost}\${$salt}";
return crypt($password, $prefix);
}
public static function isValidPassword( $password, $storedHash ) {
$newHash = crypt( $password, $storedHash );
return self::areHashesEqual($newHash,$storedHash);
}
private static function areHashesEqual( $hash1, $hash2 ) {
$length1 = strlen($hash1);
$length2 = strlen($hash2);
$diff = $length1 ^ $length2;
for($i = 0; $i < $length1 && $i < $length2; $i++) {
$diff |= ord($hash1[$i]) ^ ord($hash2[$i]);
}
return $diff === 0;
}
}
change action..
<form action="giris-yap.php" method="POST">
then change a link in your modal-footer
<button type="submit" value="Giriş Yap" class="btn btn-primary" id="submit" />

JQuery validate within a Twitter Bootstrap modal with PHP

I'm working on a transport planner and in my tasks table I have a button to sign a routenumber to a task. When a task haven't got a routenumber at all, the button will call a bootstrap modal to assign a new routenumber to that task which will also be the standard routenumber.
When a task already has a routenumber, the button will call the same modal with a new checkbox added. Unchecked means, change the routenumber only today, checked means, make the inserted routenumber the standard routenumber.
I'm using JQuery for the validation but nothings happend and the form is submitted without validation. What am I doing wrong..?
LINK TO CALL THE MODAL:
<?php if ($row['routenummer'] != ''){ echo $row['routenummer']; } if ($row['routenummer'] == ''){ ?><i class="icon-plus"></i><?php }?>
MODAL:
<div id="routenummer_view<?php echo $row['planning_id']?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="routenummer_viewLabel" aria-hidden="true">
<form class="modal-form" action="../includes/modals/routenummer/submit_view_routenummer.php"
data-remote="true" method="post" id="routenummer_edit">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="routenummerviewLabel"><?php if ($row['routenummer'] == ''){ echo 'Standaard Routenummer Invoeren';} else echo 'Routenummer Wijzigen';?></h3>
</div>
<div class="modal-body">
<label>Routenummer:</label>
<input type="text" name="routenummer" <?php if ($row['routenummer'] != NULL){ ?>value="<?php if ($row['routenummer'] != NULL){ echo $row['routenummer']; echo '" /><br />';}?> <?php } if ($row['routenummer'] == NULL){ ?> placeholder="Typ hier een routenummer..." /><?php if ($row['routenummer'] != NULL){ echo '<br />'; }}?>
<?php if ($row['routenummer'] != NULL){ ?>
<label class="checkbox">
<input type="checkbox" name="standaard"> Maak standaard routenummer.</label>
<?php }?>
</div>
<div class="modal-footer">
<input type="hidden" name="opdrachtid" value="<?php echo $row['planning_id']; ?>" />
<input type="submit" value="<?php if ($row['routenummer'] == ''){ echo 'Invoeren';} else echo 'Wijzigen';?>" class="btn btn-primary" />
Annuleren
</div>
</form>
</div>
JQUERY:
$(document).ready(function() {
$("#routenummer_edit").validate({
onkeyup : false,
rules : {
routenummer : {
required : true,
digits: true
}
},
messages : {
routenummer : {
required : "Routenummer is verplicht.",
digits : "Routenummer moet een getal zijn."
}
},
});
});
EXAMPLE IMAGE
EDIT
RENDERED HTML
When a task doesn't have a standard routenumber:
<div id="routenummer_view90" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="routenummer_viewLabel" aria-hidden="true">
<form class="modal-form" action="../includes/modals/routenummer/submit_view_routenummer.php"
data-remote="true" method="post" id="routenummer_edit">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="routenummerviewLabel">Standaard Routenummer Invoeren</h3>
</div>
<div class="modal-body">
<label>Routenummer:</label>
<input type="text" name="routenummer" placeholder="Typ hier een routenummer..." />
</div>
<div class="modal-footer">
<input type="hidden" name="opdrachtid" value="90" />
<input type="submit" value="Invoeren" class="btn btn-primary" />
Annuleren
</div>
</form>
</div>
When a task does have a standard routenumber:
<div id="routenummer_view67" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="routenummer_viewLabel" aria-hidden="true">
<form class="modal-form" action="../includes/modals/routenummer/submit_view_routenummer.php"
data-remote="true" method="post" id="routenummer_edit">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="routenummerviewLabel">Routenummer Wijzigen</h3>
</div>
<div class="modal-body">
<label>Routenummer:</label>
<input type="text" name="routenummer" value="21" /><br /> <label class="checkbox">
<input type="checkbox" name="standaard"> Maak standaard routenummer.</label>
</div>
<div class="modal-footer">
<input type="hidden" name="opdrachtid" value="67" />
<input type="submit" value="Wijzigen" class="btn btn-primary" />
Annuleren
</div>
</form>
</div>
EDIT
$(document).ready(function() {
$('.modal-form').each(function () {
$(this).validate({ // initialize the plugin
rules: {
routenummer: {
required: true,
digits: true
}
},
messages : {
routenummer : {
required : "Routenummer is verplicht.",
digits : "Routenummer moet een getal zijn."
}
}
});
});
});
EDIT
MODAL
<div id="routenummer<?php echo $row['planning_id']?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="routenummerLabel" aria-hidden="true">
<form class="modal-form" action="../includes/modals/routenummer/submit_routenummer.php"
data-remote="true" method="post">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="routenummerLabel"><?php if ($row['routenummer'] == ''){ echo 'Standaard Routenummer Invoeren';} else echo 'Routenummer Wijzigen';?></h3>
</div>
<div class="modal-body">
<label>Routenummer:</label>
<input type="text" name="routenummer" <?php if ($row['routenummer'] != NULL){ ?>value="<?php if ($row['routenummer'] != NULL){ echo $row['routenummer']; echo '" /><br />';}?> <?php } if ($row['routenummer'] == NULL){ ?> placeholder="Typ hier een routenummer..." /><?php if ($row['routenummer'] != NULL){ echo '<br />'; }}?>
<?php if ($row['routenummer'] != NULL){ ?>
<label class="checkbox">
<input type="checkbox" name="standaard"> Maak standaard routenummer.</label>
<?php }?>
</div>
<div class="modal-footer">
<input type="hidden" name="opdrachtid" value="<?php echo $row['planning_id']; ?>" />
<input type="submit" value="<?php if ($row['routenummer'] == ''){ echo 'Invoeren';} else echo 'Wijzigen';?>" class="btn btn-primary" />
Annuleren
</div>
</form>
</div>
Validation is working as intended in both of your examples...
Your "When a task doesn't have a standard routenumber" example is working:
http://jsfiddle.net/qdX8y/1/
(Validation will not allow submission of an empty field)
Your "When a task does have a standard routenumber" example is also working. However, it passes validation immediately because you already have a value in the field that meets the requirements of the compound rule, required and digits.
<input type="text" name="routenummer" value="21" />
Removing value="21" causes a validation message to be triggered.
http://jsfiddle.net/qdX8y/
EDIT:
As per comments on OP, it was discovered that the OP has multiple form elements sharing the same id. Not only is this invalid HTML, but it breaks the Validate plugin since it does not know which form to target.
However, all form elements can share the same class name and then you can use a jQuery .each() to select them all. This example contains five duplicate <form> elements, each with class="myform" and assumes all five form elements share the same .validate() options.
$(document).ready(function () {
$('.myform').each(function () {
$(this).validate({ // initialize the plugin on each form
// your options and rules
});
});
});
DEMO: http://jsfiddle.net/cWtd6/

Categories