0

I know this has been asked before, but I can't seem to make this work... I have a PHP form with several text boxes and a button.

The button will call different functions, depending on the value of one variable which is declared early on. I realized that the variable was getting lost after the 'Continue' button click, so I had thought that assigning it to a $_SESSION variable might help. NOT!

as requested, here is the full code for this page ... as it currently is. Please note, I have tried EVERYTHING suggested from the responses already, even if it is not included in the version as it is now!!..

<?php 
require_once  ("Includes/connectDB.php");
session_start();
$pn = $_GET['q'];
$hideval = $pn;  //values are as expected here...(the calling form has checks in place to ensure valid number entered

$query = "SELECT * FROM customer WHERE Phone='$pn'";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

$rowinfo = mysql_fetch_array($result);

$cphone = $pn;
if ($rows > 0)
{
    $clast = $rowinfo['LastName'];
    $cfirst = $rowinfo['FirstName'];
    $caddr1 = $rowinfo['Address1'];
    $caddr2 = $rowinfo['Address2'];
    $cpostal = $rowinfo['Postal'];
    $ccity = $rowinfo['City'];
    $cprov = $rowinfo['Province'];
    $cpay = $rowinfo['PayType'];
    $corder = $rowinfo['CustOrder'];
}
else
{
    $clast = '';
    $cfirst = '';
    $caddr1 = '';
    $caddr2 = '';
    $cpostal = '';
    $ccity = '';
    $cprov = 'ON';  
    $cpay = ''; 
    $corder = '';  
}

    function AddRecord()
{
    $clast = $_POST['CustLastName'];
    $cfirst = $_POST['CustFirstName'];
    $cphone = $_POST['CustPhone'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if(! get_magic_quotes_gpc() )
    {
        $cphone = addslashes ($cphone);
        $clast = addslashes ($_POST['CustLastName']);
        $cfirst = addslashes ($_POST['CustFirstName']);
        $caddr1 = addslashes ($_POST['CustAddress1']);
        $caddr2 = addslashes ($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addcslashes($_POST['CustOrder']);
    }
    else
    {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']); 
        $corder = $_POST['CustOrder'];
    }

    $query = "INSERT INTO customer (Phone, LastName, FirstName, Address1, Address2, City, Province, Postal, PayType, CustOrder) VALUES ('$cphone','$clast','$cfirst', '$caddr1', '$caddr2', '$City', '$Province', '$Postal', '$cpay', '$corder')";
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
}

function EditRecord()
{
    echo ('Im going to try an edit'); die;
    $clast = $_POST['CustLastName'];
    $cphone = $_POST['CustPhone'];
    $cfirst = $_POST['CustFirstName'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if(! get_magic_quotes_gpc())
    {
        $clast = addslashes ($_POST['CustLastName']);
        $cfirst = addslashes ($_POST['CustFirstName']);
        $caddr1 = addslashes ($_POST['CustAddress1']);
        $caddr2 = addslashes ($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addslashes($_POST['CustOrder']);
     }
     else
     {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
     }

     $query = "UPDATE customer SET Phone='$cphone', LastName='$clast', FirstName='$cfirst', Address1='$caddr1', Address2='$caddr2', City='$ccity', Province='$cprov', Postal='$cpostal', PayType='$cpay', CustOrder='$corder' WHERE Phone='$cphone'";
     $result = mysql_query($query);

     if (!$result) die ("Database access failed: " . mysql_error());
 }


?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="styles/beer_delivery.css" />

</head>

<body>
<p></p>
<br><br>
<div id="mainPicture">
    <div class="picture">

    </div>
</div>
    <div class="contentBox">
    <div class="innerBox">
        <h1>Customer Information</h1>
        <div class="contentText">
            <p></p>
            <div id="main">
                <form action="customer_info.php" method="post">
                    <fieldset>
                        <legend>Customer</legend>
                            <label for="CustPhone">Phone:</label>

                            <input type="text" name="CustPhone" id="CustPhone" tabindex="2" value="<?php echo $cphone;  ?>">                  
                            <br>
                            <div id="divider"></div>

                          <p>&nbsp;</p>
                           <label for="CustLastName">Last Name:</label>
                           <input type="text" name="CustLastName" id="CustLastName" tabindex="2" value="<?php echo $clast;  ?>">
                           &nbsp;&nbsp;&nbsp;&nbsp;
                           <label for="CustFirstName">First Name:</label>
                           <input type="text" name="CustFirstName" id="CustFirstName" tabindex="3" value="<?php echo $cfirst;  ?>">

                           <p>&nbsp;</p>

                           <label for="CustAddress1">Address 1:</label>
                           <input type="text" name="CustAddress1" id="CustAddress1" tabindex="4" value="<?php echo $caddr1; ?>">

                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <label for="CustAddress2">Address2:</label>
                            <input type="text" name="CustAddress2" id="CustAddress2" tabindex="5" value="<?php echo $caddr2; ?>">

                            <p>&nbsp;</p>

                            <label for="CustCity">City:</label>
                            <input type="text" name="CustCity" id="CustCity" tabindex="6" value="<?php echo $ccity; ?>">

                            <p>&nbsp;</p>

                            <label for="CustPostal">PostalCode</label>
                            <input type="text" name="CustPostal" id="CustPostal" tabindex="7" value="<?php echo $cpostal; ?>">

                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <label for="CustProvince">Province:</label>
                            <input type="text" name="CustProvince" id="CustProvince" tabindex="8" value="<?php echo $cprov; ?>">

                            <p>&nbsp;</p>
                            <label for="CustOrder">Customer Order:</label>
                            <textarea name="CustOrder" rows="4" cols="50" tabindex="9"><?php echo $corder; ?></textarea>
                            <p>&nbsp;</p>

                            <label for="PayType">Method of Payment:</label>
                            <select name="PayType" id="PayType" tabindex="10">
                                <option value="Credit Card" <?php if ($cpay == 'Credit Card') echo ' selected="selected"';?>>Credit Card</option>
                                <option value="Debit" <?php if ($cpay == 'Debit') echo ' selected="selected"';?>>Debit</option>
                                <option value="Cheque" <?php if ($cpay == 'Cheque') echo ' selected="selected"';?>>Cheque</option>
                                <option value="Other"<?php if ($cpay == 'Other') echo ' selected="selected"'; ?>>Other</option>
                            </select> 

                            <p>&nbsp;</p>
                            &nbsp;&nbsp;&nbsp;&nbsp;

                           <form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']."?".http_build_query($_GET); ?>">
                           <input type="submit" name="Continue" id="Continue" value="Continue" tabindex="11">

                            </form> 
                            <?php
                               //have also used session vars here...but same problem
                                echo $_GET['q']; //this shows the phone number
                                  if(isset($_POST['Continue']))
                                  {
                                      //ultimately, the functions AddRecord and EditRecord will be called in here ... once I get the variable problem fixed
                                      echo 'after click: '. $_GET['q'];  //this shows "after click: "
                                  }
                            ?>                    

                            <p>&nbsp;</p>
                       </fieldset>
                  </form>
            </div>   
        </div>
    </div>
</div>

</body>
</html>
12
  • Where are your getting value of $hideval from? Commented May 14, 2013 at 9:20
  • You're sure $_POST['Continue'] is set? Can you post your HTML form code? Commented May 14, 2013 at 9:21
  • Declared $hideval after session_start(); Commented May 14, 2013 at 9:21
  • 1
    I would also suggest calling session_start() at the very top of your file, since it will generate a warning/error if there is any output before it. And will not "store" your sessions as if it would if there aren't any errors Commented May 14, 2013 at 9:21
  • 1
    You are aware that you are overwriting that variable before the if right? Commented May 14, 2013 at 9:22

4 Answers 4

2

And that is what MildlyInteresting is saying. Try to replace

$hideval = $_GET['q']; 

with

if(!empty($_GET['q'])) {
    $hideval = $_GET['q'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

OK...I've changed it (even though I already knew I had the expected value going in)...but it's still the same!
Can you maybe copy/paste whole thing to pastebin.com and share it with us? It is really strange ...
1

The reason is that you declare $_SESSION['hv'] = $hideval; on the second site of your form, too, which propably has a null value because the respective form element does not exist.

Instead, you would propably want to declare it like that:

if(!empty($_GET['q'])) {
    $_SESSION['hv'] = $_GET['q'];
}

On the next site the session variable won't be overwritten with null and you can access it as expected.

1 Comment

no...as I said, I have put echo statements all the way through. $hideval contains the right information right up to the button click!!!!
0

You're saying

$hideval = $_GET['q'];

and

<form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Notice that the HTTP query string will not be sent, so when you click continue, $_GET['q'] will be not set.

Try to write it as follows:

<form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']."?".http_build_query($_GET); ?>">

Also, it is in my opinion highly recommended to set the error reporting level to the most strict mode, to see all errors (assuming that you're not testing your website 'online', enabling the world to see while you're still testing):

error_reporting(-1); // Display *ALL* PHP errors, enabling you to see every error PHP will emit.

EDIT:

For me, this is working:

<?php

//require_once  ("Includes/connectDB.php");
session_start();
$pn = $_GET['q'];
$hideval = $pn;  //values are as expected here...(the calling form has checks in place to ensure valid number entered

$query = "SELECT * FROM customer WHERE Phone='$pn'";
$result = mysql_query($query);

if (!$result)
    echo ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

$rowinfo = mysql_fetch_array($result);

$cphone = $pn;
if ($rows > 0) {
    $clast = $rowinfo['LastName'];
    $cfirst = $rowinfo['FirstName'];
    $caddr1 = $rowinfo['Address1'];
    $caddr2 = $rowinfo['Address2'];
    $cpostal = $rowinfo['Postal'];
    $ccity = $rowinfo['City'];
    $cprov = $rowinfo['Province'];
    $cpay = $rowinfo['PayType'];
    $corder = $rowinfo['CustOrder'];
}
else {
    $clast = '';
    $cfirst = '';
    $caddr1 = '';
    $caddr2 = '';
    $cpostal = '';
    $ccity = '';
    $cprov = 'ON';
    $cpay = '';
    $corder = '';
}

function AddRecord() {
    $clast = $_POST['CustLastName'];
    $cfirst = $_POST['CustFirstName'];
    $cphone = $_POST['CustPhone'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if (!get_magic_quotes_gpc()) {
        $cphone = addslashes($cphone);
        $clast = addslashes($_POST['CustLastName']);
        $cfirst = addslashes($_POST['CustFirstName']);
        $caddr1 = addslashes($_POST['CustAddress1']);
        $caddr2 = addslashes($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addcslashes($_POST['CustOrder']);
    } else {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
    }

    $query = "INSERT INTO customer (Phone, LastName, FirstName, Address1, Address2, City, Province, Postal, PayType, CustOrder) VALUES ('$cphone','$clast','$cfirst', '$caddr1', '$caddr2', '$City', '$Province', '$Postal', '$cpay', '$corder')";
    $result = mysql_query($query);
    if (!$result)
        die("Database access failed: " . mysql_error());
}

function EditRecord() {
    echo ('Im going to try an edit');
    die;
    $clast = $_POST['CustLastName'];
    $cphone = $_POST['CustPhone'];
    $cfirst = $_POST['CustFirstName'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if (!get_magic_quotes_gpc()) {
        $clast = addslashes($_POST['CustLastName']);
        $cfirst = addslashes($_POST['CustFirstName']);
        $caddr1 = addslashes($_POST['CustAddress1']);
        $caddr2 = addslashes($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addslashes($_POST['CustOrder']);
    } else {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
    }

    $query = "UPDATE customer SET Phone='$cphone', LastName='$clast', FirstName='$cfirst', Address1='$caddr1', Address2='$caddr2', City='$ccity', Province='$cprov', Postal='$cpostal', PayType='$cpay', CustOrder='$corder' WHERE Phone='$cphone'";
    $result = mysql_query($query);

    if (!$result)
        echo ("Database access failed: " . mysql_error());
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="styles/beer_delivery.css" />
    </head>
    <body>
        <p></p>
        <br><br>
                <div id="mainPicture">
                    <div class="picture">

                    </div>
                </div>
                <div class="contentBox">
                    <div class="innerBox">
                        <h1>Customer Information</h1>
                        <div class="contentText">
                            <p></p>
                            <div id="main">
                                <form action="<?php echo $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET); ?>" method="post">
                                    <fieldset>
                                        <legend>Customer</legend>
                                        <label for="CustPhone">Phone:</label>

                                        <input type="text" name="CustPhone" id="CustPhone" tabindex="2" value="<?php echo $cphone; ?>">
                                            <br>
                                                <div id="divider"></div>

                                                <p>&nbsp;</p>
                                                <label for="CustLastName">Last Name:</label>
                                                <input type="text" name="CustLastName" id="CustLastName" tabindex="2" value="<?php echo $clast; ?>">
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <label for="CustFirstName">First Name:</label>
                                                    <input type="text" name="CustFirstName" id="CustFirstName" tabindex="3" value="<?php echo $cfirst; ?>">

                                                        <p>&nbsp;</p>

                                                        <label for="CustAddress1">Address 1:</label>
                                                        <input type="text" name="CustAddress1" id="CustAddress1" tabindex="4" value="<?php echo $caddr1; ?>">

                                                            &nbsp;&nbsp;&nbsp;&nbsp;
                                                            <label for="CustAddress2">Address2:</label>
                                                            <input type="text" name="CustAddress2" id="CustAddress2" tabindex="5" value="<?php echo $caddr2; ?>">

                                                                <p>&nbsp;</p>

                                                                <label for="CustCity">City:</label>
                                                                <input type="text" name="CustCity" id="CustCity" tabindex="6" value="<?php echo $ccity; ?>">

                                                                    <p>&nbsp;</p>

                                                                    <label for="CustPostal">PostalCode</label>
                                                                    <input type="text" name="CustPostal" id="CustPostal" tabindex="7" value="<?php echo $cpostal; ?>">

                                                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                                                        <label for="CustProvince">Province:</label>
                                                                        <input type="text" name="CustProvince" id="CustProvince" tabindex="8" value="<?php echo $cprov; ?>">

                                                                            <p>&nbsp;</p>
                                                                            <label for="CustOrder">Customer Order:</label>
                                                                            <textarea name="CustOrder" rows="4" cols="50" tabindex="9"><?php echo $corder; ?></textarea>
                                                                            <p>&nbsp;</p>

                                                                            <label for="PayType">Method of Payment:</label>
                                                                            <select name="PayType" id="PayType" tabindex="10">
                                                                                <option value="Credit Card" <?php if ($cpay == 'Credit Card') echo ' selected="selected"'; ?>>Credit Card</option>
                                                                                <option value="Debit" <?php if ($cpay == 'Debit') echo ' selected="selected"'; ?>>Debit</option>
                                                                                <option value="Cheque" <?php if ($cpay == 'Cheque') echo ' selected="selected"'; ?>>Cheque</option>
                                                                                <option value="Other"<?php if ($cpay == 'Other') echo ' selected="selected"'; ?>>Other</option>
                                                                            </select>

                                                                            <p>&nbsp;</p>
                                                                            &nbsp;&nbsp;&nbsp;&nbsp;

                                                                            <form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET); ?>">
                                                                                <input type="submit" name="Continue" id="Continue" value="Continue" tabindex="11">

                                                                            </form>
<?php

//have also used session vars here...but same problem
echo $_GET['q']; //this shows the phone number
if (isset($_POST['Continue'])) {
    //ultimately, the functions AddRecord and EditRecord will be called in here ... once I get the variable problem fixed
    echo 'after click: ' . $_GET['q'];  //this shows "after click: "
}

?>

                                <p>&nbsp;</p>
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </body>
</html>

When I type http://example.com/customer_info.php?q=4, example.com being the server where you're testing it, often localhost, into the address bar, the website does show right after the Continue button:

4

and when I click the button, it shows:

4after click: 4

For me, it's working. If it's for you still not working, then check the session settings. Maybe there's something (wrong) with it.

2 Comments

I've added the http_build_query as you suggested ... but still when I echo either the $_GET variable, or the $hideval, the values are still null after the button click ??
@user2368555: Can you maybe post all your code? Maybe the error is somewhere else in your code...
0

obviously,the $_POST['Continue'] is the problem,you can use var_dump to print the $_POST varible,to check the data from form.

var_dump($_POST);

if there is no $_POST['Continue'], surely you can not get the value of $hideval. hope useful to you!

1 Comment

the var_dump does show all the values, including the one I am looking for... Now to get that maintained inside the $_POST['Continue']!! <g>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.