The code is shown below:
if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}
?>
<script type="text/javascript">
if('<?php echo $error; ?>' != '' && '<?php echo $error; ?>' != undefined) {
alert("<?php echo $error; ?>");
}
</script>
Problem is that whenever I upload an unsupported image type like a .tiff image, the alert box gets displayed. This works the way I want it to work. But if i reload it, then it displays the alert box once again with the same message. It doesn't show up the third time I reload. I want alert message to show up only once, not twice or thrice..
change code to:
<?php
if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}
if(isset($error)){
?>
<script type="text/javascript">
alert("<?php echo $error; ?>");
</script>
<?php
}
?>
OR
<?php
if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}
if(isset($error)){
echo "<script type='text/javascript'> alert('<?php echo $error; ?>'); </script>";
}
?>
Related
I am creating wishlist where I am sending data via ajax but it's not returning data as I implemented in the code I fail to figure out what is the main reason behind this. Here is the code
<li><span><i style="color:red;" class="<?php
if(isset($_SESSION['email'])){
$selwishlist = $con->prepare("SELECT * FROM wishlist WHERE user_id =:userid and product_id =:productid ");
$selwishlist->bindParam(":userid",$userid);
$selwishlist->bindParam(":productid",$id);
$selwishlist->execute();
$selresultwishlis = $selwishlist->rowCount();
if($selresultwishlis > 0){
echo "fa fa-heart";}else{
echo "ion-android-favorite-outline";
}}else{
echo "ion-android-favorite-outline";
}
?> wishlist-click" id="wishlist-product-id-<?php echo $id; ?>"></i></span>
</li>
if(isset($_SESSION['email'])){
$email = $_SESSION['email'];
$userdetail = $con->prepare("SELECT * FROM user WHERE email = :email");
$userdetail->bindParam(":email",$email);
$userdetail->execute();
$userres = $userdetail->fetch();
$userid = $userres['id'];
}
else{
$email = '';
$userid = '';
}
This is my input button Where first I check if the session is running or not an then check if the item is in the user wishlist or not and according to that I change the class of wishlist icon
$(".wishlist-click").click(function(e){
e.preventDefault();
var el = $(this);
alert('I am clicked')
var ids = this.id;
var splitids = ids.split('-');
var wishlistid = splitids[3];
$.ajax({
url:'wishlistajax.php',
type:'POST',
data:{ids:wishlistid},
success:function(response){
if(response == 1){
alert('1');
}else if(response == '0'){
alert('0');
}else if(response == '3'){
alert('its 3');
}
else{
alert('Not in All');
}
}
});
});
This is my jquery code and I am getting Not in both in response AND HERE IS MY AJAX CODE
<?php
session_start();
if(isset($_SESSION['email'])){
echo "1";
exit();
if(isset($_POST['ids'])){
$email = $_SESSION['email'];
$productid = htmlspecialchars($_POST['ids']);
include "conn.php";
$userdetail = $con->prepare("SELECT * FROM user WHERE email = :email");
$userdetail->bindParam(":email",$email);
$userdetail->execute();
$userres = $userdetail->fetch();
$userid = $userres['id'];
$ip = $_SERVER['REMOTE_ADDR'];
$insertquery = $con->prepare("INSERT INTO wishlist (product_id, user_id) VALUES (:productid, :userid)");
$insertquery->bindParam(":productid",$productid);
$insertquery->bindParam(":userid",$userid);
$insertquery->execute();
echo 'Item added';
exit();
}}
else{
echo '2';
header("location:loginregister.php");
exit();
}
echo '3';
exit();
?>
in index.php
<?php
session_start();
$_SESSION['user_login'] = false;
}
?>
<li>
<div style="margin-left: 15px ;margin-right:15px ; text-align: center">
<?php
if (!$_SESSION['user_login']){
echo '<a href="View/login.php" class="btnprofile" style="background-image:url(./img/defaullt_profile.jpg)" ></a>';
}
else{
echo '<a href="View/profile.php" class="btnprofile" style="background-image:url(coba.jpg)" ></a>';
}
?>
</div>
</li>
in login.php
just see when the $cekrole == 'member'
<?php
include_once '../Dao/UserDaoImpl.php';
include_once '../Entity/USER.php';
include_once '../util/PDOUTIL.php';
session_start();
$loginPressed = filter_input(INPUT_POST, 'btnLogin');
if (isset($loginPressed)) {
$userDao = new UserDaoImpl();
$username = filter_input(INPUT_POST, 'txtUsername');
$password = filter_input(INPUT_POST, 'txtPassword');
$md5Password = md5($password);
$userLogin = new User();
$userLogin->setUsername($username);
$userLogin->setPassword($md5Password);
/* #var $arrResult User */
$arrResult = $userDao->login($userLogin);
$longname = $arrResult['longname'];
$email = $arrResult['email'];
$gender = $arrResult['gender'];
$dateofbirth = $arrResult['dateofbirth'];
$cekrole = $arrResult['role'];
$profile = $arrResult['profile'];
if (isset($arrResult) && !empty($arrResult['name'])) {
if ($cekrole == 'admin') {
$_SESSION['user_login'] = TRUE;
$_SESSION['user_name'] = $arrResult['username'];
header('location:homeadmin.php');
} elseif ($cekrole == 'employee') {
$_SESSION['user_login'] = TRUE;
$_SESSION['user_name'] = $arrResult['username'];
$_SESSION['namauser'] = $username;
header('location:homeemployee.php');
} elseif ($cekrole == 'member') {
$_SESSION['user_login'] = TRUE;
$_SESSION['user_name'] = $arrResult['username'];
$_SESSION['profile'] = $profile;
$_SESSION['longname'] = $longname;
$_SESSION['email'] = $email;
$_SESSION['dateofbirth'] = $dateofbirth;
$_SESSION['gender'] = $gender;
header('location:../index.php');
}
}
else{
$message = 'Id atau Password salah';
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
?>
can someone help me :( , see the login.php and just see when role == member
why when the login is true and set session['user_login'] = TRUE and header to index.php, but the session['user_login'] in index.php still FALSE
sory my english so bad :D
<?php
session_start();
if (!isset($_SESSION['user_login'])) { //if session doesn't exist, set user_login to false
$_SESSION['user_login'] = false;
}
?>
So when session doesn't exist yet, or user isn't logged then $_SESSION['user_login'] = false else $_SESSION['user_login'] is true and you can continue your workflow.
Here when I click post button it inserts a random value on database.
If a value already exists on database then show error. It works fine.
But I want to add 2/3 characters at the end of value if it already exists on database. If $check == 1 then I want to add some characters at the end of the value instead of showing alert. How to do this?
<?php
$con = mysqli_connect("localhost","root","","post") or die("unable to connect to internet");
if(isset($_POST['submit']))
{
$slug = $_POST['rand'];
$get_slug = "select * from slug where post_slug='$slug' ";
$run_slug = mysqli_query($con,$get_slug );
$check = mysqli_num_rows($run_slug );
// if $check==1 then i want to add 2 characters at the end of $slug .
if($check == 1)
{
// instead of showing alert i want to add 2 more characters at the end of that value and and insert it on database
echo "<script> alert('something is wrong') </script> ";
exit ();
}
else
{
$insert ="insert into slug (post_slug) values ('$slug') ";
$run = mysqli_query($con,$insert);
if($run)
{
echo "<p style='float:right;'> Posted successfully </p>";
}
}
}
?>
<form method="POST" >
<?php
$result = "";
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$chararray = str_split($chars);
for($i = 0; $i < 7 ; $i++)
{
$randitem = array_rand($chararray);
$result .= "".$chararray[$randitem];
}
echo $result ;
?>
<input type="hidden" value="<?php echo $result;?>" name="rand" />
<span class="input-group-btn">
<button class="btn btn-info" type="submit" name="submit">POST</button>
</span>
</form>
just run update query if $check == 1
if($check == 1){
$newSlug = $slug."xy";
$update = "update slug set post_slug = '".$newSlug."' where post_slug = '".$slug."'";
$run = mysqli_query($con,$update );
echo "<script> alert('Updated Successfully') </script> ";
exit ();
}
This is helpful for you
<?php
$con = mysqli_connect("localhost","root","","post" ) or die
( "unable to connect to internet");
if(isset($_POST['submit'])){
$tmp_slug = $_POST['rand'];
$slug = $_POST['rand'];
while(check_exiest($tmp_slug))
{
$tmp_rand = rand(11,99);
$tmp_slug = $slug.$tmp_rand;
}
$insert ="insert into slug (post_slug) values ('$tmp_slug') ";
$run = mysqli_query($con,$insert);
if($run)
{
echo "<p style='float:right;'> Posted successfully </p>";
}
}
public function check_exiest($slug)
{
$get_slug = "select * from slug where post_slug='$slug' ";
$run_slug = mysqli_query($con,$get_slug );
$check = mysqli_num_rows($run_slug );
if($check >= 1)
{
return true;
}
else
{
return false;
}
}
?>
Just few modification in your code to insert new value.
<?php
$con = mysqli_connect("localhost","root","","post") or die("unable to connect to internet");
if(isset($_POST['submit']))
{
$slug = $_POST['rand'];
$get_slug = "select * from slug where post_slug='$slug' ";
$run_slug = mysqli_query($con,$get_slug );
$check = mysqli_num_rows($run_slug );
if($check == 1)
{
$slug_new = $slug.'ab'; // Add 2 characters at the end
$update ="UPDATE slug SET post_slug = '$slug_new' WHERE post_slug = '$slug'";
$run = mysqli_query($con,$update);
}
else
{
$insert ="insert into slug (post_slug) values ('$slug') ";
$run = mysqli_query($con,$insert);
if($run)
{
echo "<p style='float:right;'> Posted successfully </p>";
}
}
}
?>
I'm woking in a mobile friendly monitoring tool based on rrdtool.
All working fine but when I have the webpage opened for more that 1 hour freezes my computer.
I use this snippet for refresh the images.
<?php
if(!isset($includeCheck))
{
exit ("No tienes permisos para ver directamente el archivo");
}
//echo "
//<script language='JavaScript'>
// function refreshIt(element) {
// setTimeout(function() {
// element.src = element.src.split('?')[0] + '?' + new Date().getTime();
// refreshIt(element);
// }, 50000);
// }
//</script>";
echo "
<script language='JavaScript'>
function refreshIt(element){
setInterval(function() {
element.src = element.src.split('?')[0] + '?' + new Date().getTime();
}, 50000);
}
</script>";
?>
I include this php file in the pages what I have images and I want to refresh.
I tried the 2 options but as I said, freezes my browser.
Anyone have idea what can I do? Because I need refresh the images only, I don't want to refres the whole page.
EDIT
Here is the PHP script for print all images in database.
<?php
if(!isset($includeCheck))
{
exit ("No tienes permisos para ver directamente el archivo");
}
if(isset($_GET['freq'])){
$freq = $_GET['freq'];
$title = 'Gráficas';
} else {
exit ("No has especificado la frecuencia");
}
if($freq == "hourly"){
$title = 'Tráfico última hora';
}
if($freq == "daily"){
$title = 'Tráfico último dia';
}
if($freq == "weekly"){
$title = 'Tráfico última semana';
}
if($freq == "monthly"){
$title = 'Tráfico último mes';
}
if($freq == "yearly"){
$title = 'Tráfico último año';
}
if(isset($_GET['limit'])){
$limit = $_GET['limit'];
} else {
$limit="10";
}
if(isset($_GET['inicio'])){
$inicio = $_GET['inicio'];
}
if(isset($_GET['subpage'])){
$subpage = $_GET['subpage'];
if ($subpage==1) {
$inicio=0;
}
} else {
$subpage = "1";
$inicio="0";
}
$total_records = $conn->query("SELECT graph_id FROM graph_".$freq)->num_rows;
$total_pages = ceil($total_records / $limit);
$sql = "SELECT graph_desc,graph_id,graph_name FROM graph_".$freq." ORDER BY graph_desc ASC LIMIT ".$limit." OFFSET ".$inicio.";";
$result = $conn->query($sql);
include_once 'adm/includes/pagination.php';
echo "<h1 id='titulo' class=\"page-header\">".$title."</h1>";
include_once 'adm/includes/refresh.php';
if ($total_pages != '1') {
echo "<div id='pager1' class='col-md-12'>";
echo $pagLink;
echo "</div>";
}
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<div class='graphSize' style='float:left; position:relative; z-index: 10;'>
<a href='?page=GraphDetail&graph_id=".$row['graph_id']."&graph_desc=".$row['graph_desc']."'>
<img style='width:100%' src='/graficas/".$freq."/".$row['graph_name']."' onload='refreshIt(this)'></img>
</a>
</div>
";
}
}
else {
echo "<p>No hay ninguna gráfica para mostrar</p>"; }
if ($total_pages != '1') {
echo "<div id='pager2' class='col-md-12'>";
echo $pagLink;
echo "</div>";
}
echo "</body>";
if(isset($_GET['fullscreen'])){
echo "<style>";
echo ".graphSize {";
echo "width:500px;";
echo "}";
echo "</style>";
}
$conn->close();
You have this onload handler for your image:
refreshIt(this)
...and that sets an interval (meaning it will repeat) every 50 seconds. The callback for that interval sets the src for the image, which means that onload will once again trigger, and you'll again do a set interval. So each time a new version of the image loads, you add another interval, so eventually you'll have tons of duplicating callbacks occurring.
You probably want to use setTimeout, or call refreshIt(this) once, not on every onload.
in the model alert is not working. if condition is working,the only problem with the alert box, its not showing the dialog box.Please help..
public function setJumlahPenumpang ($idJadwal,$idPemesanan,$jml,$booked,$selected){
$data1 = $this->db->query('select p.jumlah_kursi, j.jumlah_penumpang, p.harga from tb_po p JOIN tb_jadwal j ON j.id_po = p.id_bus WHERE j.id_jadwal ='. $idJadwal);
foreach ($data1->result_array() as $dataa1) {
$tersedia = $dataa1['jumlah_kursi'] - $dataa1['jumlah_penumpang'];
if($tersedia < $jml){
?>
<script type="text/javascript">
document.location = '<?php echo base_url(); ?>proses/cekKode1/<?php echo $idPemesanan ?>';
alert("Tidak ada Bus Beroperasi");
</script>
<?php
}else{
$data3 = $dataa1['harga'] * $jml;
$this->db->query('update tb_pemesanan set harga = '.$data3.' where id_pemesanan = '.$idPemesanan);
$data = $this->db->query('select jumlah_penumpang from tb_jadwal where id_jadwal ='. $idJadwal);
$this->db->query("update tb_jadwal set booked = '".$booked."' where id_jadwal = ".$idJadwal);
$this->db->query("update tb_pemesanan set kursi = '".$selected."' where id_pemesanan = ".$idPemesanan);
foreach ($data->result_array() as $dataa) {
$data2 = $dataa['jumlah_penumpang'] + $jml;
$this->db->query('update tb_jadwal set jumlah_penumpang = '.$data2.' where id_jadwal = '.$idJadwal);
}
}
}
}
Try Echo Before Script Tag I hope So it 'll work....
if($package_id == NULL) {
echo '<script type="text/javascript">
window.location.href = "'.base_url().'"
</script>';
return;
die();
}