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>
Related
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.
I would like to specify the duration of the display of a form. Suppose there is a variable with the value x=(10), whenever I call this page, the timer should be based on the current time and the form should be displayed for 10 minutes and after this time the form should be sent automatically.
<?php
include 'db_conn.php';
if (isset($_GET['id'])) {
$var=$_GET['id'];
$query="SELECT * FROM addquiz WHERE quz_id='$var' ";
$db=mysqli_query($conn,$query);
$res=mysqli_fetch_array($db);
$noquestions=$res['noquestion'];
$quiz=$res['title'];
}?>
<div class="container">
<form method="POST" class="form-horizontal" >
<div style="margin-left: 30%;"><b>Your Quiz: <?php echo $quiz;?></b>
<input type="text" name="user_name" placeholder="Enter Your Name" style="height: 30px; width: 30%;"></div>
<div class="row">
<div class="col-md-12">
<div class="panel" style="margin:5%">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="checkbox" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="checkbox" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="checkbox" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="checkbox" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<div class="button" style="margin-left: 60%;"> View Result </div>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
$name=$_POST['user_name'];
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`,`user_name`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "', '";
$query.=$name . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
It depends on what you are trying to achieve.
As you say "at each visit of the page" it could be done like this with javascript:
window.onload = function () {
window.setTimeout(function () {
document.form.submit();
}, 5000);
};
Further you could set the timeout from your php variable and like the comment says above, you could validate the taken time on the server side with php to make sure no one is cheating.
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']} ";
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.
I am trying to submit information to a mysql database. When the page loads it should show all the data currently in the mysql table and when the user enters the information it should update the table without refreshing the page. Here is what i have so far.
for my homepage
<body>
<script src="jquery.js" type="text/javascript"></script>
<script src="script.js" typer="text/javascript"></script>
<div class="wrapper">
<h3>Conversation Portal</h3>
<div class="entry_wrapper">
<h4> Enter Conversation Here</h4>
<ul>
<li>
<label>English:</label>
<input class="inputbox" type="text" size="50" id="english"/>
</li>
<li>
<label>Sanskrit:</label>
<input class="inputbox" type="text" size="50" id="sanskrit"/>
</li>
<li>
<label>Date:</label>
<input class="inputbox" type="text" size="50" id="datepicker"/>
</li>
</ul>
</div>
<div class="response_wrapper">
<textarea id="responseText"></textarea>
</div>
<input type="button" value="submit" id="call_bck_btn"/>
</div>
</body>
For my javascript i have:
$(document).ready(function(){
$('#call_bck_btn').click(function(){
$.post("ajax.php" ,
{
english :$('#english').val(),
sanskrit :$('#sanskrit').val()
},
function(data)
{
$('#responseText').val(data);
}
);
});
});
And for my php:
<?php
$method=$_SERVER['REQUEST_METHOD'];
if(strtolower($method)=='post')
{
$english = addslashes($_POST['english']);
$sanskrit = addslashes($_POST['sanskrit']);
$connect=mysql_connect('localhost','root','');
$db=mysql_select_db('test2');
$sql="insert into conversation(English,Sanskrit) values('$english','$sanskrit')";
$result=mysql_query("select*from conversation");
echo "<table border='1'>
<tr>
<th>English</th>
<th>Sanskrit</th>
</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['English'] . "</td>";
echo "<td>" . $row['Sanskrit'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Thanks for any ideas on what to do.
Well, your pretty much there with what you want. If you ran this, I think it would work (roughly). Although there are quite a few (major!) flaws in your code. Just to sum them up first:
Don't use root access to your database, EVER!! This is a MAJOR security hazard.
Don't use the mysql extension, it has officially been deprecated for several PHP versions now; instead use either PDO or MySQLi (both are far better)
Use prepared queries (available in PDO and MySQLi) to make your queries safer.
Fix your HTML; Don't display your resulting HTML table inside a tag. This is incorrect HTML. Rather, use a div for such a thing. Likewise,
In short, I would rewrite it to:
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="script.js" type="text/javascript"></script>
</head>
<body>
<h4> Enter Conversation Here</h4>
<form id="conversationForm">
<ul>
<li>
<label>English:</label>
<input class="inputbox" type="text" size="50" id="english"/>
</li>
<li>
<label>Sanskrit:</label>
<input class="inputbox" type="text" size="50" id="sanskrit"/>
</li>
<li>
<label>Date:</label>
<input class="inputbox" type="text" size="50" id="datepicker"/>
</li>
</ul>
</div>
<input type="button" value="submit" id="call_bck_btn"/>
</form>
<div id="resultTbl">
</div>
</body>
</html>
script.js:
$(function() {
$('#conversationForm').submit(function(e) {
$.post('ajax.php', $(this).serializeArray(), function(data, status, jqXHR) {
$('.resultTbl').html(data);
});
});
});
ajax.php:
<?php
if( (isset($_POST['english']) && strlen($_POST['english']) > 0) && (isset($_POST['sanskrit']) && strlen($_POST['sanskrit']) > 0) ) {
$mysqli = new mysqli('localhost','username','password', 'test2');
if($stmt = $mysqli->prepare("INSERT INTO `conversation` (English, Sanskrit) VALUES (?,?)")) {
$stmt->bind_param('ss', $_POST['english'], $_POST['sanskrit']);
$stmt->execute();
}
$result = $mysqli->query("SELECT English, Sanskrit FROM `conversation`", MYSQLI_STORE_RESULT);
echo '<table>';
while($row = $result->fetch_array()) {
echo '<tr>';
echo '<td>', $row['English'], '</td>';
echo '<td>', $row['Sanskrit'], '</td>';
echo '</tr>';
}
echo '</table>';
?>