Delete db row according to Javascript Result - php

How can I halt a php code block when JS confirm false result. My php code like this;
<?php
function func($msg){
echo '<script type="text/javascript">
var r = confirm('$msg');
if(r==false){
return false;
}else{
return true;
}
</script>';
}
.
.
case "update":
func("Do you want to delete?");
mysql_query("UPDATE.....");
break;
?>
When func() result is false, dont process mysql_query()

With this approach it can not be done.
Javascript and PhP are different things. Javascript executes on client side end but php is server end. You would have to take into Account the power of Ajax. have a look at something similar
How to Delete MySQL Rows with Ajax & jQuery
SIMPLE JQUERY AJAX DELETE WITH CONFIRMATION

Related

Write to MySQL on mouseover

After months of testing, I'm not succeeding to create a script that writes a logtext to the MySQL when a div gets a mouseover.
I think I have to use $.ajax, the only problem is, is that ajax (still) is the language which I'm not very good at.
One of the 100 things i've tried:
<?
echo "<div id='div0' rel=".$someid.">Some dynamic text</div>";
?>
<script>
$('.div0').mouseover(function() {
$('#result').load('../../../system/molog.php?cid='+$(this).attr('rel');
});
</script>
Who can help?
Ok, there's a much better way to do this, but since I'm on a phone that's dying and you have been waiting a year...
var info = $("#div0").html();
// if Js in a php file you can do var info = <?php echo $logtext ?>; To bring it to JS
$.get("phpfilehere.php", {info:info}, function(data){
alert(data);
});
The mouseover function...
$("#div0").on("mouseover", function(){
// my JS code above goes here
});
PHP file:
if(isset($_GET['info'])){
$log = $_GET['info'];
// Put ur stuff here, make sure u only echo when u want ur php script to stop and be sent back to Ajax function as data var.
// insert $log
echo "test";
} else {
echo "no get info supplied":
}
And here is a tool I made to teach people how to write prepared statements for SQL queries :) if you need it...
http://wbr.bz/QueryPro/index.php?query_type=prepared_insert

refreshing page until new data added to database

I want to refresh a PHP page every few second with SetInterval(); and it stop refreshing the page if a data added to database. so, when it refreshing my PHP page, it always checking is there a new data added to the database or not..
I'm trying to use this kind of logic but its not working...
$query = mysql_num_rows(SELECT id_example FROM table_example);
$a = count($query);
$b = $a+1;
IF($a==$b)
{
Stop_refreshing_the_page;
}
else
{
Refresh_with_setInterval();
}
is there anyone can suggest me better logic/algorithm/code example to do that??
Which part is not working?
Also, setInterval is probably not what you want if you are refreshing the entire page each time -- seems like a simple setTimeout and reload would do the trick, then simply not print that when you have db results.
Edited for OP
I assume this is not valid PHP, but you should get the idea.
$previous_count = $_GET['previous_count'];
$query = mysql_num_rows(SELECT id_example FROM table_example);
$result_count = count($query);
if ($previous_count == $result_count)
{
// this should render some javascript on the page including $result_count
}
The resulting javascript would be something like:
<script type='text/javascript'>
window.setTimeout( function(){
window.location = window.location.pathname + "?previous_count=<?php echo $previous_count; ?>";
}, 2000);
</script>

Getting jQuery $.post to send data to a PHP file

I'm trying to post some data to a PHP file using jQuery. If I send the data via a form everything works just fine, but I want it to send in the background via jQuery. The click function works (except $.post), because I have tested it with an alert() and when I comment out the $.post line everything else works. If I don't comment out the $.post line the last two lines don't work.
Here is my javascript stored in admin.js:
$(document).ready(function(){
$(".admin-refresh").click(function () {
var movieID = $(this).prev().text();
$.post("actions.php", {refreshMovie: yes, movieID: movieID});
$(this).removeClass('btn-warning');
$(this).addClass('btn-success');
});
});
Here is some of the code from actions.php. This file is working if I post the data via form.
//Refresh Movie Details
if ($_POST['refreshMovie']) {
$movieID = $_POST['movieID'];
Here is the code from active-movies.php, which contains the button that activates the javascript.
<button class="btn admin-refresh"><i class="icon-refresh"></i> Refresh</button>
The files are stored as such ROOT/admin/active-movies.php, ROOT/admin/actions.php, and ROOT/includes/js/admin.js.
Thank you for any help you can offer!
At least one of your problems is here.
{refreshMovie: yes, movieID: movieID}
should be
{refreshMovie: "yes", movieID: movieID}
Try adding a third argument to the $.post() (a callback) like:
$.post("actions.php", {refreshMovie: yes, movieID: movieID}, function(response){
// actions.php should return some data to check if the
// action was successful
// that data will be available as a variable ("response")
if ( response == 'success' ) {
// do something
} else {
// do something else
}
});
Just enclose the parameters names into quotes, because the JS will make the mistake to think that movieID:movieID is like Kung Fu Panda:Kung Fu Panda.
{'refreshMovie': 'yes', 'movieID': movieID}

PHP: Delete from a Database with some prompts from javascript

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;
}
}

How To Call Javascript In Ajax Response? IE: Close a form div upon success

I have a form that when you submit it, it sends the data for validation to another php script via ajax. Validation errors are echo'd back in a div in my form. A success message also is returned if validation passes.
The problem is that the form is still displayed after submit and successful validation. I want to hid the div after success.
So, I wrote this simple CSS method which works fine when called from the page the form is displayed on.
The problem is that I cannot seem to call the hide script via returned code. I can return html like
echo "<p>Thanks, your form passed validation and is being sent</p>";
So I assumed I could simply echo another line after that
echo "window.onload=displayDiv()"; inside script tags (which I cannot get to display here)...
and that it would hide the form div.
It does not work. I am assuming that the problem is that the javascript is being returned incorrectly and not being interpreted by the browser...
How can I invoke my 'hide' script on the page via returned data from my validation script? I can echo back text but the script call is ineffective.
Thanks!
This is the script on the page with the form...
I can call it to show/hide with something like onclick="displayDiv()" while on the form but I don't want the user to invoke this... it has be called as the result of a successful validation when I write the results back to the div...
<script language="javascript" type="text/javascript">
function displayDiv()
{
var divstyle = new String();
divstyle = document.getElementById("myForm").style.display;
if(divstyle.toLowerCase()=="block" || divstyle == "")
{
document.getElementById("myForm").style.display = "none";
}
else
{
document.getElementById("myForm").style.display = "block";
}
}
</script>
PS: I am using the mootools.js library for the form validation if this matters for the syntax..
The AJAX call is:
window.addEvent('domready', function(){
$('myForm').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
Div ID log is where the ajax call back text (validation errors and success message) and loading graphic appear
This is a duplicate of How to make JS execute in HTML response received using Ajax? where I provided the chosen solution.
var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
var reScript = /\<script.*?>(.*)<\/script>/mg;
response = response.replace(reScript, function(m,m1) {
eval(m1); //will run alert("foo");
return "";
});
alert(response); // will alert "htmlhtml"
Your AJAX call should have a "success" callback. It looks like you can simply call displayDiv() in that callback.
Also note that the var divstyle = new String(); line is unnecessary. Strings are immutable in JavaScript, so you are creating an empty string object, which remains unreferenced in the following line. Simply declare the variable when you assign it from document.getElementById():
var divstyle = document.getElementById("myForm").style.display;
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//Since your making php do the validation, there would be two cases,
//the first case is that the php script is not echoing any thing on success, and
//the other case is that its echoing the error massages which will be assignedxmhttp.responseText
//so we need to check that xmlhttp.resposeText has been asigned a value.
if(xmlhttp.resposeText){
document.getElementById(displayContainers_id).innerHTML=xmlhttp.responseText;
}
}

Categories