SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Owz2004's Avatar
    Join Date
    Mar 2004
    Location
    U.K (Wales)
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing a variable from a function to an input

    Hello,

    I have a Page which uses a function

    PHP Code:
    function getChosenPhoto($the_selected_id) {
        require 
    'database.php';
        
    $id $the_selected_id;

        
    $q "SELECT title, description, src FROM wrappers Where id = $id LIMIT 1";

        
    $result $mysqli->query($q) or die("there was a problem");

        if(
    $result) {
            while (
    $row  $result->fetch_object()) {
                echo 
    '<img src="' $row->src '" alt="title" /><br />';
                echo 
    '<h1 class="smallh1"><b>Title:   </b>' $row->title '</h1>';
                echo 
    '<h1 class="smallh1"><b>Description:   </b><span id="description">' $row->description '</span></h1>';
            }
        } else die(
    "There was some problem");
        

    to call a product along with it's title and description from a database..

    I want to be able to call the title on the orderform page which follows afterwards and put it in an input box called title...

    I have tried to use the following code to set the value of the input box but with no luck <input type="text" name="title" value="<?php echo $title; ?>" size="35" />

    Any idea how I transfer the variable across to the orderform page?


    Code for product view page

    Code PHP:
    <!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" xml:lang="no" lang="no">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<meta name="description" content="description" />
    	<meta name="keywords" content="chocolate, wrappers, occasions, birthdays, wedding favours, valentines, personalisation, events, party bags, personalised chocolate, chocolate presents, wchocolate wrappers, chocolate bars, corporate" />
    	<meta name="author" content="author" />
    	<meta name="robots" content="index, follow" />
     
    	<!-- Favico -->
        <link rel="shortcut icon" type="image/ico" href="/favicon.ico" />
     
        <!-- CSS -->
        <link rel="stylesheet" href="css/default.css" type="text/css" media="all" />
        <style type="text/css" media="screen">@import url(css/facebox.css);</style>
        <style type="text/css" media="screen">@import url(css/css_messages.css);</style>
     
    	<script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script>
    	<script type="text/javascript" src="js/jquery.livequery.pack.js"></script>
     
    	<!-- Document title -->
        <title>Chocbars.co.uk - Review your wrapper and purchase</title>
     
    </head>
     
    <body>
     
    <!-- Header with logo and search field -->
    <div id="header">
        <!-- Logo, add your on alternative title to the image (Company name) -->
        <div id="logo"><a href="index.php"><img src="img/logo.png" id="logoimg" alt="Company name" /></a></div>
     
    <!-- Add action-url for the search function -->
         <div id="search"><h1>LOGIN</h1>
          <form action="" method="post">
              <input name="action" type="submit" id="action" value="" />
            <label ="username">
                <input name="username" type="text" id="field" onfocus="if (this.value == 'Username...') {this.value = '';}"  value="Username..." />
            </label>
            <label ="password">
            <input name="pwd" type="password" id="field" />
            </label>  
          </form>
          <?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?>
        </div>
    </div>
     
    <!-- Sets the width and center the page -->
    <div id="wrapper">
     
         <!-- Navigation -->
    	<div id="navigation">
     
    	<div id="left"></div>
    <ul class="nav">
    		<li><a href="index.php" id="left-tab">Home</a></li>
    		<li><a href="#"><b></b><span>Wrappers</span><!--[if IE 7]><!--></a><!--<![endif]-->
    			<!--[if lte IE 6]><table><tr><td><![endif]--><div></div>
    			<ul>
    				 <li><a href="birthdays.php">Birthdays</a></li> 
    				 <li><a href="weddings.php">Weddings</a></li> 
    				 <li><a href="birth.php">Birth Announcements</a></li> 
    				 <li><a href="valentines.php">Valentines</a></li>
    				 <li><a href="easter.php">Easter</a></li> 
    				 <li><a href="christmas.php">Christmas</a></li>
    				 <li><a href="halloween.php">Halloween</a></li> 
    				 <li><a href="prom.php">Prom Nights</a></li>
    				 <li><a href="fund.php">Fund Raising</a></li>
    				 <li><a href="mini.php">- Mini Bar</a></li>
    			</ul>
    			<!--[if lte IE 6]></td></tr></table></a><![endif]-->
    		</li>
     
    		<li><a href="corporate.php">Photos</a></li>
    		<li><a href="FAQ.php">FAQ</a></li>
    		<li><a href="pricing.php">FAQ</a></li>
    		<li><a href="contact.php" id="right-tab">Contact</a></li>
    </ul>
     
    <div id="right"></div>
     
    	   </div>
     
    	<div id="main">
    		<div id="columnleft">
    			<h2><span class="purple">Wrapper</span> view</h2>
    			<h1>Select Quantity and proceed to order form</h1>
    			<br />
    <?php
     
        require 'functions.php';
        require 'database.php';
     
        getChosenPhoto($_GET['id']);
     
        ?>
     
            <div id="column1footer"><p align="left"><br /><br /><a href="admin.php">Back</a></p></div>
     
          </div>
     
            <div id="columnright">
     
     
    							<h1 class="smallh1">Pricing:</h1>
     
    								<ul>
    									<li><span class="purple">1 - 10  items   @ 1.65 - Postage cost = 3.50</span></li>
    									<li><span class="purple">11 - 20  items  @ 1.60 - Postage cost = 4.80</span></li>
    									<li><span class="purple">21 - 50  items  @ 1.50 - Postage cost = 6.40</span></li>
    									<li><span class="purple">51 - 100 items  @ 1.45 - Postage cost = 8.90</span></li>
    									<li><span class="purple">101 - 200 items @ 1.35 - Postage cost = 11.55</span></li>
     
    								</ul>
     
    		 					<div class="line"> </div>
     
    							<form name="addqty" method="post" action="orderform.php">
    <label name="title">Title:</label>
    <input name="title" type="text" size="20" value="<?php echo $title; ?>" />
    <div class="clear"></div>
     
    <label name="quantity">Quantity:</label>
    <input name="qty" type="text" size="5"/>
    <input type="submit" class="submit" value="Proceed" name="submit" />
     
    </form>
     
     
     
     
     
            </div>
     
            <br class="clear" />
    	</div>
     
        <div id="end"> </div>
     
     
         <div id="footer">Copyright  2008 Created by <a href="http:www.icomcreative.co.uk"><b>icom creative.</b></a> All Rights Reserved.</div>
     
    </div>
    </body>
    </html>


    any help would be great thanks

  2. #2
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why dont you return from your function just the data retrieved from db. And when call that function put it in variable. This way you can do whatever you want to do with the retrieved data later in the page. Now you are creating the output inside the function and returning nothing from the function.

    Or if this is working fine otherway for you, just return the title and save it for example into $_SESSION variable that you will use then on the other page to display the recently retrieved title.

  3. #3
    SitePoint Addict Owz2004's Avatar
    Join Date
    Mar 2004
    Location
    U.K (Wales)
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page works fine as it is at the moment so I dont want to tinker with it to much...

    I just need to put the title retrieved from the database into a variable $title and recall it in the orderform page...

    Do you know How I would implement this?

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Owz2004 View Post
    The page works fine as it is at the moment so I dont want to tinker with it to much...

    I just need to put the title retrieved from the database into a variable $title and recall it in the orderform page...

    Do you know How I would implement this?
    For example with using session variable (remember you must have started the session in each page you use them) like changing your function a bit
    PHP Code:
    function getChosenPhoto($the_selected_id) {
        
    $title '';
        require 
    'database.php';
        
    $id $the_selected_id;
        
    $q "SELECT title, description, src FROM wrappers Where id = $id LIMIT 1";
        
    $result $mysqli->query($q) or die("there was a problem");

        if(
    $result) {
            while (
    $row  $result->fetch_object()) {
                echo 
    '<img src="' $row->src '" alt="title" /><br />';
                echo 
    '<h1 class="smallh1"><b>Title:   </b>' $row->title '</h1>';
                echo 
    '<h1 class="smallh1"><b>Description:   </b><span id="description">' $row->description '</span></h1>';
                
    $title $row->title
            
    }
        } else die(
    "There was some problem");
        return 
    $title;

    And when you call your function
    PHP Code:
    $_SESSION['title'] = getChosenPhoto(123); 
    And then use the session variable on the other page.

  5. #5
    SitePoint Addict Owz2004's Avatar
    Join Date
    Mar 2004
    Location
    U.K (Wales)
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what happens if the getchosenphoto is not 123?

    would this mean that the code would not then work? the id 123 is exclusive to that product and so this number would change depending on the product selected..

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Owz2004 View Post
    what happens if the getchosenphoto is not 123?

    would this mean that the code would not then work? the id 123 is exclusive to that product and so this number would change depending on the product selected..
    It was just an example... you give it always the ID what product you want to fetch.

  7. #7
    SitePoint Addict Owz2004's Avatar
    Join Date
    Mar 2004
    Location
    U.K (Wales)
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh O.k

    So i call my function

    Code PHP:
     
    <?php
     
        require 'functions.php';
        require 'database.php';
     
        getChosenPhoto($_GET['id']);
        $_SESSION['title'] = getChosenPhoto(123); 
    ?>

    Do i place the session code in with it? as shown above?

    apologies I new to php so struggling a bit...

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember to start also session on the other page when you use session variables there. You dont need to call the function twice. If you modified the function as pointed above you call it like this. And now when you call it, the function does the same things as it did before and in addition it will place the title to the session variable which is a return value from the function.
    PHP Code:
    <?php
        session_start
    ();

        require 
    'functions.php';
        require 
    'database.php';
       
        
    $_SESSION['title'] = getChosenPhoto(123);
    ?>


Bookmarks

Posting Permissions

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