javascript alert with different value - javascript

i have a link with the value of Active and Offline..
while($s = mysql_fetch_array($strength))
{
?>
<a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset()">
<?php if($s['status'] == 'Active')
{
?>
<span class="badge_style b_online">Active</span>
<?php
}
else
{
?>
<span class="badge_style b_offline">Offline</span>
<?php
}
?>
</a>
<?php } ?>
what i want to do is,, if the value is Active, and the user is click it, the alert with pop out.. and says do you want to deactive, and if the value is offline, when the user click, the alert will says do you want to activate ..
and my javascript is like this..
<script>
function confirmReset() {
var div = document.getElementById("div");
var span = div.getElementsByTagName("span");
if(span.innerHTML == 'Active')
{
var question = confirm("Are you sure want to deactive?");
}
else
{
var question = confirm("Are you sure want to activate?");
}
if(question){
return true;
}else{
return false;
}
}
</script>
the output is always Are you sure want to activate? even though the value is active..
This is my full code..
http://pastebin.com/FteRz6VN
this is the screen..
http://puu.sh/8CyBy.jpg

Please try this
<?php
$i = 0;
while($s = mysql_fetch_array($strength))
{
?>
<a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset('<?php echo 'status'.$i ?>')">
<?php if($s['status'] == 'Active')
{
?>
<span class="badge_style b_online" id="status<?php echo $i ?>">Active</span>
<?php
}
else
{
?>
<span class="badge_style b_offline" id="status<?php echo $i ?>">Offline</span>
}
?>
</a>
<?php $i++; } ?>
<script>
function confirmReset(id) {
var statusDiv = document.getElementById(id).innerHTML;
if(statusDiv == "Active"){
var question = confirm("Are you sure want to deactive?");
} else {
var question = confirm("Are you sure want to activate?");
}
if(question){
return true;
} else {
return false;
}
}
</script>

Change the span variable:
var span = div.getElementsByTagName("span")[0];

You can use jquery to solve this issue very easily following is the code:
$(document).ready(function() {
$(".badge_style").click(function(){
var isActive = $(this).html();
if(isActive == "Active"){
...Do what you want to do if active
alert("do you want to activate!");
}else{
... else part
}
});
});
no need to call confirmReset function

Related

how to select id which is dynamically change in jquery?

here is my html:
<?php echo ($row['status'] == 1)? ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-danger'>Inactive</span>
<?php : ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-success'>Active</span>
<?php ; ?>
here is my jquery code:
$(document).ready(function()
{
$("span").click(function()
{
var id = $(":input[name=slide_id]").val();
alert(id);
});
});
i didn't get anything , i want to get the
ID & its dynamic generated value
of the span element and then perform some action like on click i want to change the status from active to inactive or vise versa.
thanks in advance.
var id = $(":input[name=slide_id]").attr('id'); should do the job. .val() only returns the current value of input elements.
Your HTML is missing from the question, but eliminating the colon in your jQuery selector might do the trick in selecting an input field - i.e. $("input[name=slide_id]").val().
If you want to get the ID attribute of a span element, this would work:
$('span_selector').attr('id');
okay finally i got the way to solve this things. m just sharing this if anyone have same issue one day then this might helpful.
html codes:
<tr class="tr">
<td class="td">
<div id="status">
<?php
echo ($row['status'] == 1)?
"<span id='".$row['id']."' class='label label-danger'>Inactive</span>":
"<span id='".$row['id']."' class='label label-success'>Active</span>";
?>
</div>
</td>
</tr>
my jquery code:
$(document).ready(function()
{
$("#status > span") .click(function()
{
var id = $(this).attr('id');
var tempelement = $(this);
var texts = $(this).closest(".tr").find("#texts").val();
var author = $(this).closest(".tr").find("#author").val();
var status = $(this).text();
$.ajax({
type: 'POST',
url: 'manage_feedback.php',
data: {id:id, texts:texts, author:author, status:status},
success: function(data)
{
if (data == "Active")
{
$(tempelement).removeClass("label label-danger");
$(tempelement).addClass("label label-success");
$(tempelement).html(data);
alert('status changed');
}
else if (data == "Inactive")
{
$(tempelement).removeClass("label label-success");
$(tempelement).addClass("label label-danger");
$(tempelement).html(data);
alert('status changed');
}
else
{
alert(data);
}
}
});
});
php script
//ajax status changer code
if (isset($_POST['id']) && isset($_POST['texts']) && isset($_POST['status']) && isset($_POST['author']))
{
$id = $_POST['id'];
$texts = trim($_POST['texts']);
$author = trim($_POST['author']);
$status = $_POST['status'];
$qry = "SELECT count(id) as count FROM tbl_testimonials WHERE texts = '".$texts."'
AND author = '".$author."' AND id != ".$id." ";
$sql = mysql_query($qry);
$data = mysql_fetch_assoc($sql);
if ($data['count'] == 0)
{
if($status == 'Inactive')
{
$qry = "UPDATE tbl_testimonials SET status = 0 WHERE id = ".$id." " ;
$sql = mysql_query($qry);
if($sql == 1)
{
echo 'Active';
exit;
}
}
elseif ($status == 'Active')
{
$qry = "UPDATE tbl_testimonials SET status = 1 WHERE id = ".$id." " ;
$sql = mysql_query($qry);
if($sql == 1)
{
echo 'Inactive';
exit;
}
}
}
else
{
echo "name already taken";
exit;
}
}
hope it will help someone.

PHP: Detect url in text, check if url is image or website and then echo image

So I'm working on a project, and what I need to do is I have some text, and in that text it has lots of words and then a url which is an image. What I need to do it first, detect if that url is a website or an image, and then if it is an image I need to display the image with the <img> tags, and if it's a website echo the url with the <a href> tags. So far I have a script to detect if it's a url or image, but I still need to echo the image or url in the text. Here is the script:
<?php
function detectImage($url) {
$url_headers=get_headers($url, 1);
if(isset($url_headers['Content-Type'])){
$type=strtolower($url_headers['Content-Type']);
$valid_image_type=array();
$valid_image_type['image/png']='';
$valid_image_type['image/jpg']='';
$valid_image_type['image/jpeg']='';
$valid_image_type['image/jpe']='';
$valid_image_type['image/gif']='';
$valid_image_type['image/tif']='';
$valid_image_type['image/tiff']='';
$valid_image_type['image/svg']='';
$valid_image_type['image/ico']='';
$valid_image_type['image/icon']='';
$valid_image_type['image/x-icon']='';
if(isset($valid_image_type[$type])){
echo "url is image";
} else {
echo "url is website";
}
}
}
?>
A function is a routine that returns a value that can be used in your program.
Don't use functions to output stuf. Rewrite your function to :
<?php
function isValidImage($url) {
$url_headers=get_headers($url, 1);
if(isset($url_headers['Content-Type'])){
$type=strtolower($url_headers['Content-Type']);
$valid_image_type=array();
$valid_image_type['image/png']='';
$valid_image_type['image/jpg']='';
$valid_image_type['image/jpeg']='';
$valid_image_type['image/jpe']='';
$valid_image_type['image/gif']='';
$valid_image_type['image/tif']='';
$valid_image_type['image/tiff']='';
$valid_image_type['image/svg']='';
$valid_image_type['image/ico']='';
$valid_image_type['image/icon']='';
$valid_image_type['image/x-icon']='';
if(isset($valid_image_type[$type])){
return true; // Its an image
}
return false;// Its an URL
}
}
Then use the function in your logic :
<?php
$urls = [
'http://www.google.be',
'http://hearstcommerce.ca/customcontent/members/premium/sample.jpg',
];
foreach($urls as $url) {
if (isValidImage($url) {
echo '<img src="'.$url.'" />';
}else{
echo ''.$url.'';
}
}
Easy as pie
if(isset($valid_image_type[$type])){
$ech = '<img src="'.$url.'"/>';
} else {
$ech = '<a href=".'$url'.">".'$url'."<a>';
}
echo $ech;
Ok so I managed to solve it, my solution was
?>
function detectImage($url) {
$url_headers=get_headers($url, 1);
if(isset($url_headers['Content-Type'])){
$type=strtolower($url_headers['Content-Type']);
$valid_image_type=array();
$valid_image_type['image/png']='';
$valid_image_type['image/jpg']='';
$valid_image_type['image/jpeg']='';
$valid_image_type['image/jpe']='';
$valid_image_type['image/gif']='';
$valid_image_type['image/tif']='';
$valid_image_type['image/tiff']='';
$valid_image_type['image/svg']='';
$valid_image_type['image/ico']='';
$valid_image_type['image/icon']='';
$valid_image_type['image/x-icon']='';
if(isset($valid_image_type[$type])){
return true;
} else {
return false;
}
}
}
function detectLink($string) {
$content_array = explode(" ", $string);
$output = '';
foreach($content_array as $content) {
if(substr($content, 0, 7) == "http://" || substr($content, 0, 4) == "www.") {
if (detectImage($content)===true) {
$content = '<img src="'.$content.'">';
} else {
$content = ''.$content.'';
}
}
$output .= " " . $content;
}
$output = trim($output);
return $output;
}
?>
Feel free to use this anyone!

Checkbox Input doesn't work at all in chrome. Doesn't function in other browsers

Basically I want a checkbox to onClick change whether or not to show explicit content. I'll get right to the code HTML (and PHP):
</form>
<?php if (login_check($mysqli) == true) {
$username = $_SESSION['username'];
echo '
<form name="explicit">
<input type="checkbox" onClick="changeexplicit();" value="Filter Explicit Content" id="explicitcheckbox" ' ;
if (checkadult($mysqli,$username)!=1){echo "checked";}
echo ' /> <label for="explicitcheckbox" class="explicit">Filter Explicit Content</label>
</form>';}?>
Jquery: (i left the error handling blank because I didn't realize JQuery doesn't have a built in popup alert)
<script>
changeexplicit()
{
!(function($){
$(function() {
$.post('includes/changeadult.php', $('form[name="explicit"]').serialize(), function(data){
data = $.parseJSON(data);
if(data.success){
window.location.reload(true);
}
else if(data.error){
}
else{
}
});
});
})(window.jQuery);
}
</script>
PHP:
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start(); // Our custom secure way of starting a PHP session.
$response = array();
if (login_check($mysqli) == true) {
$username=$_SESSION['username'];
$result=mysqli_query($mysqli,'SELECT isAdult FROM members WHERE username = "'.$username.'"');
$userinfo = mysqli_fetch_array($result);
$isAdult=$userinfo['isAdult'];
if ($isAdult==1)
{mysqli_query($mysqli,'UPDATE members SET isAdult=0 WHERE username="'.$username.'"');}
else{
{mysqli_query($mysqli,'UPDATE members SET isAdult=1 WHERE username="'.$username.'"');}}
$response['success'] = true;
}
else if(login_check($mysqli) == false) {
$response['error'] = true;
}
echo json_encode($response);
<script>
function changeexplicit(){
$.post('includes/changeadult.php', $('form[name="explicit"]').serialize(), function(data){
data = $.parseJSON(data);
if(data.success){
window.location.reload(true);
}else if(data.error){
}else{}
});
}
</script>
Try change onClick toonchange?

Call Jquery Function in Codeigniter

I have one if-else and I want to call function which contain jquery script. but its not working
Here is my codeignitor view code
Code
if($osoption == "windows")
{
?>
<script>
windows();
</script>
<?
$mysqldatabasehidden="N/A";
if ($_POST['mssqldatabasehidden']=="")
{
$mssqldatabasehidden = "1 No";
}
else
{
$mssqldatabasehidden = $_POST['mssqldatabasehidden']." No";
}
if($_POST['mssqlstoragehidden']=="")
{
$mssqlstoragehidden = "100";
}
else
{
$mssqlstoragehidden = $_POST['mssqlstoragehidden']." MB";
}
if($_POST['mssqltotalcosthidden'] =="")
{
$mssqltotalcosthidden = "3000 INR";
}
else
{
$mssqltotalcosthidden = $_POST['mssqltotalcosthidden'].".00 INR";
}
//echo ("mssql database =".$mssqldatabasehidden." <br>");
//echo ("mssql storage =".$mssqlstoragehidden." <br>");
//echo ("mssqltotalcost =".$mssqltotalcosthidden." <br>");
}
else
{
?>
<script>
linux();
</script>
<?
if($_POST['mysqldatabasehidden'] =="")
{ $mysqldatabasehidden = "0 No.";
$mysqldatabasecost="0.00 INR";
}
else
{
$mysqldatabasehidden = $_POST['mysqldatabasehidden']." No";
$mysqldatabasecost="0.00 INR";
//echo ("mysqldatabse =".$mysqldatabasehidden." <br>");
}
}
If windows selected than its call function name "windows" and same for "linux" when its in Else condition.
Here is my two functions
JQ Functions
<script>
function windows()
{
alert();
$("#divmssqldb").removeClass("divhide").addClass("divshow");
$("#divmssqlstor").removeClass("divhide").addClass("divshow");
$("#divmssqlprice").removeClass("divhide").addClass("divshow");
$("#divmysql").addClass("divhide");
}
function linux()
{
alert();
$("#divmssqldb").removeClass("divshow").addClass("divhide");
$("#divmssqlstor").removeClass("divshow").addClass("divhide");
$("#divmssqlprice").removeClass("divshow").addClass("divhide");
$("#divmysql").removeClass("divhide").addClass("divshow");
}
</script>
Remove the script which is written in your PHP code and call your functions after there definitions and declarations like,
<script>
function windows(){
alert('windows');
....
}
function linux(){
alert('linux');
....
}
$(function(){// add document ready function
<?php if($osoption == "windows") { ?> windows(); <?php } ?>
<?php if($osoption == "linux") { ?> linux(); <?php } ?>
});
</script>

How to PHP_SELF in a UI dialog?

So I've been spending hours on finding something specific to my issue, but I can't seem to find any.
I'm in the middle of creating a small CMS. My problem is that I don't know how to make a submitted form in a UI dialog to do the action of PHP_SELF inside of the UI dialog. I have a list of user which can be selected by a radio button. There is a delete button which has some javascript attached:
$('#delete_user').on('click', function(e) {
if (id != null ) {
var url = "delete_user.php?id=" + id;
$('#dialog-placeholder').dialog().load(url);
$('.ui-dialog :button').blur();
}
e.preventDefault();
return false;
});
Now, my problem is that I have made it to the UI dialog where i get the ID sent with the url, but I have no idea how to send a form and still keep it in the dialog with the underneath PHP:
<?php
if ((isset($_GET['id'])) && is_numeric($_GET['id'])) {
$id = $_GET['id'];
} elseif ((isset($_POST['id'])) && is_numeric($_POST['id'])) {
$id = $_POST['id'];
} else {
echo "<p>An error has occurred. Please try again.</p>";
echo "Close";
$jQuery_close = <<<msg
<script>
$('.close_dialog').on('click', function(){
location.reload();
dialog("close");
});
</script>
msg;
echo $jQuery_close;
exit();
}
require('includes/db_con.php'); //making a connection to the database
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['sure'] == 'Yes') {
$q = "DELETE FROM users WHERE user_id=$id LIMIT 1";
$r = #mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1) {
echo "<p>The user has been deleted</p>";
} else {
echo "<p>The user could not be deleted due to system error. Please try again.</p>";
}
} else {
echo "The user has NOT been deleted!";
}
} else {
$q = "SELECT email, CONCAT(firstname, ' ',lastname) AS name FROM users WHERE user_id='$id'";
$r = #mysqli_query($dbc, $q);
$num = mysqli_num_rows($r);
if ($num = 1) {
while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
echo "<p>Are you sure you want to delete this user?</p>";
echo $row['email'] . '<br />';
echo $row['name'];
}
echo '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">
<input type="HIDDEN" name="id" value="' . $id .'" />
<input type="submit" name="sure" value="Yes" />
<input type="submit" name="sure" value="No" />
</form>';
} else {
echo "This page has been accessed in error";
}
}
mysqli_close($dbc);
?>
When pressing "yes" or "no" in the form, it just directly goes to a new page.
My question is, how do I fire the php and send the form within the dialog?
Place your e.preventDefault() at the start of the delete click handler, not at the end where you currently have it, it should be like this:
$('#delete_user').on('click', function(e) {
e.preventDefault();
if (id != null ) {
var url = "delete_user.php?id=" + id;
$('#dialog-placeholder').dialog().load(url);
$('.ui-dialog :button').blur();
}
return false;
});

Categories