Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by nani_nisha06 View Post
    Code:
    ...
    $user= $_REQUEST['ticket'];
    $sql = ("DELETE FROM $tbl_name1 WHERE ticket='$user'")...
    really?

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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();
    ?>
    Last edited by DanInMa; 11-07-2012 at 08:40 PM.

  • Users who have thanked DanInMa for this post:

    nani_nisha06 (11-09-2012)

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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;
    });
    });
    Last edited by poyzn; 11-07-2012 at 09:35 PM.

  • Users who have thanked poyzn for this post:

    nani_nisha06 (11-09-2012)

  • #5
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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.
    Last edited by poyzn; 11-07-2012 at 09:11 PM.

  • #6
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by poyzn View Post
    really?
    Poyzn,

    I want to understand what was wrong here ?

    Regards,
    Nani

  • #7
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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'"
    Last edited by poyzn; 11-08-2012 at 10:21 AM.

  • #8
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    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();

    Last edited by Redcoder; 11-09-2012 at 02:49 AM.

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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.

  • #10
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    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');
    }

  • Users who have thanked Redcoder for this post:

    nani_nisha06 (11-09-2012)

  • #11
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts
    Thanks verymuch Redcoder, poyzn & DanInMa, I have sucessfully cleared this issue using Jquery...

    I really happy with your support.....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •