jquery mobile and append/injecting HTML - php

check this out.
<?php
include('configdb.php');
if(isset($_POST['submit'])) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$query = "SELECT * FROM user WHERE email='$email' AND password='$password' AND com_code IS NULL";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
if( $num_row == 1 ) {
$_SESSION['user_name']=$row['username'];
header("Location: member.php");
exit;
}
else {
?>
<script type="text/javascript">
$('.error').append('This will display if there was an error');
</script>
<?php
}
}
?>
<div data-role="content" data-theme="b"><p class="error"></p></div>
So as a newbie dev I'm playing around and trying to figure out why certain things work and why other's don't.
The idea here is that if the database check fails jQuery injects a message into the paragraph tag with the class of error. (You can't do that with PHP echo can you?)
Works great until I add the jQuery Mobile framework. Then all it does is refresh and nothing updates. I can't figure out why the Mobile framework is preventing this from happeneing.
Any advice would be appreciated. Thanks guys.

Why can't you use PHP to do it? It looks pretty trivial:
<?php if (isset($num_row) && $num_row == 0) { ?>
<div data-role="content" data-theme="b">
<p class="error">This will display if there was an error</p>
</div>
<?php } ?>

Related

How to update the data from mysql database with out refreshing the page

I have a website and I need to put a chat box but when someone write the users has to refresh the whole page to read the text but I need to make it automatically update the data please help me.
Now this is the code:
<?
session_start();
include("includes/config.php");
if($_GET['with']){
if($_SESSION['login']){
if($_SESSION['login'] == $_GET['with']){
header("Location: index.php");
}else{
$id = $_SESSION['login'];
$with = intval($_GET['with']);
if($_POST['submit']){
$text = $_POST['text'];
if(empty($text)){
}else{
$query = mysqli_query($connect,"INSERT INTO chat(`from`,`to`,`topic`) VALUES('$id','$with','$text')");
}
}
?>
<form method="post" action="chat.php?with=<?=$with?>">
<textarea name="text" placeholder="Write Here..." style="text-align:right;resize:none;width:100%;height:200px;font-size:24">
</textarea>
<br/>
<input type="submit" name="submit" value="Send"/>
</form>
<div id="chat">
<?
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$id'");
$f = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$with'");
$ff = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM chat order by id desc");
while($fetch = mysqli_fetch_array($query)){
if($fetch['from'] == $with && $fetch['to'] == $id or $fetch['from'] == $id && $fetch['to'] == $with){
if($fetch['from'] == $f['id']){
echo "<div style='word-wrap: break-word;'>".$f['fname']." ".$f['lname'].":<br/>".$fetch['topic']."</div>";
}
if($fetch['from'] == $ff['id']){
echo "<div style='max-width:200px;word-wrap: break-word;'>".$ff['fname']." ".$ff['lname'].":<br/>".$fetch['topic']."</div>";
}
}
}?>
</div>
<?}
}else{
header("Location: index.php");
}
}else{
header("Location: index.php");
}
?>
With only using php you cannot achieve what you want. However you can use ajax to accomplish what you asked for. An there is examples with source codes provided. Here is some of them:
https://css-tricks.com/jquery-php-chat/ (i recommend this/ well documented and with source code as well)
http://code.tutsplus.com/tutorials/how-to-create-a-simple-web-based-chat-application--net-5931
http://www.sitepoint.com/9-crazy-jquery-chat-chat-box-plugins/ (lis of ajax/php based chat applicatiosn)
Ajax will help you. But it is a bad practice the way you mix html javascript and php in the same code; Like this, it will be difficult for many people to help you to adapt your code with ajax.

show all users projects stored in the database

i am making a profile.php page and i would like it to show the user all his projects, this is my first time doing something like this, and i cant find a solution for it
code to show the projects :
$username = $_SESSION['username'];
if ($_SESSION['type'] = "developer"){
$q = "SELECT * FROM `projects` WHERE `developer` = '$username'";
$result = mysqli_query($con,$q);
$row = mysqli_fetch_array($result);
$numrows = mysqli_num_rows($result);
if(empty($numrows)){
echo'
<div class="row">
<div class="col-lg-12 newp">
<p><span class="glyphicon glyphicon-plus plus"></span>Add a new project</p>
</div>
</div>';
}else{
$p_id = $row['project_id'];
$p_name = $row['project_name'];
$p_owner = $row['owner'];
$p_developer = $row['developer'];
$p_price = $row['price'];
$p_date_started = $row['date_started'];
$p_date_end = $row['date_end'];
$p_paid = $row['paid'];
//foreach project the user has do this :
echo"
<div class=\"row\">
<div class=\"col-lg-12\">
<p>$p_name </br>owner : $p_owner, developer : $p_developer, price : $p_price$</br>started : $p_date_started, ends :$p_date_end, paid :$p_paid</p>
</div>
</div>";
}
}
} else {
while($row = mysqli_fetch_array($result)) {
$p_id = $row['project_id'];
...
Besides the other answer given:
You're presently assigning instead of comparing with
if ($_SESSION['type'] = "developer"){...}
^
which the above will fail and everything inside that conditional statement and should read as
if ($_SESSION['type'] == "developer"){...}
^^
with 2 equal signs.
Make sure the session has also been started, it's required when using sessions.
session_start();
You're also open to an SQL injection. Use a prepared statement:
https://en.wikipedia.org/wiki/Prepared_statement

Login Page Not Connected to Registration page

the registration form is connected to the database via db.php but I am having trouble in submitting the login details.
<html>
<head>
<?php
include('db.php');
$username = #$_POST['username'];
$password = #$_POST['password'];
$submit = #$_POST['submit'];
the main problem is after the submit button is clicked by an existing user it should give the message but there's problem in the if statement, because on the wamp server its showing only the else message i.e. Error.
if ($submit)
{
$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
if (mysql_num_rows($result)) {
$check_rows = mysql_fetch_array($result);
$_POST['username'] = $check_rows['username'];
$_POST['password'] = $check_rows['password'];
echo "<center>";
echo "You are now Logged In. ";
echo "</center>";
}
else {
echo "<center>";
echo "No User found. ";
echo "</center>";
}
}
else echo "Error";
?>
</head>
<body>
<form method="post">
Username : <input name="username" placeholder="Enter Username" type="text"><br></br>
Password : <input name="password" placeholder="Enter Password" type="password"><br>
<input type="submit" value="Submit">
</body>
</html>
You want get $_POST with name submit, but do not send it to the form
Try change
<input type="submit" value="Submit">
to
<input type="submit" name="submit" value="Submit">
Firstly this is old style of php/mysql. So look at PDO on php.net seeing as you are setting out on new project it really wont be hard to make the change now rather than later.
Now onto your issue. if you intend on carrying on with your old method try this.
$sql = "SELECT * FROM user WHERE username=' . $username . ' AND password=' . $password . '";
// check the query with the die & mysql_error functions
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_num_rows($query);
// checking here equal to 1 In a live case, for testing you could use >= but not much point.
if ($result == 1) {
// Checking needs to be Assoc Now you can use the field names,
// otherwise $check_rows[0], $check_rows[1] etc etc
$check_rows = mysql_fetch_assoc($query); // oops we all make mistakes, query not result, sorry.
// This is bad but for example il let this by,
// please dont access user supplied data without
// validating/sanitising it.
$_POST['username'] = $check_rows['username'];
$_POST['password'] = $check_rows['password'];
} else {
// do not logged in here
}
The same in PDO
$sql=" Your query here ";
$pdo->query($sql);
$pdo->execute();
$result = $pdo->fetch();
if ($result = 1) {
// do login stuff
} else {
// no login
}
Remember though that you need to set up PDO and it may not be available on your server by default (older php/mysql versions) but your host should be happy enough to set them up.

MySQL is not receiving information. Using MySQLi extension

<?php
include_once("php_includes/check_login_status.php");
if($user_ok != true){
header("location: login.php");
exit();
}
?>
<?php
$id = "SELECT id FROM users WHERE username='$log_username'";
if(isset($_POST["signupbtn"])) {
if ($log_username) {
/// getting data from submitted form into local variables
$x = preg_replace('#[^a-z 0-9]#i', '', $_POST['xbox']);
$p = preg_replace('#[^a-z 0-9]#i', '', $_POST['psn']);
$s = preg_replace('#[^a-z 0-9]#i', '', $_POST['steam']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR GAMER PROFILES
$sqli = "SELECT username FROM player WHERE xbox='$x' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$x_check = mysqli_num_rows($query);
// -------------------------------------------
if ($x_check > 0){
echo "Xbox Gamer-Tag already linked to a user on this website";
exit();
} else if (is_numeric($x[0])) {
echo 'Xbox Gamer-Tag cannot begin with a number';
exit();
}
$sqli = "SELECT username FROM player WHERE psn='$p' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$p_check = mysqli_num_rows($query);
// -------------------------------------------
if ($p_check > 0){
echo "PSN User already linked to a user on this website";
exit();
} else if (is_numeric($p[0])) {
echo 'PSN User cannot begin with a number';
exit();
}
$sqli = "SELECT username FROM player WHERE steam='$s' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$s_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if ($s_check > 0){
echo "Steam account already linked to a user on this website";
exit();
} else if (is_numeric($s[0])) {
echo 'Steam account cannot begin with a number';
exit();
} else { $sqli = "INSERT INTO player (id, username, xbox, psn, steam, ip, created, lastupdated, notecheck)
VALUES ('$id','$log_username','$x','$p','$s','$ip',NOW(),NOW(),NOW())";
$query = mysqli_query($db_conx, $sqli);
}
echo "Gamer Profiles Updated";
exit();
if (!file_exists("p_player/$log_username")) {
mkdir("p_player/$log_username", 0755);
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="templates/default/style/style.css">
<style type="text/css">
#profiles{
margin-top:24px;
}
#profiles > div {
margin-top: 12px;
}
#profiles > input,select {
width: 200px;
padding: 3px;
background: #F3F9DD;
}
#profiles {
font-size:18px;
padding: 12px;
}
</style>
<script src="js/main.js"></script>
<script src="js/ajax.js"></script>
</head>
<body>
<?php include_once("templates/default/template_pageTop.php"); ?>
<div id="pageMiddle">
<h3>Gamer Profiles</h3>
<form action="player.php" method="POST" name="profiles">
<div>Xbox Gamer-tag: <input type="text" name="xbox"></div>
<div>PSN User: <input type="text" name="psn"></div>
<div>Steam User: <input type="text" name="steam"></div>
<input type="submit" name="signupbtn">
</form>
</div>
<?php include_once("templates/default/template_pageBottom.php"); ?>
</body>
</html>
The $log_username variable comes from the top php script and every thing is fine with it as I can echo the variable and comes back with the logged user on this page.
so basicaly nothing is being written to my database on submitting the button and I am really stumped with it tbh. I am using the MySQLi extension within php 5 as I have been following a tutorial on things and have came to the end of the tutorial and now I am by myself. I was going to included a shot of my database below to prove that the names on the database is fine but with the fact that my rep is 9 I can't add one to make your lives easier as you need to b 10.
have I got a } in the wrong place or labeling something MySQL when it should be MySQLi.
When I submit the form the mkdir is made in the folder and I get sent to the page with the echo "Gamer Profiles Updated"
Thank you to anyone who tries to solve this problem out I have.
I have been trying to do this for hours tbh and from what i have done everything checks fine and it should be working in theory but it just doesn't work. Thank you once again for looking at this and I look forward to your responses.
Change:
$id = "SELECT id FROM users WHERE username='$log_username'";
to:
$result = mysqli_query($db_conx, "SELECT id FROM users WHERE username='$log_username'") or die(mysqli_error($db_conx));
$row = mysqli_fetch_assoc($result) or die ("$log_username not found");
$id = $row['id'];

If MySQL Result Empty display a confirm box

I'm using some crazy mixture of PHP/JavaScript/HTML/MySQL
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
// Display a confirm box saying "Not everyone has entered a bid, continue?"
}
// If confirmed yes run more queries
// Else nothing
What is the best way to have this confirm box display, before completing the rest of the queries?
if($row != NULL) {
?>
<script>alert("not everyone has submitted their bid.");</script>
<?php
}
or
<?php
function jsalert($alert_message){
echo "<script type='text/javascript'>alert('".$alert_message."');</script>";
}
if($row!=null){
jsalert("Not everyone has submitted their bid.");
}
?>
You can't do this in 1 continuous block, as all of the PHP will execute before the confirm (due to server vs. client).
You will need to break these into 2 separate steps and have the client mediate between them:
part1.php:
<?php
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row != NULL) { ?>
<form id="confirmed" action="part2.php" method="post">
<noscript>
<label>Not everyone has entered a bid, continue?</label>
<input type="submit" value="Yes">
</noscript>
</form>
<script type="text/javascript">
if (confirm("Not everyone has entered a bid, continue?")) {
document.getElementById('confirmed').submit();
}
</script>
<?
} else {
include_once('part2.php');
}
?>
part2.php:
<?php
// assume confirmed. execute other queries.
?>
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
// more queries here
} else {
echo "<script>alert('Empty result');</script>";
}
Play with this code and you will get it to work eventually. I know you are not looking for just alertbox , instead you are looking for something like "yes or no" informational box. So check this out.
<?php
?>
<html>
<head>
<script type="text/javascript">
function displayBOX(){
var name=confirm("Not everyone has entered a bid, continue?")
if (name==true){
//document.write("Do your process here..")
window.location="processContinuing.php";
}else{
//document.write("Stop all process...")
window.location="stoppingProcesses.php";
}
}
</script>
</head>
<?php
$query = "SELECT * faculty SET submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
echo "<script>displayBox();</script>";
}
?>

Categories