Add confirm Popup - javascript

I have this code in line:
$action = "window.location = 'fruit_list.php.php?do=delete&id={$fruitdata['id']}";
echo '<a style="cursor: pointer" onclick="javascript:' . $action . '">Delete</a>';?>
Is there a way to add below code to make confirm box popup when link clicked?
onclick="return confirm('are you sure?')"

You don't need to add return to the onclick, just
<a href='#' onclick='confirm("Are you sure?")'> Click </a>
http://jsfiddle.net/PFKFq/1/

$action = "if(window.event) event=window.event; event.preventDefault();if (confirm('Are you sure?')){window.location = 'fruit_list.php.php?do=delete&id={$fruitdata['id']} }";
echo '<a style="cursor: pointer" onclick="javascript:' . $action . '">Delete</a>';?>

Why you don't use href ?
<?php
$link = "fruit_list.php?do=delete&id={$fruitdata['id']}";
echo "<a href='{$link}' onclick='return confirm(\"Delete ?\");'>Delete</a>";

Related

Combining PHP into a Javascript?

Basically I'm struggling adding PHP into
<a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/'">LINK</a>
My code right now:
<a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/
<?php $randomid = rand(361, 370);
echo '' . $randomid . '&name=john'; ?>
'">Link</a>
But the above doesn't work. When you click, nothing happens.
I have also tried this way:
<?php $randomid = rand(361, 370);
echo '<a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/' . $randomid . '&name=john'">Link</a>'; ?>
But since the javascript contains '' they confict with PHP's html code ''.
How do I solve this?
So ... :) like this - you either use href or use a function onclick... not supposed to be together but this will work:
$randomid = rand(361, 370);?>
Link
From what I read in the comments you are looking for a js function... not an a href link eh?
So how about this:
<?php
$randomid = rand(361, 370);?>
<a onclick="myFunction('<?php echo $randomid;?>');" style="cursor:pointer;">Link</a>
<script type="text/javascript">
function myFunction(randomid){
window.location.assign("https://www.example.com/go/"+randomid+"&name=john");
}
</script>
Either you can use this one it's also working.
<?php
$randomid = rand(361, 370);
echo '<a style="cursor:pointer;" href="https://www.example.com/go/'.$randomid.'&name=john">Link</a>';
?>

how to prompt a delete confirmation inside the php echo with an image inside an a tag link

I have done this code in viewstudents.php the deletion process worked with the image but I'm not able to do the javascript part inside php inside the echo i mean can someone help me out. here is my code:
<?php
session_start();
include("connection.php");
if(!isset($_SESSION['user']))
header("location:login.php");
$qry="SELECT * FROM studentinformation";
$result= mysqli_query($con,$qry) or die (mysqli_error($con));
echo "Welcome ".$_SESSION['user'];
echo "<table border =1 width=80%><tr><td>FullName</td><td>Email</td><td>Password</td></tr>";
while($i = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$i['FullName']."</td>";
echo "<td>".$i['email']."</td>";
echo "<td>".$i['Password']."</td> ";
echo "<td><a href=DeleteStudentAction.php?Key=".$i['ID']."onclick="return confirm('Are you sure?')" ><img src = delete-1432400-1211078.png style = width:35px;hight:20px;> </img> </a></td>";
echo "</tr>";
}
echo "</table>";
?>
it worked fine with the image but I want to have a confirmation prompt that's not working can someone help me out please. Here is my php action for deletion from the database in DeleteStudentAction.php
I really appreciate if someone would help me out because I'm new to php. Thanks!
You haven't got quotes around your href, try this:
while($i = mysqli_fetch_array($result)) {
echo '<tr>
<td>' . $i['FullName'] . '</td>
<td>' . $i['email'] . '</td>
<td>' . $i['Password'] . '</td>
<td>
<a href="DeleteStudentAction.php?Key=' . $i['ID'] . '" onclick="return confirm(\'Are you sure?\')">
<img src="delete-1432400-1211078.png" style="width:35px; height:20px;" />
</a>
</td>
</tr>';
}

Replacing Class of <a> </a> Base On Status

I am trying to change the class of anchor tag of my html and php code.
This is the screenshot:
And below is php code for that screenshot
Here is my PHP code:
<?php echo "<td>{$id}</td>" ?>
<?php echo "<td>{$ontrack_websiteName}</td>" ?>
<?php echo "<td>{$ontrack_url}</td>" ?>
<?php echo "<td>{$ontrack_geography}</td>" ?>
<?php echo "<td>{$ontrack_Comment}</td>" ?>
<?php echo "<td><a class='btn btn-success' href='status_db.php?id={$id}&status={$ontrack_status}'>
<span>$ontrack_status</span></a>
</td>" ?>
<?php echo "<td width='100px'><a class='btn btn-warning btn-sm' href='archiveProcess.php?id={$id}' role='button'><span class='glyphicon glyphicon-save-file' aria-hidden='true'></span></a>
<a class='btn btn-danger btn-sm' href='delete_websiteOntrack.php?id={$id}' role='button'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></a></td>" ?>
There is two values coming from "Status" column. One is "Enable" and another is "Disable". When user click "Enable" green link. It will change the value in DB as "Disable" and show "Disable". When it show "Disable", i would like to change the background as "btn btn-danger" class so that the background will become red.
But currently. It only show as green background as show below:
Pleae kindly help me. I trying to do different way but always failed :(
You can Do something like below
on success and When it show "Disable" :
$( "#btn-change" ).removeClass( "btn-success" );
$( "#btn-change" ).addClass( "btn-danger" )
Let btn-change is an id of that button.
Okay, so first of all you should not use so many <?php ?> tags.
What you are actually looking for is a simple if-statement:
<?php
//if status is enable, set button class to success
if($ontrack_status == "Enable"){
$btn_class = "success";
}else{
$btn_class = "danger"; //else set it to danger
}
//put btn class into the string:
echo "<td>{$id}</td>
<td>{$ontrack_websiteName}</td>
<td>{$ontrack_url}</td>
<td>{$ontrack_geography}</td>
<td>{$ontrack_Comment}</td>
<td> <!-- set button class here-->
<a class='btn btn-{$btn_class}' href='status_db.php?id={$id}&status={$ontrack_status}'>
<span>$ontrack_status</span>
</a>
</td>
<td width='100px'>
<a class='btn btn-warning btn-sm' href='archiveProcess.php?id={$id}' role='button'>
<span class='glyphicon glyphicon-save-file' aria-hidden='true'></span>
</a>
<a class='btn btn-danger btn-sm' href='delete_websiteOntrack.php?id={$id}' role='button'>
<span class='glyphicon glyphicon-trash' aria-hidden='true'></span>
</a>
</td>";
?>
I don't exactly get why you're using a new echo and opening and closing PHP on each line. You could just do
<?php
echo "<td>{$id}</td>
<td>{$ontrack_websiteName}</td>
<td>{$ontrack_url}</td>...
Apart from that, to answer your question, you'll need to use an if-statement. So for example:
if($ontrack_status == 'Disable') {
echo "<td><a class='btn btn-success' href='status_db.php?id={$id}&status={$ontrack_status}'>
<span>$ontrack_status</span></a>
</td>"
}else{
echo "<td><a class='btn btn-danger' href='status_db.php?id={$id}&status={$ontrack_status}'>
<span>$ontrack_status</span></a>
</td>"
}
A nicer solution would be to use shorthands
echo "<td><a class='btn btn-".($ontrack_status == 'Disabled' ? 'success' : 'danger')."' href='status_db.php?id={$id}&status={$ontrack_status}'>
<span>$ontrack_status</span></a>
</td>"
<script type="text/javascript">
var status = <?= $ontrack_status ?>;
$(document).ready(function() {
if(status == "Enable"){
$("a").addClass("btn-success");
$("a").removeClass("btn-danger");
} else {
$("a.status").addClass("btn-danger");
$("a.status").removeClass("btn-success");
});
<a class="status " href=""></a>

replace onclick action to display inline output

i want to change the behavior of the button in this function
$STRING .= ''.$CORE->_e(array('auction','41')).'';
to when clicked to display the output of this function
$data = get_user_meta( $authorID, 'cellno', true);
if(strlen($data) > 0){
echo "<span><i class='fa fa-phone'></i> <a href='phone:".$data."' rel='nofollow' target='_blank'</a> </span>";
so that when " class="btn btn-info btn-lg" is clicked it will display the phone number inside it, for now it shows contact author, and opens a new page to the author page.
thanks everybody
Try this,
<?php
$STRING .= ''.$CORE->_e(array('auction','41')).'';
$data = get_user_meta( $authorID, 'cellno', true);
if(strlen($data) > 0){
echo "<span id='phoneNumber'><i class='fa fa-phone'></i> <a href='phone:".$data."' rel='nofollow' target='_blank'</a> </span>";
}
?>
<script>
$(document).ready(function(){
$('#phoneNumber').hide();
});
function showPhoneNumber(){
$('#phoneNumber').show();
}
</script>
Also, try learning onclick events.

how to pass the variables through a javascript function when it is written inside the php echo statement

how to pass the variables through a javascript function when it is written inside the php echo statement .
here my code
problem with quotes
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('$u_code','$u_name')" >'.$u_name.'</a>';
The cleanest option by using \DOMDocument:
<?php
$dom = new DOMDocument;
$e = $dom->createElement('a', $u_name);
$a = $dom->appendChild($e);
$a->setAttribute('style', 'color: green;');
$a->setAttribute('href', 'javascript:void(0);');
$a->setAttribute('onclick', 'chatWith("' . $u_code . '","' . $u_name . '");');
echo $dom->saveHTML();
You need to escape those quotes:
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
You need to append your php variables with "." and escape quotes
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('.$u_code.','.$u_name.')" >'.$u_name.'</a>';
Fix the quotes
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
echo "<a style=\"color:green\"
href=\"javascript:void(0)\"
onclick=\"javascript:chatWith('".$u_code."','".$u_name."')\"
>'.$u_name.'</a>";
Escape the quotes in your statement.
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
Sometimes, we should just close PHP, improves readability AND you don't get stuck in nested quotes.
?>
<a style="color: green"
href="javascript: void(0);"
onclick="javascript: chatWith('<?php echo $u_code; ?>','<?php echo $u_name; ?>');">
<?php echo $u_name; ?>
</a>
<?php
You can do this, which is cleaner
$link = '<a onclick="javascript:chatWith({ucode},{uname})" style="color:green" href="javascript:void(0)">{anchor}</a>';
$link = str_replace(
$q = "'";
array('{ucode}', '{uname}', '{anchor}'),
array($q.$u_code.$q, $q.$u_name.$q, $uname),
$link
);
echo $link;
use this
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith('.$u_code.','.$u_name.')" >'.$u_name.'</a>';
Your formatting seems to be off.
echo '<a style="color:green" href="javascript:void(0)" onclick="javascript:chatWith(\''.$u_code.'\',\''.$u_name.'\')" >'.$u_name.'</a>';
the . is used to append variables to a string in PHP, since you are using single quotes you'll have to escape the string every time you insert a variable.
Hope this helped.
&dash; Sid

Categories

Resources