I am having a problem of getting a variable passed from one page to another. On the first page there is a drop down menu populated by sql.

$result = mysql_query ("select Name from dba.wx_faq_backup where Name like '%dev%' group by Name") or die (mysql_error());

//$result = mysql_query ("select Name from dba.wx_faq_backup group by Name") or die (mysql_error());

echo "<select name='DB_Backup' value=''>DB_Backup</option>";

$i=1;
while($nt=mysql_fetch_assoc($result))
{
    echo "<option selected value='$i' > $nt[Name] </option>"."<BR>";
    $i++;
}
echo "</select>";

This works fine. On the second page I have an include that includes the first page and I have a variable extracting the value, but when I echo this variable it is not correct? I seem to get values like 1 and 5?

<?php include ("restore_table.php");?>
$backup = $_POST["DB_Backup"];
echo $backup;

Recommended Answers

All 9 Replies

You have an error in your select statmend on line 5

echo "DB_Backup <select name='DB_Backup'>";

Even when changing this I still get the echo to be 1? The words DB_Backup show up now near the drop down menu though.

Where is $_POST["DB_Backup"] coming from (and what do you expect it to contain)? Your first module has a select statement but it doesn't have a form definition. To further complicate it, you are including that first module into the second one. If your first module had a form with the action being the second module, then you could reasonably expect that $_POST["DB_Backup"] would contain the value selected from the drop-down list and you could access that in the second module (which would no longer have the include). As it is, you have structured it incorrectly or you have a much better knowledge of shortcuts than I do but now you're caught in your own web.

I tried this and I still get 1?

<html>
<body>
<form action="dbform.php" method="post">

<?php

//Variables for mysql database connection
$user='dba';
$password='mysqlrocks';

// connect to MySQL
$connection = mysql_connect(localhost, $user, $password, TRUE, 131074);
if (!$connection) {
    die('Could not connect: ' . mysql_error());
}

$result = mysql_query ("select Name from dba.wx_faq_backup where Name like '%dev%' group by Name") or die (mysql_error());

//$result = mysql_query ("select Name from dba.wx_faq_backup group by Name") or die (mysql_error());

//echo "<select name='DB_Backup' value=''>DB_Backup</option>";
echo "Database Backups: <select name='DB_Backup'>";

$i=1;
while($nt=mysql_fetch_assoc($result))
{
    echo "<option selected value='$i' > $nt[Name] </option>"."<BR>";
    $i++;
}
echo "</select>";

mysql_free_result($result) ;

?>
<p><input type="submit" value="submit"></p>
</form>

<?php
$backup = $_POST["DB_Backup"];
echo $backup;
?>
</body>
</html>

I switched the script up a little. Still doing something wrong. Any ideas? Sorry I do not work with php much.

<fieldset>
        <legend>Please select the Database Backup</legend>
                <label for="branch">Backup:</label>
                <select name="backup_name" STYLE="width: 500px" size="0">
                <option selected value=""></option>
<?php

//Variables for mysql database connection
$user='dba';
$password='mysqlrocks';

// connect to MySQL
$connection = mysql_connect(localhost, $user, $password, TRUE, 131074);
if (!$connection) {
    die('Could not connect: ' . mysql_error());
}

$result = mysql_query ("select Name from dba.wx_faq_backup where Name like '%dev%' group by Name") or die (mysql_error());

//$result = mysql_query ("select Name from dba.wx_faq_backup group by Name") or die (mysql_error());

//echo "<select name='DB_Backup' value=''>DB_Backup</option>";
//echo "Database Backups: <select name='DB_Backup'>";

$i=1;
while($nt=mysql_fetch_assoc($result))
{
    echo "<option selected value='$i' > $nt[Name] </option>"."<BR>";
    $i++;
}
echo "</select>";

mysql_free_result($result) ;

?>
<br><br><br><br><br><br><br><br>
<input type="submit" value="Restore pre" name="re_pre">
<input type="submit" value="Restore live" name="re_live">
<br><br>
</form>

<?
$backup = $_POST["backup_name"];
echo $backup;
?>
</fieldset>

If I can go back to the version 2 back before you changed it, you seemed to be on the right track. Assuming that dbform.php is the module that you have provided above, then it it going to call itself and redisplay the form so you might want to change that to only display the form the first time (you can check if re_pre or re_live has a value). You also have "selected" on every option statement. Normally, you would only put it on one or not use it all. On your option statement you also have value='$i'. Normally php doesn't substitute variables within single quotes but given that the whole statement is within double quotes, I'm not sure if it will or not. You can also use value=\"$i\".

Not sure if any of this will fix your problem but it will be a cleaner starting point. I would also put:
print_r ($_POST);
right after the <?PHP statement to show you what has been Posted and passed to the module.

You can try that and then see what you get.

Seems like 3 is passed along because it is the third choice, but I want the name not the choice number.

Array ( [Fname] => [Lname] => [backup_name] => 3 [re_pre] => Restore pre )

Another way would be to store all your variables in one file then use PHP_INCLUDE or PHP_REQUIRE. Carefully choose which one you want, PHP_REQUIRE will need it before the page will work, PHP_INCLUDE will show an error as well as the rest of the page.

I use these a lot and are very useful.

I was making this too hard. The number returned was the position of the value returned. I simply created an array and in the while loop added the values to the array.

$i=0;
$backuparray = array();

while($nt=mysql_fetch_assoc($result))
{
    echo "<option selected value='$i' > $nt[Name] </option>"."<BR>";
    $i++;
    array_push($backuparray, $nt[Name]);
}
commented: Finally he had an apiphany xD +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.