Storing information to database using JavaScript prompt function - javascript

If I were to do a JavaScript prompt and allow a user to enter the information, how do I store this information into the database?
<?php
if(isset($_GET['cancel_req_id']))
{
echo "<script>var reason = prompt('Reason')</script>";
$res = "<script>document.write(reason)</script>";
$query="UPDATE booking SET status='Cancelled', s_approval='', s_authorize='', cancellation='$res' WHERE req_id='$id'";
$result=$conn->query($query);
echo "<script>alert('Booking cancelled!')</script>";
echo "<script>window.location=('status.php')</script>";
}
?>
this is my current code.

Related

How can I get data from MySQL for each user

I need help. I crated a MySQL database and I connected database with login and registration form. All it´s working. I tried to create page that show all data about user from database. I created tried this code:
<p>Username: <?php echo $_SESSION['username']; ?></p>
<p>Email: <?php echo $_SESSION['email']; ?></p>
<p>Create profile date and time: <?php echo $_SESSION['create_datetime']; ?></p>
But that showed me only username.
I created this from this page.
Can you help me with this? Very much thanks for response!
Inside login.php page you need to assign email and create_datetime to $_SESSION. It should be like
if ($rows == 1) {
while($row = $result->fetch_assoc()) {
$_SESSION['username'] = $row["username"];
$_SESSION['email'] = $row["email"];
$_SESSION['create_datetime'] = $row["create_datetime"];
}
// Redirect to user dashboard page
header("Location: dashboard.php");
}

how to call and echo a value from my database for a a certain user using username to identify the user

I have a table named users on my database which is suppose to hold balance in BTC and Cash in two of its rows
I have a php file which i have used to echo the value for each user using the user's username to identify the particular user, but for some reason the echoed data contains the whole row for BTC and Cash value instead of identifying the user using the username and displaying only the value in the column for BTC and Cash where it suppose to display the balance on my website.
So what i am basically looking for is the code to call the data from my database and using username to identify user, display the value for the user.
This is the html where it suppose to display the user's balance on my website
<!doctype html>
<html>
<div class="logged-user-w text-center avatar-inline">
<div class="logged-user-info-w">
<div class="logged-user-name">
<a class="text-primary" href="profile.php"><?php echo $_SESSION['username']; ?></a>
</div>
//BTC balance//
<div class="logged-user-role">
<a class="text-grey" href="wallet.php"><?php include('new.php')?></a>
</div>
//Cash balance//
<div class="logged-user-role">
<a class="text-grey" href="cashwallet.php"><?php include('new.php')?></a>
</div>
</div>
</html>
hare is the php (new.php) which is suppose to get the values from my database and echo it
<?php
// Server credentials
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Creating mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Checking mysql connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Writing a mysql query to retrieve data
$sql = "SELECT * FROM users bitcoin, cash";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Show each data returned by mysql
while($row = $result->fetch_assoc()) {
?>
<!-- USING HTML HERE : Here I am using php within html tags -->
<p> <?php echo $row["bitcoin"]; ?></p>
<p> <?php echo $row["cash"]; ?></p>
<?php
}
} else {
echo "0 results";
}
// Closing mysql connection
$conn->close();
?>
Your sql query is wrong change it to
$sql = "SELECT * FROM users";
Or if you just want the fields like bitcoin and cash you may write a query as
$sql = "SELECT bitcoin,cash FROM users";
And then echo the respective variables.
As stated by Kunal Raut, change your sql statement, but if you want to select the data based on the username, then use the following.
$sql = "SELECT bitcoin, cash FROM users WHERE username=$_SESSION[‘USERNAME’]”
Or use the user id.
$sql = "SELECT bitcoin, cast FROM users WHERE userid= $_SESSION[‘USERID’]
In both examples, you would need to change the field name to match what you used in the database to hold either the username or the user id.

How to process data using ajax, if there are thousand entries?

I have 1000 entries of users with their email id. I want to send mail to all of them. But I want to do it with ajax.
userdata.php
$con = mysqli_connect("localhost","namename","123456","namename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "Successfully Connected";
}
$selectsql = "SELECT * FROM table_name";
$rowcount = mysqli_num_rows(mysqli_query($con,$selectsql));
$result = $con->query($selectsql);
?>
<div>
<div><h1>Send Mail</h1></div>
<div>
<p>Total Subscribers: <?php echo $rowcount; ?></p>
</div>
</div>
<div>
<table>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> Email ID </td>";
echo "<td>".$row["email"]."</td>";
echo "</tr>";
}
}
?>
</table>
</div>
It returns 1000 emails. Now I want to send these emails one by one to another page (Mail.php).
I know we can do this with ajax. But how? will it increase server load?
Please guide me through it. I want to know what logic I should use to send that much data to another page via ajax without increasing too much load on the server.
In a single ajax call, send all the email ids so that you won't have 1000 calls to Mail.php.
Then in Mail.php, parse the request and extract all email ids and send mails by iterating all email idsin loop.

How to auto-save and auto-update textarea

I'm currently attempting to create a test-website based on the "Secret Diary" project of a web developer course. I'm trying to create a page that saves all of the notes written into a textbox, and displays them when I log in again. Almost everything works - I can start a session and display the saved text when I log in, but the box is deleting the textbox's saved data when the page is loaded. I know that there are better ways of storing the info, I'm just looking for how to get this method to work. This should be all of the relevant code:
mainpage.php:
<?php include("updatediary.php"); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<form method="post">
<textarea class="form-control"><?php echo $diary; ?></textarea>
</form>
<script>
$("textarea").keyup(function() {
$.post("updatediary.php", function(){diaryInput:($("textarea").val());} );
});
</script>
updatediary.php:
<?php
include("connection.php");
$query = "SELECT content FROM users WHERE id='".$_SESSION['id']."' LIMIT 1";
$result = mysqli_query($dbCon,$query);
$row = mysqli_fetch_array($result);
$diary = $row['content'];
if ($_POST['diaryInput']!="") {
$updateQuery = "UPDATE `users` SET `content`='".mysqli_real_escape_string($dbCon, $_POST['diaryInput'])."' WHERE id='".$_SESSION['id']."' LIMIT 1";
if (mysqli_query($dbCon, $updateQuery)) {
echo "saved";
} else {
echo "not saved";
};
}
?>
connection.php:
$dbCon = mysqli_connect("localhost", "owenxwfg_admin", "(password)", "owenxwfg_users");
Any help would be awesome. I personally think that there's a problem with my $.post part.

how to make the result appear in popup widow?

I want to make the result comes out in a popup window.
Code:
<?php
$con=mysqli_connect("localhost","root","1234","fyp");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT password FROM admin WHERE email = '$_POST[email]' AND Admin = '$_POST[admin]'");
while($row = mysqli_fetch_array($result))
echo "your password is : " . " $row['password']" ;
mysqli_close($con);
?>
Is it possible to make it echoed in popup window like javascript alert messages ??
I have tried this but still not working
echo "<script> alert ("<?php echo 'your password is: ' . '$row['password']'?>")</script>";
I found a maybe strange solution for this a while back.
echo "<style onload=\"jsfunc($row['password']);\"></style>";
//in your html or javascript add this function
<script type='text/javascript'>
function jsfunc(data){
alert(data);
}
</script>
the style tag is invisible and will run the function onload, so when its gets echoed. Also I use the style tag because its one of the few tags where the onload works when you echo it like this.
Its a strange solution but it works.
There are some serious flaws in your code, including it being open to SQL Injection. I'd recommend changing it to look more like this:
$con = new MySQLi("localhost","root","1234","fyp");
if($con->connect_errorno) {
echo "Failed to connect to MySQL: ".$sql->connect_error;
}
$email = $sql->real_escape_string($_POST['email']);
$admin = $sql->real_escape_string($_POST['admin']);
$query = "SELECT password FROM admin WHERE email = '$email' AND Admin = '$admin'";
$result = $sql->query($query);
if($result) {
$row = mysqli_fetch_assoc($result);
$pass = $row['password'];
echo '<script> alert("Your password is '.$pass.'");</script>';
} else {
//do some error handling
}
//the closing of a mysqli connection is not required but
mysqli_close($con);
Real escape string is not 100% proof against injection but is a good place to start with sanitising your inputs. Additionally I would strongly advise against storing your passwords in plain text. Please take a look at the PHP sha1() function or the SQL equivalent when storing the passwords initially.
Use this code
<?php
$alert='your password is: '.$row['password'];
?>
<script>
alert("<?php echo $alert; ?>");
</script>
It will work for sure.

Categories

Resources