Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 08:22 PM   PM User | #1
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Lightbulb info requred on Jquery & php

Hi Crew,

As Always i am back with some other requirement understanding.

point 1: I have below php script to delete the user

Code:
<?php
session_start();
include_once "C:/xampp/htdocs/mym/include/database.php";
$myusername = $_SESSION['myusername']; //user who updating
$user= $_REQUEST['ticket'];
echo '$user';
$sql = ("DELETE FROM $tbl_name1 WHERE ticket='$user'")or die(mysql_error());
if(mysql_query($sql))
{
echo 'Successfully deleted user !!!'
header("location: test.php") or die("record not inserted");
} 
mysql_close();
?>
point 2:

I have below jquery script for stylish alert which will invisible after 3 seconds.

Code:
$(document).ready(function() { 
    $('#demo13').click(function() { 
        $.blockUI({ 
            theme:     true, 
            title:    'This is your title', 
            message:  '<p>This is your message.</p>', 
            timeout:   2000 
        }); 
    });    
});
Now I want to use the Jquery script in the PHP exactly in the place of "echo 'Successfully deleted user !!!'" in the above script.

can any one please help me understand if this can be possible or any other way coz...when i see Jquery it has more fancy thing and I want some of them to be included on my site .......but, I am newbie to Jquery and beginner with PHP so,I am finding hard time to work on them, please he me in this regards .


I will be waiting for your answers. Also I will be searching for some solution around in the Google....


Regards,
nani

Last edited by nani_nisha06; 11-09-2012 at 07:49 PM..
nani_nisha06 is offline   Reply With Quote
Old 11-07-2012, 08:26 PM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
Quote:
Originally Posted by nani_nisha06 View Post
Code:
...
$user= $_REQUEST['ticket'];
$sql = ("DELETE FROM $tbl_name1 WHERE ticket='$user'")...
really?
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Old 11-07-2012, 08:38 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,336
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
Quote:
Originally Posted by poyzn View Post
really?
instead of posing just a one word question, perhaps you should point out his mistake or issue? It would probably help him learn...

anyhow.

ok I think I understand.

add this to the javascript on the page either inline or externally
Code:
function UserDeleted(){
$.blockUI({ theme: true, title: 'User Deleted', message: '<p>User was successfully deleted!</p>', timeout: 2000 }); 
}
then in your php code you have to call the javascript function ( I don't use php so guys correct me if this is wrong please)

Code:
<?php
session_start();
include_once "C:/xampp/htdocs/mym/include/database.php";
$myusername = $_SESSION['myusername']; //user who updating
$user= $_REQUEST['ticket'];
echo '$user';
$sql = ("DELETE FROM $tbl_name1 WHERE ticket='$user'")or die(mysql_error());
if(mysql_query($sql))
{
echo '<script>UserDeleted()</script>';
header("location: test.php") or die("record not inserted");
} 
mysql_close();
?>
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 11-07-2012 at 08:40 PM..
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
nani_nisha06 (11-09-2012)
Old 11-07-2012, 08:44 PM   PM User | #4
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
You should filter any input or escape it in the query, do something like this:

PHP Code:
session_start();
include_once 
"C:/xampp/htdocs/mym/include/database.php";
$myusername $_SESSION['myusername']; //user who updating
$userfilter_input(INPUT_POST'ticket'FILTER_VALIDATE_INT);
$result = array('redirect' => false'message' => 'record not inserted');
if(
$user) {
  
$sql "DELETE FROM $tbl_name1 WHERE ticket='$user'";
  if(
mysql_query($sql)) {
    
$result = array('redirect' => true'message' => 'Successfully deleted user !!!');
  }
}
echo 
json_encode($result); 
in html:
Code:
<a href="#" id="demo13" data-ticket="13">Delete user</a>
in js file:

Code:
jQuery(document).ready(function($) {
$('#demo13').click(function() {
  var ticket = $(this).data('ticket'); // get ticket
  $.ajax({
    url: 'path_to_php_file_with_delete_function.php',
    data: { ticket: ticket },
    dataType: 'json',
    type: 'post',
    success: function(data) {
      $.blockUI({ 
         theme: true, 
         title: 'This is your title', 
         message: '<p>' + data.message + '</p>',
         timeout: 2000 
       });
       if(data.redirect) {
         setTimeout( function() { window.location('test.php') }, 2000 );
       }
    });
  return false;
});
});
__________________
Ushousebuilders.com

Last edited by poyzn; 11-07-2012 at 09:35 PM..
poyzn is offline   Reply With Quote
Users who have thanked poyzn for this post:
nani_nisha06 (11-09-2012)
Old 11-07-2012, 08:47 PM   PM User | #5
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
Quote:
Originally Posted by DanInMa View Post
Code:
...if(mysql_query($sql))
{
echo '<script>UserDeleted()</script>';
header("location: test.php") or die("record not inserted");
}...
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
__________________
Ushousebuilders.com

Last edited by poyzn; 11-07-2012 at 09:11 PM..
poyzn is offline   Reply With Quote
Old 11-08-2012, 09:25 AM   PM User | #6
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by poyzn View Post
really?
Poyzn,

I want to understand what was wrong here ?

Regards,
Nani
nani_nisha06 is offline   Reply With Quote
Old 11-08-2012, 09:40 AM   PM User | #7
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
Quote:
Originally Posted by nani_nisha06 View Post
Poyzn,

I want to understand what was wrong here ?

Regards,
Nani
You can get info here: SQL injection

Imagine that $_REQUEST['ticket'] == "' OR '1' = '1"

then you ll get $sql = "DELETE FROM $tbl_name1 WHERE ticket='' OR '1'='1'"
__________________
Ushousebuilders.com

Last edited by poyzn; 11-08-2012 at 10:21 AM..
poyzn is offline   Reply With Quote
Old 11-09-2012, 02:45 AM   PM User | #8
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
A good practise is to use output buffering to prevent any headers problems. It helps as you can modify headers even after sending content.

At the very top of your PHP scripts add:

PHP Code:
<?php

ob_start
();

?>
At the very bottom add:

PHP Code:
<?php

ob_end_flush
();

?>
You can then echo out stuff wherever you want whenever you want as it's all stored in a variable and sent to the browser after the whole script is parsed. In contrast, without them, echoing causes a reply header to be sent which will cause any session_start() or header() to not work at all.

After header(), add exit() or die() to stop execution of the rest of the script.


PHP Code:
if(mysql_query($sql))
{
echo 
'Successfully deleted user !!!'
header("location: test.php") or die("record not inserted");
exit();

__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 11-09-2012 at 02:49 AM..
Redcoder is offline   Reply With Quote
Old 11-09-2012, 05:26 AM   PM User | #9
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
Quote:
Originally Posted by Redcoder View Post
PHP Code:
...
echo 
'Successfully deleted user !!!'
header("location: test.php") or die("record not inserted");
.., 
It won't work
You will not see the message because of the immediate redirect.
If you want to redirect with php, I suggest you to put the message into session and then show it after redirect.
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Old 11-09-2012, 03:37 PM   PM User | #10
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Quote:
Originally Posted by poyzn View Post
It won't work
You will not see the message because of the immediate redirect.
If you want to redirect with php, I suggest you to put the message into session and then show it after redirect.
The ouptut buffering was only a solution to the annoying "HEADERS HAVE ALREADY BEEN SENT" error. And yeah, to see the message you should store it in a session variable. Remove the message to be echoed. Or you can echo the message in the exit() then redirect after some time. Like this:

PHP Code:

<?php
if(mysql_query($sql))
{
header('Refresh: 10; url= test.php');
exit(
'Succesfully deleted user');
}
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Users who have thanked Redcoder for this post:
nani_nisha06 (11-09-2012)
Old 11-09-2012, 07:47 PM   PM User | #11
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Thanks verymuch Redcoder, poyzn & DanInMa, I have sucessfully cleared this issue using Jquery...

I really happy with your support.....
nani_nisha06 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 PM.


Advertisement
Log in to turn off these ads.