Jump to content

[SOLVED] Passing A SQL Var Into A Funtion


phpretard

Recommended Posts

I am trying to get the page title to read the value of $APCompany

 

I can't get the value from the while loop into the function that displays the form...

 

Please Help.

 

<?
$ID=$_GET['ID'];

include ("connect.php");

$result = mysql_query("SELECT * FROM table WHERE ValKey='$ID'");

while($row = mysql_fetch_array($result))
{

$APCompany=$row['APCompany'];
$APEmail=$row['APEmail'];

} //END WHILE

echo $APCompany;  <<<<<<<<<<<<< IT WILL DISPLAY HERE

function VerifyForm(&$values, &$errors)
{

    // VALLIDATION
    
    if ($_POST['pass']!=$_POST['pass2'])
        $errors['pass'] = "ERROR 1";
        
    if (!$_POST['pass'])
        $errors['pass'] = "ERROR 2";        
      
    return (count($errors) == 0);
}

function DisplayForm($values, $errors)
{


?>
    <html>
    <head>
        <title><? echo $APCompany; ?></title>  <<<<<<<<<<<<<<<<<< HERE IS THE PROBLEM NO DISPLAY
        <style>
            TD.error
            {
                color: red;
                font-weight: bold;    
            }
        </style>
    </head>
    <body>
   
<div align="center">
    
    <table>
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    
        <tr>
            <td colspan=2 height='30' style='padding-left:50px;'><?= $errors['pass'] ?><?= $errors['pass2'] ?></td>
        </tr>    
        <tr>
            <td align=right>New Password:</td>
            <td><input class='input' type="text" size="30" name="pass" value="<?= htmlentities($values['pass']) ?>" autocomplete='off' />
        </tr>
        <tr>
            <td>Confirm:</td>
            <td><input class='input' type="text" size="30" name="pass2" value="<?= htmlentities($values['pass2']) ?>" autocomplete='off'/>
        </tr>
        <tr><td colspan="2" align="center"><input class='select' type="submit" value="Submit"></tr>
    </form>
    </table>

    
    </div>

    
    </body>
    </html>
   
    
    <?php
}

function ProcessForm($values)
{

$TO = "";
$from = "";

$subject = "";

$body= "";


mail($TO, $subject, $body, "From: <$from>");
    
echo "
<html>
<head>

<title>$APCompany</title> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<, AND AGAIN
</head>
<body>

<div align='center'>
<table bgcolor='#FFFFFF'>
<tr>
<td><center>SUCCESS</center>
</td>
</tr>    
</table>
</div>

</body>
</html>
";

}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $formValues = $_POST;
    $formErrors = array();
    
    if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
    else
        ProcessForm($formValues);
}
else
    DisplayForm(null, null);
?>

<? mysql_close($con); ?>

Link to comment
https://forums.phpfreaks.com/topic/125645-solved-passing-a-sql-var-into-a-funtion/
Share on other sites

functions have their own variable scope, meaning variables defined outside of the function can not be used within it (and vice-versa )

 

In order to use a variable from outside of the function, either pass it the function via a parameter, or use the global the keyword (not recommended).

 

examples:

function a($var)
{
   echo $var;
}

$var = 'Hello';
a($var); // 'Hello';

// OR

function b()
{
   global $var;

   echo $var;
}

$var = 'Hello';
b(); // 'Hello';

So this part of the code would be it's own function?

 

$ID=$_GET['ID'];

include ("connect.php");

$result = mysql_query("SELECT * FROM table WHERE ValKey='$ID'");

while($row = mysql_fetch_array($result))
{

$APCompany=$row['APCompany'];
$APEmail=$row['APEmail'];

} //END WHILE

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.