passing a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

passing a variable

Post by michaelk46 »

I am writing a script (index.php) that generates a string that is used to search a MySQL database and display relevant information on the screen. At the end of each row that comes up, I have an edit link that passes the page number to another script (edit.php) which pulls up more detailed information for that record and allows me to edit it and, once I get these bugs worked out, save those changes back to the database. Now there are two buttons that are controlled by edit.php, one is there to save any changes (save) and the other is there to send the user back to the search screen (return).

When the return button is hit, it calls the same search page that is called in the index.php. The problem doesn't start until the search button on that page is hit. Instead of passing the info to index.php, it passes it to edit.php. I know it does that because the edit page is where it was called from and that is all well and good, but I need it to pass the post to index.php and not back to edit.php. That way I am not adding the same code to two different pages.

Anyone know how to do that? If anyone knows how to pass the information from the link directly back to the index page, that would be good to know as well.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: passing a variable

Post by AbraCadaver »

I'm sure you'll have to post the code for the pages.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: passing a variable

Post by michaelk46 »

No problem... Here is index.php

Code: Select all

 
  <?php
$link = mysqli_connect('localhost', 'root');
    if (!$link)
    {
        $error = 'Unable to connect to the database server.';
        include 'error.html.php';
        exit();
    }
if (!mysqli_set_charset($link, 'utf8'))
    {
        $output = 'Unable to set database connection encoding.';
        include 'output.html.php';
        exit();
    }
if (!mysqli_select_db($link, 'wcms'))
    {
        $error = 'Unable to locate the content management database.';
        include 'error.html.php';
        exit();
    }
    
if ($_SERVER['REQUEST_METHOD'] != "POST") //looks to see if a button was pressed 
    {                                                                  
        include "search.html.php";
    }
    
else
    {
        $pagetable = NULL; 
        $pageinfo = array();
        if(!empty($_POST['searchid']) || (!empty($_POST['pagename'])))  //looks to see if either search field is populated
            {
                //begins to generate search string 
                $sql = "SELECT pages.id, category.cat, manufacturer.manu, pages.pagename FROM pages 
                        INNER JOIN category ON pages.categoryid = category.id 
                        INNER JOIN manufacturer ON pages.manufactureid = manufacturer.id WHERE ";
                        
                if(!empty($_POST['searchid']))
                    {
                        $searchid = ($_POST['searchid']);
                        $sql .= "pages.id = ".$_POST['searchid']."";
                    }
                    
                if(!empty($_POST['searchid']) && (!empty($_POST['pagename'])))
                    {
                        $sql .= " and ";
                    }
            
                if(!empty($_POST['pagename']))
                    {
                        $sql .= "pages.pagename = ".$_POST['pagename']." ";
                        $pagename = ($_POST['pagename']);
                    }
                    
                                                    
                $result = mysqli_query($link, $sql);
                if (!$result)
                    {
                        include 'searchres.html.php';
                        $error = 'No Pages Found.';
                        include 'error.html.php';
                        exit();
                    }
                                                        
                $pagetable = "<table border='1'><TR><TD WIDTH=150 BGColor='#00FF00'>Page ID</TD><TD WIDTH=150 BGColor='#00FF00'>Pagename</TD><TD WIDTH=150 BGColor='#00FF00'>Category</TD><TD WIDTH=150                         
                                BGColor='#00FF00'>Manufacturer</TD></TR>";
                              
                while ($row = mysqli_fetch_array($result))
                    {
                        $pagetable .= ('<TR>' . '<TD>' . ($row['id']) . '</TD>' . '<TD>' . ($row['pagename']) . '</TD>' . '<TD>' . ($row['cat']) . '</TD>' . '<TD>' . 
                        ($row['manu']) . '</TD>' .  '<TD>' . '<a href="edit.php?id=' . ($row['id']) . '">Edit</a> '.  '</TD>' . '</TR>');
                    }   
                $pagetable .= '</table>';                           
            }
        else $error = "You must enter search terms"; 
        include "searchres.html.php"; 
    }
?>
 
and the edit.php

Code: Select all

 
<?php
$link = mysqli_connect('localhost', 'root');
    if (!$link)
    {
        $error = 'Unable to connect to the database server.';
        include 'error.html.php';
        exit();
    }
if (!mysqli_set_charset($link, 'utf8'))
    {
        $output = 'Unable to set database connection encoding.';
        include 'output.html.php';
        exit();
    }
if (!mysqli_select_db($link, 'wcms'))
    {
        $error = 'Unable to locate the content management database.';
        include 'error.html.php';
        exit();
    }
    
if(isset($_POST['return'])) //checks if return button was hit
    {
        include 'search.html.php';
    }
 
else
    {
        $sql = 'SELECT pages.id, category.cat, manufacturer.manu, pages.pagename, pages.retailprice, 
                pages.salesprice, pages.upc, pages.sku, pages.caption, pages.pagename, image.name FROM pages 
                INNER JOIN category ON pages.categoryid = category.id 
                INNER JOIN image ON pages.imageid = image.id
                INNER JOIN manufacturer ON pages.manufactureid = manufacturer.id WHERE pages.id = "'. $_GET['id'] .'"';
    
        
        $result = mysqli_query($link, $sql);
            if (!$result)
                {
                    $error = 'No Pages Found.';
                    include 'error.html.php';
                    exit();
                }
 
        $row = mysqli_fetch_array($result);
        include 'edit.html.php';
    }
 
 
?>
 
bjazmoore
Forum Newbie
Posts: 11
Joined: Mon Dec 28, 2009 12:12 pm

Re: passing a variable

Post by bjazmoore »

I presume that the 'search.html.php' is your actual search form. Since you are including the 'search.html.php', it will have access to the current variables for the script you are running. Why not simply set a variable that describes the action you expect the search button to take BEFORE including the search code and then code the form action based on the form you want to load (which is based on the variable you set).

Code: Select all

 
if ($action == 'EDIT') {
   echo '<form action="edit.php" method="post">';
} else {
   echo '<form action="search.php" method="post">';
}
 
Probably about as clear as mud...
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: passing a variable

Post by michaelk46 »

yes, search.html.php is the actual search form...

I am still somewhat new to PHP... could you explain how what you posted would accomplish what I am looking for.
Would I put that into the index page or the edit page?

BTW... I updated the code I have on this page, because of a couple of errors I saw...
bjazmoore
Forum Newbie
Posts: 11
Joined: Mon Dec 28, 2009 12:12 pm

Re: passing a variable

Post by bjazmoore »

Post the code to search.html.php and I will see how one would work this into your code.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: passing a variable

Post by michaelk46 »

Thanks all for your help...

I finally found out last night at 2AM what the main issue was....

When the button is generated, I forgot to include the

Code: Select all

<form action="?" method="POST">
and

Code: Select all

</form>
before and after the actual button, so I could never get it to post... which is what I think you were trying to get at.


Thanks again all...
Post Reply