PHP: Delete from a Database with some prompts from javascript - php

My code is below, I am trying to delete records from mysql database but before deleting the browser has to prompt the user whether the deletion should continue. My problem is my logic is not working its deleting the record no matter what. Any help will be appreciated.
if (isset($_POST['outofqcellchat'])){
?>
<script type ="text/javascript">
var question = confirm("Are you sure you want to unsubscribe\nThis will delete all your facebook information in QCell Facebook");
if(question){
<?php
$delusr = mysql_query("delete from `chat_config` where `phone` = '$phonenumb'");
$row = mysql_num_rows($delusr);
if($row>=1){
header("Location:http://apps.facebook.com/qcellchat");
}
?>
alert("Unsubscribed, You can register again any time you wish\nThank You");
}else {
alert("Thanks for choosing not to unregister \nQCell Expand your world");
}
</script>
<?php
}
?>
Thats my code. Please help

you want to prompt the user upon click of a anchor tag or button. For eg using anchor tag
<a href="delete.php" onclick="return javascript:confirm("Are you sure you want to delete?");" />Delete</a>
This will prompt user.
Or you might use a javascript function such as
<a href="delete.php" onclick="return check();" />Delete</a>
<script type="text/javascript">
function check(){
var question = confirm("Are you sure?");
if(question){
return true;
}else{
alert("Thanks for not choosing to delete");
return false;
}
}
</script>
Hope this helps.

You have a fundamental misunderstanding between PHP and Javascript here. The PHP code will be executed regardless of any JavaScript conditions (which will be processed long after PHP is done, in the browser).
You will need to change the logic so that confirming the deletion redirects the user to a PHP page that deletes the record, or starts an Ajax request with the same effect.

The PHP runs on the server before the client even sees the JavaScript. Use AJAX or a form submission instead.

Try to separate your PHP from javascript and do not forget to delete using the exact link u are targeting ,if you want to delete one by one record , in href that is where u put that Id first .
Delete
<script type="text/javascript">
function check(){
var question = confirm("Are you sure?");
if(question){
return true;
}else{
alert("Thanks for not choosing to delete");
return false;
}
}

Related

Change php array when user checks a checkbox

I have a column that has a button that when pressed, links to a URL set in PHP. I want to add a checkbox next to that button so that if it's checked when a user presses the button, it will take them to an alternate url. The PHP code setting the url:
<?php
$link = 'http://www.example.com';
?>
I realize that the code needs to be in javascript, which I don't know. I know only a tiny bit of php, so any help would be apprciated.
To clarify: (and of course I know this code will never work)
What I want to do is this:
<?php
If (checkbox is checked) {
$link = 'http://www.google.com';
} else {
$link = 'http://www.example.com';
}
?>
There is probably another way to do what you want to achieve. The value of the checkbox should be sent to a single php script on the server with the rest of the form's fields' values. Then you can use the checkbox's value (boolean) in php and do what you need to do accordingly, possibly requiring external scripts.
Checkbox value is not sent to server with form submit if it is not checked.
So, you can use something like this:
<?php
if (isset($_POST['checkbox_name'])) {
$link = 'http://www.google.com';
}else{
$link = 'http://www.example.com';
}
?>
Include the jQuery from Google:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
Then write the redirect function which you call after clicking the button;
<script>
function foo() {
if ($('#checkbox').is(':checked')) {
//redirect to google.com
window.location = "http://www.google.com/";
} else {
window.location = "http://www.example.com/"
}
}
</script>
And finaly your button should look like this:
<button onclick="foo();" >Your button</button>
This code assumes your checkbox has an id "checkbox".
Also, I don't think that what you're trying to do should be done with PHP - so you should learn Javascript/jQuery straight away instead of writing code the way it shouldn't be written.
Example: http://jsfiddle.net/5bdae/
Using jQuery this is fairly simple. You bind a function to the link, this function works out whether the checkbox is checked, if it is it links to one place, otherwise it links to another.
For an HTML structure like this:
<input id='myCheckbox' type="checkbox" name="box" value="box" />
<a href='#' id='myLink'>My Link</a>
The jQuery would be:
$('#myLink').click(function(event){
event.preventDefault();
if ($('#myCheckbox').is(':checked')){
window.location.href='http://www.example.com';
} else {
window.location.href='http://www.ask.com';
}
});
This could would go outside of the PHP tags, and you would need to include jQuery in your code.

Validation for joomla button

I need help on Joomla admin panel validations.
I am using Joomla 2.5. I need validation on some Joomla buttons in the admin backend.
For example, when the "Publish", "Unpublish" or "Delete" buttons are clicked, I would like a popup confirmation message to appear saying "Are you sure you want to unpublish?"
Any help?
Trying to insert it will take a little while so to get you started off, you could use this:
<script type="text/javascript">
function confirm_delete() {
var answer = confirm("Are you sure you want to delete?")
if (answer){
//run delete script
}
else{
//go back to previous page
}
}
</script>
The use pull the function when the button is clicked on.
Here is a basic html version using the script >> http://jsfiddle.net/n56vt/
you can try this
<script type="text/javascript">
Joomla.submitbutton = function(task){
if(task == 'Unpublish'){
var answer = confirm("Are you sure you want to delete?")
if(answer){
submitbutton(task); // run the unpublish script on controller
}
else{
return false;
}
}
submitbutton(task);
}
</script>

ajax and php updating mysql

i am having problem updating the database.
Basically what i am trying to do is everytime a download link is clicked the download count in the database goes up.
Can someone point me in the right direction as i have been trying to get this right for hours :(
Here is the html.
<div class="button_border_dark"> <a id="linkcounter" href="http://www.derby-web-design-agency.co.uk/freeby-download/<?php echo $freebies_download ; ?>" target="_blank" title="Download">Click To Download File</a></div>
Here is the jquery
<script>
$('#linkcounter').bind('click',function(){
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
});
</script>
Here is the downloadcount.php which i am trying to post data too, so it updates the content.
<?php
require_once("applications/constants/connection.php");
require_once("applications/controllers/basic.php");
if(isset($_REQUEST["linkid"])){
$linkid = sanitise($_POST["linkid"]);
$updatedownload = mysql_query("UPDATE freebies SET download_count=`download_count` +1 WHERE id ='".$linkid."'") OR die(mysql_error());
}
You don't say what is the problem!
Is not incrementing? is incrementing too much? is one process blocking the other? the problem is that people can cheat and make so a file has ben downloaded a million times?
Anyway, I think you code can be simpler.
$('#linkcounter').click(function(){
$("#invisibleiframe").attr("src",$(this).attr("src");
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
return false;
});
this need to create a invisible iframe, that will be the one downloaded the file. after starting this download, the ajax request is made. a single event do the two things. made this way the stuff still works if js is disabled.
I think should be
$updatedownload = mysql_query("UPDATE freebies SET download_count= download_count +1 WHERE id ='".$linkid."'") OR die(mysql_error());
Note the single quote:
SET download_count = download_count +1
Well first off I would not mix the jQuery and PHP personally, this may be the source of your problem, I would try something like this
<div class="button_border_dark" theLinkId="<?php echo $id ; ?>">
<a id="linkcounter" href="http://yourURL/" target="_blank" title="Download">
Click To Download File
</a>
</div>
With the jQuery like this
<script>
$('#linkcounter').bind('click',function(){
var theLinkId = $(this).parent().attr('theLinkId');
$.post("downloadcount.php",{ linkid : theLinkId});
});
</script>
It is possible that your event is not actually being bound to the anchor, because you are running .bind() before the anchor exists. Try this:
<script>
$(document).ready(function(){
$('#linkcounter').bind('click',function(){
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
});
})
</script>
how many anchors you have with id = 'linkcounter' ?
If you have more than one, i recommend you to change 'id' for 'class'
Greatings.
EDIT:
Try something like this:
Link
<script type="text/javascript">
function goToLink(o, id) {
$.post("downloadcounter.php",
{linkid : id},
function () {
window.open($(this).attr("href"));
}
);
return false;
}
</script>

Can the PHP $_GET be used to get a variable in the URL using Hashchange?

Click Me
<script src="https://raw.github.com/cowboy/jquery-hashchange/v1.3/jquery.ba- hashchange.js"></script>
<script type="text/javascript">
$(window).hashchange(function () {
//
});
</script>
When Click Me is clicked the URL looks like this "www.mydomain.com/#create=1".
What I am trying to do is us the $_GET in PHP to bring down the parameter. Ex:
<?php echo $_GET['create'];?>
Using the
Click Me
works, but it reloads the page and that is what I am trying to avoid. Any help would be greatly appreciated.
PHP runs on the server. A request has to be sent to the server for PHP to know what is in the query string. You don't need to reload the whole page but you will need to send something to the server, e.g. in an AJAX request and do something with the result.
//java script code
$("#clickme").click(function(event) {
var arr = $(this).attr('href').split('#');
var arr=(arr[1]);
event.preventDefault();
$("#content").load("data.php?"+ arr);
});
//html code
<a id="clickme" href="#create=1">Click Me</a>
<div id="content"></div>
// php code
data.php
There is a new future for that:
window.history.pushState("Remember me!", "Changing the get Parameter...", "?create=1");
You can apply this to all links by using this:
$("a").click(function() {
if ($(this).attr("href").substr(0, 1) != "#") {
$("body").load($(this).attr("href"));
window.history.pushState("Remember me!", "Nothing special", $(this).attr("href"));
return false;
}
});

Sending PHP Variable within a JavaScript Alert / Confirm Box

I've been making steady progress up until the point I wanted the user to have a 'confirm' alert before deleting a row via PHP. Essentially, I need to pass the variable of the row's id number to the 'delete.php' page once the user has pressed 'ok'.
So the PHP prints out a button:
$clickid = $db_field['id'];
print "<form><input type='button' onclick='confirmation()' value='Delete'></form>";
And my Javascript:
function confirmation() {
var answer = confirm("You wish to delete this event?")
if (answer){
alert("Deleted")
window.location = "http://www.xxxxxxx.co.uk/admin/delete.php<? "?id=$clickid "?>";
}
else{
alert("Your Event is Not Deleted")
}
}
</script>
It obviously runs through to the 'delete.php' page, but doesn't delete anything because $clickid seems to have no value passed to it. If anyone can spot where I am going wrong, or maybe something that would work, that would be great help.
window.location = "http://www.xxxxxxx.co.uk/admin/delete.php?id=<?php echo $clickid; ?>";
window.location = "http://www.xxxxxxx.co.uk/admin/delete.php<? "?id=$clickid "?>";
should this line not read more like this?
window.location = "http://www.xxxxxxx.co.uk/admin/delete.php?id="<?=$clickid ?>"";

Categories