0

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;
4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by gunderjj
0

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.

0

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.

0

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>
0

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>
0

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.

0

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 )

0

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.

1

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]);
}
Votes + Comments
Finally he had an apiphany xD
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.