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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Cannot set property 'value' of undefined (Anonymous function)

    Hello, I'm hoping someone can help me with this.

    I have 3 pages that I need to put into my framework, they are products, shopping cart and billing. At the moment they work perfectly fine. Here is a live example -

    http://www.cems.uwe.ac.uk/~r4-george...g/products.php

    Now, I have a framework for a whole website that I need to put these pages into. (http://www.cems.uwe.ac.uk/~r4-george/wp4/index.php) The index uses a switch statement to go between pages.

    Here is the index.php

    PHP Code:
    <?php # index.php

    /* 
     *    This is the main page.
     *    This page includes the configuration file, 
     *    the templates, and any content-specific modules.
     */

    // Require the configuration file before any PHP code:
    require_once ('./modules/config.inc.php');

    // Validate what page to show:
    if (isset($_GET['p'])) {
        
    $p $_GET['p'];
    } elseif (isset(
    $_POST['p'])) { // Forms
        
    $p $_POST['p'];
    } else {
        
    $p NULL;
    }

    // Determine what page to display:
    switch ($p) {

        case 
    'about':
            
    $page 'about.inc.php';
            
    $page_title 'About This Site Again';
            break;
            
        case 
    'products':
            
    $page 'products.inc.php';
            
    $page_title 'Products on this site';
            break;

        case 
    'this':
            
    $page 'this.inc.php';
            
    $page_title 'This is Another Page.';
            break;
        
        case 
    'that':
            
    $page 'that.inc.php';
            
    $page_title 'That is Also a Page.';
            break;
        
        case 
    'contact':
            
    $page 'contact.inc.php';
            
    $page_title 'Contact Us';
            break;
        
        case 
    'search':
            
    $page 'search.inc.php';
            
    $page_title 'Search Results';
            break;
        
        
    // Default is to include the main page.
        
    default:
            
    $page 'main.inc.php';
            
    $page_title 'Site Home Page';
            break;
            
    // End of main switch.

    // Make sure the file exists:
    if (!file_exists('./modules/' $page)) {
        
    $page 'main.inc.php';
        
    $page_title 'Site Home Page';
    }

    // Include the header file:
    include_once ('./includes/header.inc');


    echo 
    "<div id=\"content\">";

    // Include the content-specific module:
    // $page is determined from the above switch.
    include ('./modules/' $page);

    // Include the footer file to complete the template:
    include_once ('./includes/footer.inc');

    ?>
    It uses the .inc.php files located in the modules folder to switch between pages.

    Here is my products.inc.php -

    PHP Code:
    <?
        
    include("includes/db.php");
        include(
    "includes/functions.php");
        
        if(
    $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
            
    $pid=$_REQUEST['productid'];
            
    addtocart($pid,1);
            
    header("location:shoppingcart.php");
            exit();
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Products</title>
    <script language="javascript">
        function addtocart(pid){
            document.form1.productid.value=pid;
            document.form1.command.value='add';
            document.form1.submit();
        }
    </script>
    </head>


    <body>
    <form name="form1">
        <input type="hidden" name="productid" />
        <input type="hidden" name="command" />
    </form>
    <div align="center">
        <h1 align="center">Products</h1>
        <table border="0" cellpadding="2px" width="600px">
            <?
                $result
    =mysql_query("select * from products");
                while(
    $row=mysql_fetch_array($result)){
            
    ?>
            <tr>
                <td><img src="<?=$row['picture']?>" /></td>
                <td>       <b><?=$row['name']?></b><br />
                        <?=$row['description']?><br />
                        Price:<big style="color:green">
                            &pound;<?=$row['price']?></big><br /><br />
                        <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
                </td>
            </tr>
            <tr><td colspan="2"><hr size="1" /></td>
            <? ?>
        </table>
    </div>
    </body>
    </html>
    This is EXACTLY the same code as the working example. The products get listed correctly but the problem I have is the 'Add to Cart' button fails to work.

    Live example - http://www.cems.uwe.ac.uk/~r4-george...php?p=products

    Everything is in the right directory. When I inspect the 'Add to Cart' button in chrome I get the following -

    Uncaught TypeError: Cannot set property 'value' of undefined
    addtocart index.php:51
    (anonymous function)index.php:83
    onclick
    Any help is really appreciated, I'm struggling to see what I have done wrong. I don't know whether it's a Javascript problem.

    If you need any of the code from other pages I can post it too.

    Thanks in advance.
    Last edited by swiltch; 03-07-2012 at 11:45 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Any reason why you are not showing the php page containing the error (addtocartindex.php) ? Please show us its HTML output (as seen in the browser).

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I'm really sorry, I realise now that I haven't posted the correct pages. I will post them in around an hour, please check back.

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Ok, I have changed my first post to now show the correct products.inc.php

    Here is the HTML output for the page included in the framework (http://www.cems.uwe.ac.uk/~r4-george...php?p=products) -


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Products on this site</title>
    <link href="./styles/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <div id="container">
    	<div id="header">
    	  <h1>your wp project framework</h1>
    	  <p>isn't it good to have a byline?</p>
    	  	<form name="form1" id="form1" method="get" action="index.php">
    	  	<input type="text" name="terms" value="Search..." />
    	  	<input type="hidden" name="p" value="search" />
    	  	<input class=" button" type="submit" name="Submit" value="GO" />
    		</form>
    	</div>
    	
    	<!-- Navigation -->
    <div id="navigation"> 
    		<ul id="navlist">
    			<li><a href="index.php">Home</a></li>
    			<li><a href="index.php?p=about">About Us</a></li>
    			<li><a href="index.php?p=this">This</a></li>
    			<li><a href="index.php?p=that">That</a></li>
    			<li><a href="index.php?p=products">Products</a></li>
    			<li><a href="index.php?p=contact">Contact</a></li>
    			<!--use php to make the following dynamic -->
    			<li><p><strong>A tiny little service announcement.</strong><br/>Put all your little tidbits of information or pictures or dynamic widgets in this area </p></li>
    		</ul>
    </div>
    <!-- Navigation -->
    	<!-- Start sidebar -->
    <div id="sidebar">
        <h2>Members Log in</h2>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas venenatis enim ut purus. In hac habitasse platea dictumst. Sed rutrum tempus turpis. Sed rhoncus dui eu ipsum. Pellentesque tincidunt. Quisque pulvinar. Morbi quis leo sit amet neque tempor fringilla. Pellentesque faucibus metus vitae erat. Quisque a urna ut sapien accumsan ornare. Nulla porta pretium eros. Fusce velit erat, accumsan pellentesque, porttitor eu, commodo quis, augue. <a href="#">Fusce convallis ipsum eget felis</a>. </p>
        <p>Aenean eros arcu, condimentum nec, dapibus ut, tincidunt sit amet, urna. Quisque viverra, eros sed imperdiet iaculis, est risus facilisis quam, id malesuada arcu nulla luctus urna. </p></div>
    <!-- End sidebar -->	
    	
    
    
    	<!-- End of header. -->
    <div id="content"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Products</title>
    
    <script language="javascript">
    
    	function addtocart(pid){
    
    		document.form1.productid.value=pid;
    
    		document.form1.command.value='add';
    
    		document.form1.submit();
    
    	}
    
    </script>
    
    </head>
    
    
    
    
    
    <body>
    
    <form name="form1">
    
    	<input type="hidden" name="productid" />
    
        <input type="hidden" name="command" />
    
    </form>
    
    <div align="center">
    
    	<h1 align="center">Products</h1>
    
    	<table border="0" cellpadding="2px" width="600px">
    
    		    	<tr>
    
            	<td><img src="images/tool-6.jpg" /></td>
    
                <td>   	<b>Splatter painting Se</b><br />
    
                		Use the ‘spatter’ tool in conjunction with the brush supplied. By gently flicking the brush hairs approximately 20cm (8") from your material, wood, paper etc.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;3</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(6)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-5.jpg" /></td>
    
                <td>   	<b>Foam Roller Set</b><br />
    
                		A mini foam roller set that contains 1 roller handle 190mm long and 3 foam rollers. The roller lays colour evenly and is suitable for use with all sorts of paints<br />
    
                        Price:<big style="color:green">
    
                        	&pound;4</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(5)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-4.jpg" /></td>
    
                <td>   	<b>Rubber Rolling Stamp</b><br />
    
                		A set of 24 interchangeable rubber rollers all with different designs. Just slide rollers on and off the handle and produce continuous lines of pattern.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;16</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(4)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-3.jpg" /></td>
    
                <td>   	<b>Pattern Rollers Set</b><br />
    
                		This inexpensive pack contains 6 assorted pattern rollers. Each roller has a very different pattern. Pack of 6 assorted.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;3</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(3)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-1.jpg" /></td>
    
                <td>   	<b>Artists Acrylic Brid</b><br />
    
                		An essential platform to protect finished areas of work whilst allowing you to continue on new sections. <br />
    
                        Price:<big style="color:green">
    
                        	&pound;13</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(1)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-2.jpg" /></td>
    
                <td>   	<b>Daler Rowney Tube Sq</b><br />
    
                		A tool that allows you to extract all the paint from your artists tubes in a simple mangle type action.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;6</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(2)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                </table>
    
    </div>
    
    </body>
    
    </html>
    
    
    
    <!-- footer footer.inc -->
      </div>
    	  
    	<div id="footer">
    	<p><a href="http://validator.w3.org/check?uri=referer" target="_blank">Valid 
      XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank">Valid 
      CSS</a> | Copyright &copy; 2012 Richard George </p>
      </div>
    </div>
    </body>
    </html>
    Here is the HTML output for the page NOT in the framework (http://www.cems.uwe.ac.uk/~r4-george...g/products.php) -

    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Products</title>
    
    <script language="javascript">
    
    	function addtocart(pid){
    
    		document.form1.productid.value=pid;
    
    		document.form1.command.value='add';
    
    		document.form1.submit();
    
    	}
    
    </script>
    
    </head>
    
    
    
    
    
    <body>
    
    <form name="form1">
    
    	<input type="hidden" name="productid" />
    
        <input type="hidden" name="command" />
    
    </form>
    
    <div align="center">
    
    	<h1 align="center">Products</h1>
    
    	<table border="0" cellpadding="2px" width="600px">
    
    		    	<tr>
    
            	<td><img src="images/tool-6.jpg" /></td>
    
                <td>   	<b>Splatter painting Se</b><br />
    
                		Use the �spatter� tool in conjunction with the brush supplied. By gently flicking the brush hairs approximately 20cm (8") from your material, wood, paper etc.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;3</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(6)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-5.jpg" /></td>
    
                <td>   	<b>Foam Roller Set</b><br />
    
                		A mini foam roller set that contains 1 roller handle 190mm long and 3 foam rollers. The roller lays colour evenly and is suitable for use with all sorts of paints<br />
    
                        Price:<big style="color:green">
    
                        	&pound;4</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(5)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-4.jpg" /></td>
    
                <td>   	<b>Rubber Rolling Stamp</b><br />
    
                		A set of 24 interchangeable rubber rollers all with different designs. Just slide rollers on and off the handle and produce continuous lines of pattern.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;16</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(4)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-3.jpg" /></td>
    
                <td>   	<b>Pattern Rollers Set</b><br />
    
                		This inexpensive pack contains 6 assorted pattern rollers. Each roller has a very different pattern. Pack of 6 assorted.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;3</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(3)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-1.jpg" /></td>
    
                <td>   	<b>Artists Acrylic Brid</b><br />
    
                		An essential platform to protect finished areas of work whilst allowing you to continue on new sections. <br />
    
                        Price:<big style="color:green">
    
                        	&pound;13</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(1)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                	<tr>
    
            	<td><img src="images/tool-2.jpg" /></td>
    
                <td>   	<b>Daler Rowney Tube Sq</b><br />
    
                		A tool that allows you to extract all the paint from your artists tubes in a simple mangle type action.<br />
    
                        Price:<big style="color:green">
    
                        	&pound;6</big><br /><br />
    
                        <input type="button" value="Add to Cart" onclick="addtocart(2)" />
    
    			</td>
    
    		</tr>
    
            <tr><td colspan="2"><hr size="1" /></td>
    
                </table>
    
    </div>
    
    </body>
    
    </html>
    Is it because the Javascript isn't being found within the framework? (The script isn't being shown in the source, but it is in the page outside the framework)
    Last edited by swiltch; 03-07-2012 at 11:53 AM.


  •  

    Posting Permissions

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