Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: js function help?

  1. #1
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default js function help? 6 Mar 2009 @ 16.30

    Hi all,

    i need help using php within my js function

    here is my function
    Code:
    function checkimg(){
    if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
            // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
    $sql="UPDATE ratings 
      SET 
        used_ips = NULL,
        total_value = 0,
        total_votes = 0 
      WHERE 
      id IN ('valuetemp', 'easeofusetemp', 'deliverytemp', 'delchargetemp')";
      $query = mysql_query($sql);        
            
            alert('Your vote has been counted! Thank you for voting');
            unset($_SESSION['security_code']);
       } else {
            // Insert your code for showing an error message here
            alert('Sorry, you have provided an invalid security code');
       }
    }
    but it doesnt work, i have included the file on my php page and added the relivant code to a button

    Code:
    <input type="Button" name="Button" value="Button" onclick="checkimg();"/>
    but nothing happens, and ideas
    cheers all
    Luke
      Reply With Quote

  2. #2
    Senior Member CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    824
    Blog Entries
    4

    Default 7 Mar 2009 @ 18.07

    Umm.... checkimg() is a PHP function, but your accessing it as a javascript function. PHP cannot be run from a users machine, only on the server. So you have a few option:

    • Make the checkimg() function javascript
    • Use the form to submit an HTTP request to the server, and the server handles the PHP
    • Use Javascript AJAX to send a request to the server
    I'd use the second one. For example:

    HTML Code:
    <form method="POST" action="checkimg.php">
    <input type="Button" name="Button" value="Button" />
    </form>
      Reply With Quote

  3. #3
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default 9 Mar 2009 @ 15.08

    hi mate, i never knew you could do that lol

    will give it a go see how i get on

    thanks for your help
    Luke
      Reply With Quote

  4. #4
    Trusted Guru WelshStew's Avatar
    Join Date
    Dec 2008
    Posts
    2,555
    Blog Entries
    3

    Default 9 Mar 2009 @ 16.02

    Great help CV, cheers,

    Oh, and Luke; happy birthday for today ;o)
      Reply With Quote

  5. #5
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default 9 Mar 2009 @ 16.17

    @WelshStew,

    cheers mate!!!!!!!
      Reply With Quote

  6. #6
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default 9 Mar 2009 @ 17.20

    @ CV,

    Hi mate,

    have tried what you suggested but i cant get anything to work

    here is my form page
    PHP Code:
    <?php 
    session_start
    ();
    require(
    'functions.php'); 
    require(
    '_drawrating.php');
    include_once(
    'db101.php');
    require(
    'checkimg.php');?>
    <link rel="stylesheet" type="text/css" href="stylesheets/common_stylesheet.css" />
    <link rel="stylesheet" type="text/css" href="stylesheets/stores_stylesheet.css" />
    <link rel="stylesheet" type="text/css" href="css/rating.css" />

    <script type="text/javascript" language="javascript" src="js/behavior.js"></script>
    <script type="text/javascript" language="javascript" src="js/rating.js"></script>

    <style type="text/css">
    <!--
    .apDiv1 {
        width:200px;
        height:115px;
        float:left;
    }
    .style2 {font-size: 12px}
    -->
            </style>

                <div id="subcontent" style="position:absolute; border: 7px solid black; background-color: white; width: 294px; height:361px; padding: 8px;">

    <form method="POST" action="checkimg.php">

    <div id="apDiv1">
        <table width="294" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="167"><span class="ratingscat_head">Value for money</span></td>
            <td width="119"><span class="storeRatings_Individual"><?php print rating_bar('valuetemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">Ease of use</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('easeofusetemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">Delivery time</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('deliverytemp',5)?></span></td>
          </tr>
          <tr>
            <td><span class="ratingscat_head">P&amp;P costs</span></td>
            <td><span class="storeRatings_Individual"><?php print rating_bar('delchargetemp',5)?></span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><label for="label"><span class="style2">Please Enter Security Code: </span></label></td>
          </tr>
          <tr>
            <td colspan="2"><input id="security_code" name="security_code" type="text" /></td>
          </tr>
          <tr>
            <td><?php echo $message101?></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><input type="Button" name="Button" value="Button" />
            </td>
            <td><a href="javascript:dropdowncontent.hidediv('subcontent')">Close</a></td>
          </tr>
          </table>
          </form>
    <P>
                    
        </div>

           <script type="text/javascript">
        //Call dropdowncontent.init("anchorID", "positionString", glideduration, "revealBehavior") at the end of the page:

        dropdowncontent.init("ratelink", "right-bottom", 600, "mouseover")
        dropdowncontent.init("contentlink", "left-top", 300, "click")

        </script>
    and my checkimg.php page code

    PHP Code:
    <?php
    function checkimg (){;
    if( 
    $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
            
    // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
    $sql="UPDATE ratings 
      SET 
        used_ips = NULL,
        total_value = 0,
        total_votes = 0 
      WHERE 
      id IN ('valuetemp', 'easeofusetemp', 'deliverytemp', 'delchargetemp')"
    ;
      
    $query mysql_query($sql);        
            
            
    $message101 'Your vote has been counted! Thank you for voting';
            unset(
    $_SESSION['security_code']);
       } else {
            
    // Insert your code for showing an error message here
            
    $message101 'Sorry, you have provided an invalid security code';
       }
    }
    ?>
    any ideas why this isnt working?

    cheers
    Luke
    Last edited by ljackson; 9 Mar 2009 at @ 17.36.
      Reply With Quote

  7. #7
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default 9 Mar 2009 @ 17.42

    ok i added this to the form page
    PHP Code:
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting (E_ALL); 
    and im getting an error message saying
    Code:
    Notice:  Undefined variable: message101 in /customers/kernow-connect.com/kernow-connect.com/httpd.www/form.php on line 69
    which is strange as you can see above it is clearly defined

    any ideas?
    Luke

    ok i have defined the varible in the form page which gets rid of the error but the page still isnt checking the image?
    Last edited by ljackson; 9 Mar 2009 at @ 18.16.
      Reply With Quote

  8. #8
    Senior Member CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    824
    Blog Entries
    4

    Default 9 Mar 2009 @ 21.09

    The checkimg.php file only contains a function. Functions are only run when called. Since you never call the function, it won't run. Just take out the function and you'll be good.

    Also, as it is now out of the function, it will run when called by form.php with the code require('checkimg.php');

    Also, since checkimg is a standalone file, you'll need to connect to the database in it, since it isn't associated with form.php anymore.
      Reply With Quote

  9. #9
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    573

    Default 9 Mar 2009 @ 21.31

    hi mate, i think i have done what you suggested above and im now getting a message saying "
    Notice: Undefined index: security_code in /customers/kernow-connect.com/kernow-connect.com/httpd.www/checkimg.php on line 17"

    i tried adding require('form.php'); to my checkimg.php file but it gave more errors about session value already being defined or something similar.
    this is what my new checkimg.php file looks like

    PHP Code:
    <?php

        $user_name 
    "";
        
    $password "";
        
    $database "";
        
    $server "";

           
    $db_handle mysql_connect($server$user_name$password);
        
    $db_found mysql_select_db($database$db_handle);

        if (!
    $db_found) {
                   die(
    'Could not connect: ' mysql_error());
            }
                
    mysql_query("SET NAMES 'utf8'");


    if( 
    $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
            
    // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
    $sql="UPDATE ratings 
      SET 
        used_ips = NULL,
        total_value = 0,
        total_votes = 0 
      WHERE 
      id IN ('valuetemp', 'easeofusetemp', 'deliverytemp', 'delchargetemp')"
    ;
      
    $query mysql_query($sql);        
            
            
    $message101="Your vote has been counted! Thank you for voting";
            unset(
    $_SESSION['security_code']);
       } else {
            
    // Insert your code for showing an error message here
            
    $message101="Sorry, you have provided an invalid security code";
       }
    ?>
    any ideas
    Cheers
      Reply With Quote

  10. #10
    Senior Member CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    824
    Blog Entries
    4

    Default 9 Mar 2009 @ 23.00

    Then that means that $_SESSION['security_code'] isn't defined. You can use isset() to check if it is defined without passing an error.
      Reply With Quote

Page 1 of 2 12 LastLast

Similar Threads

  1. ASP Mail function
    By stu2000 in forum PHP, ASP & Java
    Replies: 1
    Last Post: 28 Sep 2011, @ 07.02
  2. help pls... javascript click option in same function
    By oldgoat in forum Javascript Libraries
    Replies: 6
    Last Post: 19 Dec 2010, @ 16.16
  3. redirecting a page with additional function
    By Dre in forum General Design & Development Issues
    Replies: 2
    Last Post: 31 Aug 2010, @ 17.20
  4. I will be so grateful - assigning function to links not working
    By Jack Franklin in forum Javascript Libraries
    Replies: 2
    Last Post: 19 May 2009, @ 16.14
  5. PHP Copy() function
    By stu2000 in forum PHP, ASP & Java
    Replies: 3
    Last Post: 20 Jan 2009, @ 19.52

Tags for this Thread

Posting Permissions

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