Passing id in URL to a modal - php

I have a modal with an ID called modal_act_cat so I have anohter modal with a table. I want to pass the id from the table to the modal by clicking the field. My line code to go to the modal is (which opens the modal but with the fields empty):
echo "<tr><td><a href='#modal_act_cat name='cat_nombre_id'>" . $row['cat_nombre_id'] . "</a></td>
but if I add parameters to the URL in href, the modal does not show.
echo "<tr><td><a href='#modal_act_cat?cat_nombre_id=" . $row['cat_nombre_id'] . "' name='cat_nombre_id'>" . $row['cat_nombre_id'] . "</a></td>
See my modal below that I'm trying to get the cat_nombre_id to show the results.
<div class="modal_container" id="modal_act_cat">
<div class="modal">
+
<span class="modal_heading">Actualizar categoría</span>
<a href="#buscar_cat" class="logo">
<i class="icon ion-md-search"></i>
</a>
<form class="form_crear_cat" action="crear_cat.php" method="POST">
<?php
require("conn.php");
if(isset($_GET['cat_nombre_id'])){
$cat_nombre_id = $_GET['cat_nombre_id'];
$query = "SELECT * FROM categorias WHERE cat_nombre_id = '$cat_nombre_id'";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
?>
<input type="text" id="cc1" name="cat_nombre_id" value="" placeholder="Nombre de la Categoría" required><?php echo $row['cat_nombre_id']; ?></input>
<input type="text" name="supCat_nombre_id" value="" placeholder="Nombre de la Super Categoría" required><?php echo $row['supCat_nombre_id']; ?></input>
<textarea name="cat_descripcion" rows="3" placeholder="Descripción de la Categoría"><?php echo $row['cat_descripcion']; ?></textarea>
<?php
}
}
?>
</form>
</div>
</div>
I appreciate your suggestions.

I suppose you are using javascript modal and if it is true you should pass the id using javascript
echo "<tr><td><a href='#modal_act_cat" data-id='".$row['cat_nombre_id']."' name='cat_nombre_id'>" . $row['cat_nombre_id'] . "</a></td>
and then rest of modal content should be ajax based.

Related

how to retrieve data from db and display it in two tables or more in php?

I'm trying to display data from database into multi table in html. here is my code:
php code:
<?php
mysql_select_db("gestion_condidat");
$requet1 ="SELECT * FROM spe_tab WHERE langue ='ar'";
if($res = mysql_query($requet1)){
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_array($res)) {
echo'<img src="images/linep.png"><br>'; /*succisful*/
echo'<CAPTION ALIGN=top><h2><span style="color: #EFB61A">|</span>'.$row['type'].'<span style="color: #EFB61A">|</span></h2></CAPTION> ';
echo'<table id="customers">';
echo' <tr><th> <center> الاختـــصـــاص</center></th><th><center>شروط الالتــحـاق</center></th><th><center>الشهــادة المحصــلة</center></th></tr>';
echo '<tr class="spe">';
echo '<td>'.$row['nom'].'</td>';
echo '<td>'.$row['cond'].'</td>';
echo '<td>'.$row['diplome'].'</td>';
echo '</tr>';
echo '<tr style="display:none"><td colspan="3">'.$row['info'].'</td></tr>';
echo'</table> <br><br>';
}
}}
else {
echo" Il ya une erreur ! <br><br>".mysql_error(); /*error*/
}
?>
html code:
<form method="post" action="admin2.php" enctype="multipart/form-data">
<br><img src="images/linep.png"><br>
<br>
<h2 style="color: #AF7AC5">اضافة تخصص جديد</h2><br>
<div class="cad-1 cad-2 cad-3 cad-4 cad-5 cad-7 cad-10 cad-12">
<span style="float: right">الدورة</span>
<input type="text" class="form-controlmine" name="type" placeholder="مثال: أمن صناعي" required="">
<br><br><span style="float: right">الإختصاص</span>
<input type="text" class="form-controlmine" name="nom" placeholder="مثال: اسعافات أولية" required="">
<br><br><span style="float: right">شروط الإلتحاق</span>
<input type="text" class="form-controlmine" name="cond" placeholder="مثال: جامعي" required="">
<br><br><span style="float: right">الشهادة المحصلة</span>
<input type="text" class="form-controlmine" name="diplome" placeholder="مثال: شهادة تأهيلية" required="">
<br><br><span style="float: right">معلومات إضافية</span>
<input type="text" class="form-controlmine" name="info" placeholder="" required="">
<br><br><input type="submit" name="add" value="إضافة">
</div>
</form>
I have php and html in one page.
I have to create new table every time the $row['type'] changed.
It means that each table will display with the same name of $row['type'] ,but has different parameters $row['nom'], $row['cond'], $row['diplome'], $row['info']
can i duplicate mysql_fetch_array or what should i do ??
Can you please show how can i do it?
You will need an ORDER BY type in the sql so you can detect when $row['type'] changes.
What would you need to do on such a change? You would have to close the previous <table> if there is one (remember, there would not be an open <table> element when the first row is processed). Then start a new <table>. (Similarly remember to echo the final </table> when the loop is complete.)
Beware, id should be unique so this echo'<table id="customers">'; would be problematic.
You'll probably need a couple of new variables: one to keep track of the current type, one to know if this is the first row. You should be able to incorporate this "break" logic within the existing while loop.
I solved the problem ...
this is the solution ...it is complicated but it work
code php:
mysql_select_db("gestion_condidat");
$requet1 ="SELECT * FROM spe_tab WHERE langue ='ar' ORDER BY type ASC ";
if(($res = mysql_query($requet1))&&($ref = mysql_query($requet1))){
if ((mysql_num_rows($res) > 0)&&(mysql_num_rows($ref) > 0)) {
$new = mysql_fetch_array($ref);
while ($row = mysql_fetch_array($res)) {
$row=$new;
if(($row!=null)&&($new!=null)){
echo'<img src="images/linep.png"><br>';
echo'<CAPTION ALIGN=top><h2><span style="color: #EFB61A">|</span>'.$row['type'].'<span style="color: #EFB61A">|</span></h2></CAPTION> ';
echo'<table id="customers">';
echo' <tr><th> <center> الاختـــصـــاص</center></th><th><center>شروط الالتــحـاق</center></th><th><center>الشهــادة المحصــلة</center></th></tr>';
while($row['type']==$new['type']){
echo '<tr class="spe">';
echo '<td>'.$new['nom'].'</td>';
echo '<td>'.$new['cond'].'</td>';
echo '<td>'.$new['diplome'].'</td>';
echo '</tr>';
echo '<tr style="display:none"><td colspan="3">'.$new['info'].'</td></tr>';
$new = mysql_fetch_array($ref);
}
echo'</table> <br><br>';
}}
}}
else {
echo" Il ya une erreur ! <br><br>".mysql_error(); /*error*/
}
?>```

How to call out a data using an id from the displayed table?

I need to call out a the other data from the database and display it through modal but i cant call out the ID from my html table.
Here's my html table and php for posting some of the data's :
<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table class='table'>
<thead>
<th>JOB</th>
<th>STATUS</th>
<th>APPLICATIONS</th>
<th>EDIT</th>
<th>DELETE</th>
</thead>
"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr>
<th>" . $row['job_title'] . "</th>
<td>" . $row['status'] . "</td>
<td>" . $row['applications'] . "</td>
<td><a class='openModal' data-id='".$row['post_ID']."'>edit</a></td>
<td><a href='#'>delete</a></td>
</tr>";
}
echo "</table>"; //Close the table in HTML
?>
And here is my php inside the modal body
<?php
(LINE 121:) $queryEdit = "SELECT * FROM job_posted WHERE post_ID = '$row['post_ID']' ";
$resultEdit = mysql_query($queryEdit, $connection);
while($row1 = mysql_fetch_array($resultEdit)){
?>
<span>Name:</span> <?php echo $row1['job_title']; ?><br>
<span>loc:</span> <?php echo $row1['location']; ?><br>
<span>job type:</span> <?php echo $row1['job_type']; ?><br>
<span>description:</span> <?php echo $row1['job_desc']; ?><br>
<?php
}
?>
Here is the Execution error
Notice: Array to string conversion in C:\xampp\htdocs\talent_space_php\employer\jobs_posted.php on line 121
PS. I just knew recently that the mysql_* will be depreciated, so im planning to finish this first and then convert into MYSQLI or PDO.
I did something similar recently.
The way i worked around it was this:
While generating my rows i added data properties to my modal toggler (in my case a button). Just like you added data-id. However i did this for every value that i need.
I simply used a jquery function to add my data into input fields inside of my modal.
Button table row generation:
<td><a href="#" class="editId" data-hidden="' . $product['id'] . '" data-name="' . $product['name'] .'"
data-short="' . $product['kuerzel'] . '"data-anr="' . $product['anumber'] . '"
data-comment="' . $product['comment'] . '" data-toggle="modal"
data-target="#editProductModal"><i class="far fa-edit"></i></a></td>
My Modal:
<div class="modal-body">
<input type="hidden" name="action2" value="edit">
<div class="form-row">
<div class="col-4">
<div class="form-group">
<input type="hidden" class="form-control" name="hidden" id="modal_hidden" value="">
</div>
<div class="form-group">
<label for="select_p" class="col-form label">Produkt:</label>
<input type="text" class="form-control" name="select" id="select_p" value="" required disabled>
</div>
<div class="form-group">
<label for="new_products_add_name" class="col-form label">New Name:</label>
<input type="text" class="form-control" name="new_name" id="new_products_add_name">
</div>
<div class="form-group">
<label for="new_products_add_short" class="col-form label">New Short:</label>
<input type="text" class="form-control" name="new_short" id="new_products_add_short">
</div>
<div class="form-group">
<label for="new_products_add_number" class="col-form label">New ProductNr:</label>
<input type="text" class="form-control" name="new_number" pattern="{3,11}" id="new_products_add_number">
</div>
<div class="form-group">
<label for="new_products_add_comment" class="col-form label">New Comment:</label>
<input type="text" class="form-control" name="new_comment" id="new_products_add_comment">
</div>
</div>
</div>
</div>
Function:
<script>
$(document).on("click",".editId",function() {
var hidden_value = $(this).data('hidden');
var name_value = $(this).data('name');
var short_value = $(this).data('short');
var anr_value = $(this).data('anr');
var comment_value = $(this).data('comment');
$('#modal_hidden').val(hidden_value);
$('#select_p').val(name_value);
$('#new_products_add_name').val(name_value);
$('#new_products_add_short').val(short_value);
$('#new_products_add_number').val(anr_value);
$('#new_products_add_comment').val(comment_value);
});
</script>
I hope this helps. This might not be the cleanest way of doing this, but it worked for me (and my table contains a fairly large number of rows).
First, I suggest you to not echo out html values.
Second, I would use pdo there are enough examples on the internet. A few tutorials should help you out.
As of your problem:
Change this:
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = '$row['post_ID']' ";
Into one of these:
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = " + $row['post_ID'];
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = {$row['post_ID']} ";

How to associate query with changing variable in PHP

I'm in need of a bit help. I'm trying to find out how to associate a specific query (deletion of a record) with not the id of a record, but the record with which another query (selection of a record) is echoed out.
This line of code totally works when the id is specified, but again I need it for the record that gets called, where the id can skip numbers if I delete a record.
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
I've got a table in my phpmyadmin database with columns 'id', 'pagetitle', 'toevoeging' (addition in Dutch) , 'message'. First one is an INT, rest are varchars/text.
This may be a stupid question, I'm sorry for that. I'm still new to PHP, and to programming in general.
Here is the code. I've commented on lines code to clarify. Thanks you!.
<?php
if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
{
echo '</nav>
<br><br> <div class="inlogscript">
<form action="verstuurd.php" method="post">
<input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
<input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
<textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
<input type="submit" value="Bevestigen" />
</form></div>';
}
?>
<div class="mainContent">
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">' . '<div class="mct">' . '<h2>' . "$pagetitle" .'</h2>' . '</div>' . "<br>" .
'<p class="post-info">'. "$toevoeging" . '</p>' . '<p class="post-text">' . '<br>'. "$message" . '</p>' .'</article>' . '<div class="deleteknop">' . '<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>' . '</div>' ;
} //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled,
//along with a Delete button per echo that has the 3 variables
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
</div>
</div>
Also here is the Insert INTO query of the records. Thanks again!
$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database
if ($MySQLi_CON->query($sql) === TRUE) {
echo "";
} else {
echo "Error: ". $sql . "" . $MySQLi_CON->error;
}
This won't be sufficient but, to begin with your echo :
echo '<article class="topcontent">
<div class="mct">
<h2>' . $pagetitle .'</h2>
</div><br>
<p class="post-info">'. $toevoeging . '</p>
<p class="post-text"><br>'.$message.'</p>
</article>
<div class="deleteknop">
<form method="post">';
// you ll want to use $_POST["id"] array to delete :
echo '<input type="hidden" name="id" value="'.$row['id'].'">
<input name="delete" type="submit" value="Delete Now!">
</form>
</div>' ;

how to create a table in php with mysql?

I am trying to create a table for a leaderboard which displays users high scores for a game. I am using my sql to hold the data and php to get that data and put it into a table. when i go to this page on the site however, the page loads but all the php code shows up instead of the table. i dont know what im doing wrong, how do i get the php to display the table?
Sokoban Game
<body>
<div class="container">
<div class="header">
<h1>Sokoban</h1>
</div>
<div class="nav">
<div class="nav_wrapper">
<ul>
<li>Home</li><li>
Play</li><li>
Help</li><li>
Leaderboards
<ul>
<li>High Scores</li>
<li>Top Levels</li>
<li>Most completed</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="leftSideBar">
</div>
<div class="mainContent">
<h2>High Scores</h2>
<div class = "leaderboard">
<?php
// Get a connection for the database
require_once('../mysqli_connect.php');
// Create a query for the database
$query = "SELECT rankNo, username, highScores FROM Users INNER JOIN leaderboardHighScores";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
echo '<table>
<tr><td><b>Rank</b></td>
<td><b>Username</b></td>
<td><b>High Score</b></td></tr>';
// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = mysqli_fetch_array($response)){
echo '<tr><td>' .
$row['rankNo'] . '</td><td>' .
$row['username'] . '</td><td>' .
$row['highScores'] . '</td><td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
}
// Close connection to the database
mysqli_close($dbc);
?>
</div>
</div>
<div class="rightSideBar">
<h3>Sign in</h3>
<input type="text" placeholder="Username" class="txtBox" /></br>
<input type="text" placeholder="Password" class="txtBox" /></br></br>
<input type="button" value="login" class="btn"/>
<h2>Sign up<h2>
<input type="button" value="Create account" class="btn"/>
</div>
</div>
</body>
</html>

POST and GET at the same time

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL
This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.

Categories