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.
Related
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");
}
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.
I have created a php script that submits a query and downloads the results in a CSV. I have created a bootstrap button where users can click to download the file. Some of the reports take longer to create and I wanted to show the user some sort of status that the script is running in the background.
I tried something simple like BlockUI from inside the PHP script. Using echo '<script type="text/javascript">$.blockUI();</script>'; in the beginning of the script and echo '<script type="text/javascript">$.unblockUI();</script>'; at the end but it didn't work.
Can someone help? I don't need a progress bar or anything fancy. I just need to show some type of status while the php script is running.
HTML:
...
<td class="pull-right"><a type="button" href="report1_download_csv.php" class="btn">Download</a></td>
...
PHP:
<?php
/* Set up and execute the query. */
$sql = "SELECT
FROM TABLE ";
$stmt = sqlsrv_query( $conn, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
foreach($row AS $key => $value){
$pos = strpos($value, '"');
if ($pos !== false) {
$value = str_replace('"', '\"', $value);
}
$out .= '"'.$value.'",';
}
$out .= "\n";
}
sqlsrv_free_stmt($results);
sqlsrv_close($conn);
// Output to browser with the CSV mime type
header("Content-type: text/x-csv");
$date = date('m-d-Y-His');
header("Content-Disposition: attachment; filename=Report1_{$date}_UTC.csv");
echo "Column1, Column2\n";
echo $out;
?>
use ajax to send request to server to do operation then show a spinner or any thing else untill server give you response.
I currently have a ajax script to run a notiload.php file which runs every 5 seconds to check if theres a change in the database, if theres a change it then outputs "1 New" for a message. However as I will be introducing a notification sound in that file. It will chime every 5 secconds when the file is loaded. Of course this will piss people off. So I need the script to check if theres a change in the database. I have a time and date stamp of every message, with the message being viewed as 2 and not as 1. So if theres a change in the database I will need it to call that file or play a sound.
here is my ajax script inside the main page
<script type="text/javascript">
$(document).ready(function(){
refreshTable<?= $FriendName->id ?>();
});
function refreshTable<?= $FriendName->id ?>(){
$('#NotiLoad<?=$FriendName->id?>').load('elements/notiload.php?id=<?=$FriendName->id?>', function(){
setTimeout(refreshTable<?= $FriendName->id ?>, 5000);
});
}
</script>
<div id="NotiLoad<?= $FriendName->id ?>" style="display: inline-block;"></div>
And here is the php of notiload.php
<?php
session_start();
// redirect to index page if not superuser
$con = mysqli_connect('localhost','root','','');
if (!$con) {
die('Could not connect to Socialnetwk DB: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
?>
<?php
$uid = $_GET['id'];
$mid = $_SESSION['user']['id'];
?>
<?php $usrload = $con->query("SELECT * FROM message WHERE sender = $uid AND recipient = $mid AND seen = '2' OR sender = $mid AND recipient = $uid AND seen = '2'"); ?>
<?php while($FriendName = $usrload->fetch_object()): ?>
<h6 style="display:inline-block;font-size: 10px;color:#37BC9B">1 New</h6>
<?php endwhile;?>
So I am creating an inbox in PHP and It displays Mail Id and Subject.
If you click that division(containing mail id and subject), it opens up a new page, read. PHP where the body and attachments are displayed.
I am facing some problem in getting to know which division is being clicked and how to display the mail of that particular person.
My concerned code is:
<?php
$conn = connect(); //Connects to the database
$sql = "select senderId, subject, body, attachment from mail where receiverId = '".$_SESSION["Email"]."' ";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo "<div class='mail' onclick='location.href=`read.php`;'>";
echo $row["senderId"] . " - " . $row["subject"];
echo "</div>";
echo "<hr />";
}
$conn->close();
?>
I have an idea of storing the variables using SESSION as:
if (isset($_GET['div'])) {
$_SESSION["body"] = $row["body"];
$_SESSION["attachment"] = $row["attachment"];
}
But where do I place the latter part of the code and suggest me if I can make an modifications please.
You can echo out the id during the while loop iteration.
echo "<div class='mail' onclick='location.href=`read.php?id=". $row['id'] ."`;'>";
And inside the read.php file, you can then grab that value by using $_GET['id'].