This is my code, i was supposed to get the "$pw" outside the modal.. But it seems that i can't get the value of it and Also i can't seem to fetch the "$errMSG" value.. If it's if($pin != $pww) cant get the err MESSAGE.. Really need some help.. Thank you!
This is my "$pw" code, its on the same page as my modal..
<?php
include('connectdb.php');
$sql = "SELECT * FROM accounts WHERE usernamee='$userid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($data = $result->fetch_assoc()) {
$pww = $data['passwordd'];
}
}
?>
This is my modal code:
<div class="container">
<form method="post">
<h2>Enter your Pin Number</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Enter your Pin Number</h4>
</div>
<div class="modal-body text-center">
<input type="password" id="first-name" required="required" class="form-control" name="pin">
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-infos-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
</div>
<div class="modal-footer">
<button type="submit" name="submit3" class="btn btn-default"><i class="fa fa-key"></i> Confirm Pin</button>
</div>
</div>
</div>
</div>
<!-- START OF PHP -->
<?php include ('connectdb.php');
if(isset($_POST['submit3']))
{
$pin = $_POST["pin"];
if ($pin != $pww) {
$errMSG= "WRONG PIN.. TRY AGAIN..";
}
else{
header('Location: myorders.php');
}
} // SUBMIT
?>
</form>
</div>
include('connectdb.php');
$pww = '';
$sql = "SELECT * FROM accounts WHERE usernamee='$userid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($data = $result->fetch_assoc()) {
$pww = $data['passwordd'];
}
}
Try this code, You should declare pww before your if or while brackets, to get acces to this variable.
Related
so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your modal -->
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
I'm very noob in PHP, but i'm really stuck here trying to figure out how to delete a row printed in a while loop here :
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=".$_SESSION["ID"]."";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result3)) {
echo "<h4>" . $row["exp_title"]. "<a href='#' title=''><i class='fa fa-pencil'></i><i onclick='location.href='userprofile.php?deleteID=".$row["auto_id"]."';' class='fa fa-minus-square' data-toggle='modal' data-target='#EXPDELModal' value='delete_exp'></i></a></h4>";
echo "<p>" . $row["exp_detail"]. "</p>";
}
}
else
{
echo "<div class='textfaded2'>Add you experience here.</div>";
}
?>
Note: i want to delete the row using a font-awesome icon and i'm using
'auto_id' as the auto_increment, primary key to define the row i want
to delete.
Here is the code for the modal:
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php?deleteID=<?php '.$row["auto_id"].';?>"
style="width: 100%;" method="post" value="delete_exp">
<button type="button" name="delete" class="btn btn-default" data-
dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-
dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
And finally the query for delete:
<?php
if(isset($_POST['delete']))
{
$sql6="DELETE FROM `user_exp` WHERE auto_id=".$_GET['deleteID']."";
$result=mysqli_query($conn,$sql6) or die();
}
?>
I would like to thank you for taking the time to read this.
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=" . $_SESSION["ID"] . "";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
?>
<h4><?php echo $row["exp_title"]; ?>
<a href='#' title=''>
<i class='fa fa-pencil'></i>
<i onclick='setValue("<?php $row["auto_id"]; ?>");' class='fa fa-minus-square' data-toggle='modal'
data-target='#EXPDELModal'></i>
</a>
</h4>
<p><?php echo $row["exp_detail"]; ?></p>;
<?php
}
} else {
echo "<div class='textfaded2'>Add you experience here.</div>";
}
if (isset($_POST['delete'])) {
if($_POST['deleteID']) {
$sql6 = "DELETE FROM `user_exp` WHERE auto_id=" . $_POST['deleteID'] . "";
$result = mysqli_query($conn, $sql6) or die();
}
}
?>
<script>
function setValue(value) {
document.getElementById("deleteId").value = value;
}
</script>
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php" style="width: 100%;" method="post" name="delete_exp">
<input type="hidden" value="0" name="deleteID" id="deleteId" readonly/>
<button type="submit" name="delete" class="btn btn-default" data-dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
I am trying to do a simple save of data to the database. I have a form in a modal.
When I try to save the data to my database, it will not save. If i place my form out of the modal it will save the data to the database.
I have googled for a solution but found none. The submit button is in my form, so thats not causing the issue.
PHP code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
include 'config.php';
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
}
//read out data from db
$query_string = "SELECT * FROM tbl_Category";
$query = mysqli_query($con, $query_string);
//save data to db
if(isset($_POST['saveCategory'])){
$category = mysqli_real_escape_string($con, $_POST['category']);
$query = "INSERT INTO tbl_Category(category) VALUES('$category')";
$result = mysqli_query($con, $query);
if(!$result){
echo mysqli_error();
}
else{
$category = '';
echo "YES";
}
}
?>
My config.php file:
<?php
$con = mysqli_connect("localhost", "root", "root", "challenger") or die("Error " . mysqli_error($con));
// if($con){
// echo"There is a connection";
// }
?>
Part of my html code:
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<div class="row bg-title">
<div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
<h4 class="page-title">Category</h4>
</div>
<div class="col-lg-9 col-sm-8 col-md-8 col-xs-12">
<ol class="breadcrumb">
<li>Category</li>
<li>Category</li>
</ol>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /row -->
<div class="row">
<div class="col-sm-12">
<div class="white-box">
<h3 class="box-title m-b-0">Overview Category</h3>
<button class="btn btn-danger btn-outline pull-right" data-toggle="modal" data-target="#addCategory">Add Category</button>
<br><br><br>
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['category_id']."</td>";
echo "<td>".$row['category']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
<!-- MODAL WINDOW -->
<div class="modal fade" id="addCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1">
<div class="modal-dialog" role="document">
<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="exampleModalLabel1">Add a new category</h4>
</div>
<div class="modal-body">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="form-group">
<label for="recipient-name" class="control-label">Category Name:</label>
<input type="text" class="form-control" id="recipient-name1" name="category">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="saveCategory" data-dismiss="modal">Create Category</button>
</div>
</form>
</div><!-- end modal body -->
</div>
</div>
</div>
<!-- END MODAL WINDOW -->
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /end Page Content -->
change this
$query = "INSERT INTO tbl_Category(category) VALUES('$category')";
to this
$query = "INSERT INTO tbl_Category(`category`) VALUES('$category')";
I want to fetch row values of the table whenever I click on that row.
In this I have to display data of the table in row..
Whenever I click on the row I need to return other variables values of that row ..
Like When I am clicking on the subject I need to show Message of respective subject from database.
PHP Code to return row values from database.
This snippet will not run as it is PHP code(just add in the snipppet to good formatting )
In this image, Popup Model is not showing correct subject and message ..
I need this particular subject and message in the popup
<?php
include('../config/conn.php');
$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id=$row["user_id"];
$pooler_id=$row["pooler_id"];
$date=$row["date"];
$subject=$row["subject"];
$req=$row['req_id'];
$message=$row['message'];
$sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email'];
}
}
echo ' <tr>
<td>'.$sr.'</td>
<td>'.$username.'</td>
<td><a href="#" data-toggle="modal" data-target="#myModal">'.$subject.'</td>
<td>'.$date.'</td>
</tr>';
$sr++;
}
} else {
echo "0 results";
}
?>
Modal POP Code where Sender Name, Subject and Message should be displayed.
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"> <?php echo $username; ?> <br><br> <?php echo $subject; ?> </h4>
</div>
<div class="modal-body">
<p>
<?php
echo $message;
?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
In your html code
<td><a href="#" data-toggle="modal" data-target="#myModal" onclick="showData('.$pooler_id.')">'.$subject.'</td>
Please pass your unique id in javascript function.
Modal
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject; ?></span> </h4>
</div>
<div class="modal-body">
<p id="msg">
<?php
echo $message;
?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
Script
<script>
function showData(pooler_id)
{
$.ajax({
type: POST,
url : 'some_file.php',
data: {pooler_id: pooler_id},
success: function(response){
var resp = JSON.parse(response);
$('#user_name').html(resp.user_name);
$('#subject').html(resp.subject);
$('#msg').html(resp.msg);
}
});
}
</script>
In some_file.php
$pooler_id = $_POST['pooler_id'];
//get records of your primary key json_decode it
//get details from database
//this will be result fetched from database
$responseData['user_name'] = $name;
$responseData['subject'] = $subject;
$responseData['msg'] = $msg;
echo json_decode($responseData);
If your issue is not resolved yet and you try to avoid usage of JSON:
You could try to add an individual id (e.g. $sr) to your <div class="modal-content"> id property and <a href="#" data-toggle="modal" data-target="#myModal">'.$subject.' call in view. Got this from http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/
Maybe this works for you.
Add to your PHP code <a href="#modal-content-'.$sr.'" data-toggle="modal" data-target="#myModal">'.$subject.':
<?php
include('../config/conn.php');
$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id=$row["user_id"];
$pooler_id=$row["pooler_id"];
$date=$row["date"];
$subject=$row["subject"];
$req=$row['req_id'];
$message=$row['message'];
$sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email']; //note this value is never used
echo ' <tr>
<td>'.$sr.'</td>
<td>'.$username.'</td>
<td>
'.$subject.'
</td>
<td>'.$date.'</td>
</tr>';
$sr++;
}
}
}
} else {
echo "0 results";
}
?>
and to your Modal <div class="modal-content-<?php echo $sr; ?>">:
<!-- Modal content -->
<div class="modal-content-<?php echo $sr; ?>">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject; ?></span> </h4>
</div>
<div class="modal-body">
<p id="msg"><?php echo $message; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
In my search input give me result about keywords like below:
![While of result's] http://i.stack.imgur.com/7VaWl.png
and my target is get more information by open Modal if click any result:
http://i.stack.imgur.com/qU36A.png
I want to select more information from mysql on open model. How can I do this?
Below is my code:
<body>
<h1>חפש פריט</h1>
<br>
<form action="search.php" method="post">
<input type="text" name="keywords" class="form-control" style="width: 250px;margin-left:auto;margin-right:auto;display:inline;" placeholder="הקלד שם של פריט..." /><button type="submit" name="search" class="btn btn-primary">חפש</button>
</form>
<br>
<?php
$keywords = $_POST['keywords'];
$ok = 1;
if(isset($_POST['search'])) {
if(empty($keywords)) {
echo '<div class="alert alert-danger fade in">
×
<strong>שגיאה:</strong> הקלד מילות חיפוש
</div>';
$ok = 0;
}
if(!empty($keywords) && !preg_match("/[A-Za-z0-9א-ת\.\,\_\- ]/", $keywords)) {
echo '<div class="alert alert-danger fade in">
×
<strong>שגיאה:</strong> הזנת תווים לא חוקיים
</div>';
$ok = 0;
}
else if($ok !== 0) {
$search_sql = mysql_query("SELECT * FROM `list` WHERE `Name` LIKE '%$keywords%';");
if(mysql_num_rows($search_sql) < 1) {
$status = "
<div class='alert alert-warning'>
<strong>לא מצאנו</strong> תוצאות עבור חיפוש זה ($keywords)
</div>
";
}
echo '<div class="container">
<h2>תוצאות חיפוש</h2>
<br />
'.$status.'';
while($result = mysql_fetch_array($search_sql)) {
printf('
<div class="well" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">%s</div><br />
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
', $result['Name']);
}
echo '</div>';
}
}
?>
EDIT: Now I am can select the information form the mysql but only with while of "$i"..
with this:
echo '<div class="container">
<h2>תוצאות חיפוש</h2>
<br />
'.$status.'';
$i = 1;
while($result = mysql_fetch_array($search_sql) and $i < 1000) {
printf('
<div class="well" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal'.$i.'">%s</div><br />
<div id="myModal'.$i.'" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">'.$result['Name'].'</h4>
</div>
<div class="modal-body">
<p>'.$result["Description"].'</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">סגור</button> <span style="float:left;">'.$result["Date"].'</span>
</div>
</div>
</div>
</div>
', $result['Name']);
}
But, it is not working good. If I'm click on any-result the modal is open with same result:
i.stack.imgur.com/SKrka.png
So, I want anything more proffesional for this..
Does not matter,
I am just replace the i var to result-id from the mysql
Replace this:
<div class="well" class="btn btn-info btn-lg" data-toggle="modal" data-target="#openM'.$i.'">%s</div><br />
<div id="openM'.$i.'" class="modal fade" role="dialog">
To:
<div class="well" class="btn btn-info btn-lg" data-toggle="modal" data-target="#openM'.$result['ID'].'">%s</div><br />
<div id="openM'.$result['ID'].'" class="modal fade" role="dialog">
Have a nice day! ☺