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
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';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.