Button connect DB

Discussion in 'JavaScript' started by krdzal, Mar 15, 2009.

  1. #1
    I have a website where there is a "Add to my favorites" button. I want it to make it when user click on the button to check if the user is logged in and if it isto connect to the DB and to add a data into the DB, if he is not logged in to say "You are not logged in". I know how to write the PHP code but i need some help for the button or link it doesent matter

    Any help
     
    krdzal, Mar 15, 2009 IP
  2. CGSS-CyberGeek

    CGSS-CyberGeek Peon

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you wanting an AJAX like feature? Or a basic POST form submit?

    Either or, you will be required to use a server side scripting language such as PHP.

    Pretty simple really, for a HTML <form> to POST data.

    
    <?php
    $error_flag = FALSE;
    $output_message = '';
    if( TRUE == isset($_POST['submit']) ) {
    /* Do check here for user status. */
    if( FALSE == $user->is_logged_in() ) {
    $output_message = 'You are not logged in.';
    $error_flag = TRUE;
    }
    
    /* Do check here for Database connection */
    if( FALSE == $db->is_connected() ) {
    $error_flag = TRUE;
    }
    
    if( FALSE == $error_flag ) {
         /* Do operations needed if the user is logged in and DB connection is made */
    }
    }
    ?>
    <!-- HTML Code -->
    <!-- Echo $output_message -->
    <?= $output_message; ?>
    
    <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" name="submit" value="Button to Do Something" />
    </form>
    
    Code (markup):
    Let me know if this basic example helps you out at all.
     
    CGSS-CyberGeek, Mar 16, 2009 IP
  3. krdzal

    krdzal Peon

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes but how to make it the AJAX or the Javascript coding. That i dont know. And how to connect that code with the PHP script
     
    krdzal, Mar 16, 2009 IP