简体   繁体   中英

Can't reach PHP variable in if statement

I'm having a problem with the following code. The variable $houseid appears as empty in the second if statement but can be called in the first. I need the $houseid variable to be reached in the second if statement. I have tested it outside the both if's and in the first and it appears to work fine in both.

$landlordid = $_SESSION['landlordid'];

$con = mysqli_connect("mysql1616int.cp.blacknight.com","***","***","***"); // Connect to the MySQL server

$mysql = mysqli_query($con, "SELECT * FROM houseInfo WHERE landlordID = '{$landlordid}'");


    echo '<form method="POST" action=""><select name="pickhouseid">';

    while($row = mysqli_fetch_array($mysql)){
      echo '<option value="' . $row['houseID'] . '">'. $row['housename'] . '</option>';
      }

    echo '</select> 
    <input type="submit" name="houseselect"  value="Select">
    </form>';
    echo "<br>";

    $houseid = $_POST['pickhouseid']; ///where house is set.

    if(isset($_POST['houseselect'])){
    echo "<form method='POST' action=''><select name='services'><option value='gas1'>Gas Reading</option>
            <option value='water1'>Water Reading</option><option value='elec1'>Electricity Reading</option>
            </select><input type='submit' value='Select'></form>";

    echo "<br>";
    echo $houseid; /// returns correct value
    }
    if (($_POST['services']) == 'gas1') { 
    echo "something";
    echo $houseid; ///doesn't work, appears as empty.
    }

Can anyone help with this problem? thank you.

A form only submits what's in this form, not what's in another form on the page.

$houseid (in your last if) should be now NULL so echoing it should be output nothing.

So add the information about the $houseid into the other form. $_POST data aren't passed through multiple requests.

echo "<form method='POST' action=''><select name='services'><option value='gas1'>Gas Reading</option>
        <option value='water1'>Water Reading</option><option value='elec1'>Electricity Reading</option>
        </select><input type='submit' value='Select' />
        <input type='hidden' value='$houseid' name='houseid' /> <!-- here was added -->
       </form>";

wow, is that possible? I see no reason why $houseid, should return empty, except if the condition returns false, try echoing something else (a string) in the if statement block and see if it appears, I really think the if statement returns false anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM