Can't get php script to show select box from program in firefox - console.log shows code including results I want
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can't get php script to show select box from program in firefox - console.log shows code including results I want
Using Ubuntu 20.04
I can't get the select box in restoreBakup.php to show on the screen so you can make a choice. The screen shows only the header the rest is blank. console.log shows all the code to produce the options as well as the options themselves that I want to pick from. If I choose to run restoreBakup.php by itself it works fine.
Thanks for help in resolving this, R
Code:
//init.php calls restoreBak.php
<script type="text/javascript" >
var passwd="rick";
var database="accounts";
var file="";
// ================== Call to Ajax() ========================
var request = $.ajax({
url: "chkDB.php",
type: "POST",
data: { "pwd": passwd, "db": database},
dataType: "text" ,
success: (function(retval) {
if (retval=='found') {
window.location.href='AccountsLogIn.php';
} else {
alert(database +" Was not found ... Trying 'restoreBakup'.");
}
})
})
request.fail(function( jqXHR, textStatus ) {
alert("go see firebug");
document.write( "Request failed: " + textStatus );
})
var request2 = $.ajax({
url: "../restoreBakup.php",
type: "POST",
data: { "pwd": passwd, "db": database},
dataType: "text",
success: (function(retval){
//window.location.href='AccountsLogIn.php?file=retval';
console.log("return value "+retval);
})
});
request2.fail(function( jqXHR, textStatus ) {
alert("go see firebug");
document.write( "Request failed: " + textStatus );
});
</script>
</body>
</html>
Code:
restoreBakup.php
$directory='./bakups';
$pickFrom=$pickFile=array();
$selected;
//echo getcwd(); exit();
// Open the directory, and read its contents
if (is_dir($directory)){
if ($fh = opendir($directory)){
while (($file = readdir($fh)) !== false){
if($file=="." || $file=="..") {continue;}
//echo $file;
$pickFile[]=$file;
}
closedir($fh);
}
}
?>
<!DOCTYPE html >
<html>
<head>
<title>restoreBakup.php 15Oct2020 </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2>Pick A Database to Restore.</h2>
<form action='' method='post'>
<select name='DBase'>
<option value=''>Select...</option>
<?php
for($i=0; $i<count($pickFile); $i++) {
?>
<option value='<?php echo ($i+1) ?>'> <?php echo $pickFile[$i] ?> </option>;
<?php } ?>
</select>
<input type='submit' name='submit' value='Get DB' >
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['DBase'])) {
$selected = $_POST['DBase'];
//echo 'You have chosen: ' . $pickFile[($selected-1)];
//exit();
//$choice=$pickFile[($selected-1)];
echo $pickFile[($selected-1)];
} else {
echo 'No Database to Restore ... Aborting in 5 seconds.';
sleep(5);
}
} // isset
// none of this below will work
//shell_exec(mysql-u$username -p$passwd < $choice);
//echo "finished";
?>
Will not show select box on screen however, ajax success function return data shows this:
I've made progress but now in a loop that I can't see how to fix.
Here is what I'm trying to do.
I have a MySql database 'accounts' that I am trying to access. The initiating program, 'init.php' first checks to see that 'accounts' exists.
If it Does Exist: Make 1st Call to Ajax, On Success the program continues on to 'AccountsLogIn.php to get a password and continue;
If it Does Not Exist: Make 2nd call to Ajax, which calls the 'restoreBakup.php' This program creates, on-the-fly, a select box offering the available backup files to restore the MySql database. Thanks to your help the select box now pops up and a choice can be made. The problem is I can't figure out how to get the choice. I press on 'Get DB', the submit button, and the program immediately jumps to 'init.php' and starts the process over again. I have revised the code as below.
Code:
// init.php
<h2>Welcome to Accounts Database </h2>
<p>
<h3>Checking for Existing Database 'accounts'</h3>
</p>
<script type="text/javascript" >
$(function() {
var passwd="rick";
var database="accounts";
var file="";
// ================== 1st Call to Ajax() ========================
var request = $.ajax({
url: "chkDB.php",
type: "POST",
data: { "pwd": passwd, "db": database},
dataType: "text" ,
success: (function(retval) {
if (retval=='found') {
window.location.href='AccountsLogIn.php';
} else {
alert(database +" Was not found ... Trying 'restoreBakup'.");
// ========= 2nd Call to Ajax() ===========
var request2 = $.ajax({
url: "../restoreBakup.php",
type: "POST",
data: { "pwd": passwd, "db": database},
dataType: "text",
success: (function(retval){
document.open("text/html");
document.write(retval);
document.innerHTML= retval;
document.close();
//window.location.href='AccountsLogIn.php?file=retval';
//console.log("return value "+retval);
})
}); // end Ajax-2
request2.fail(function(jqXHR, textStatus ) {
alert("go see firebug");
document.write("Request failed: " + textStatus );
});
// =========== End 2nd Call ==============
} // else
}) // success from 1st call
}) // end ajax-1
request.fail(function(jqXHR, textStatus ) {
alert("go see firebug");
document.write("Request failed: " + textStatus );
})
// =========== End 1st Call ============
}); // ready
</script>
</body>
</html>
I'm going nuts!!! This all makes no sense to me. Here is my new scheme that doesn't work. I tried to break it down into smaller steps.
'init.php' checks for MySql to have the database we want. If it does not the program calls, through Ajax, 'restoreBakup.php' which is supposed to present a 'select box' to choose a bakup file for restoring the database to MySql. However, the 'select box' never presents itself to the screen so a choice may be made!!!. Using 'web developer tools' you see the 'select box' complete with the correct options in console.log. So the Present action of this application is that 'init.php' is run, finds the database is not contained in MySql and calls 'restoreBakup.php' which bypasses the 'select.box' on the screen and which then calls 'restoreMySql.php' which skips all the contained code and returns 'nothing' to the original Ajax call in init.php.
Gone nuts, R
Code:
<h3>Checking for Existing Database 'accounts'</h3>
</p>
<script type="text/javascript" >
$(function() {
var passwd="rick";
var database="accounts";
var file="";
// ================== 1st and Only Call to Ajax() Now ========================
var request = $.ajax({
url: "chkDB.php",
type: "POST",
data: { "pwd": passwd, "db": database},
dataType: "text" ,
success: (function(retval) {
if (retval=='found') {
window.location.href='AccountsLogIn.php';
} else {
alert(database +" Was not found ... Trying 'restoreBakup'.");
window.location.href="../restoreBakup.php"
} // else
}) // success from 1st call
}) // end ajax-1
request.fail(function(jqXHR, textStatus ) {
alert("go see firebug");
document.write("Request failed: " + textStatus );
})
// =========== End 1st Call ============
}); // ready
</script>
</body>
</html>
Code:
<?php
// restoreBackup.php pick a database to restore into MySql ...
include './include/myPhpFunctions.inc';
error_reporting(-1);
ini_set("display_errors", true);
$hostname = "localhost";
$database = "accounts";
$username = "rick";
$passwd = "rick";
$row=array();
$directory='./bakups';
$pickFrom=$pickFile=array();
$selected;
//echo getcwd(); exit();
// Open the directory, and read its contents
if (is_dir($directory)){
if ($fh = opendir($directory)){
while (($file = readdir($fh)) !== false){
if($file=="." || $file=="..") {continue;}
//echo $file;
$pickFile[]=$file;
}
closedir($fh);
}
}
// ==================================================================================
// Need function here to abort and go back to index.html if no databases in bakup file
// ===================================================================================
?>
<!DOCTYPE html >
<html>
<head>
<title>restoreBakup.php 15Oct2020 </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="./jquery/jquery-1.9.1.js"> </script>
</head>
<body>
<h2>Pick A Database to Restore.</h2>
<form id="frm1" action="" method='post'>
<select id="mySelect" name='DBase'>
<option value=''>Select...</option>
<?php
for($i=0; $i<count($pickFile); $i++) {
?>
<option value='<?php echo ($i+1) ?>'> <?php echo $pickFile[$i] ?> </option>;
<?php } ?>
</select>
<input type='submit' name='submit' value='Get DB' >
</form>
<script type="text/javascript" >
var choice="";
var database="accounts";
$(function() { // BELOW - on submit do the following
$("#frm1").submit(function(event ) {
alert("Inside submit()"); // NEVER HAPPENS
event.preventDefault();
choice=$("#mySelect option:selected").text();
})
// ================== 1st & Only Call to Ajax() Now ========================
var request = $.ajax({
url: "restoreMySql.php",
type: "GET",
data: { "pwd": "rick", "db": choice},
dataType: "text" ,
success: (function(retval) {
if (retval=='ok') {
window.location.href='AccountsLogIn.php';
} else {
alert(" Restoring MySql "+database+" ... Was not successful.");
window.location.href="../index.html"
} // else
}) // success from 1st call
}) // end ajax-1
request.fail(function(jqXHR, textStatus ) {
alert("go see firebug");
document.write("Request failed: " + textStatus );
})
// =========== End 1st Call ============
})
</script>
</body>
</html>
Code:
<?php
// restoreMySql.php starts mysql and reads database into itself thus re-creating missing database
echo "Inside restoreMySql.php "; // ZIPS by this and returns to the Ajax call in init.php
echo"<pre>";
print_r($_POST);
echo "</pre>";
//shell_exec(mysql-u$username -p$passwd < $choice);
// (exec to AccountsLogIn.php
?>
You said you had the selection working in #3, but when you submitted that form I’d didn’t go to the next step.
I said you need to check the value in submit...and call the restore function if it contains the value the form put in it.
You need to do that first, before the script does anything else.
Now you’re saying you’re not getting the form at all...so you’ve broken somethings that was working. Go back to when the form is presented.
I’ve not coded PHP in a decade, so I’m no help in actually debugging your code, but I’ve written hundreds of scripts (in perl) that present different pages based on the last action taken. As a web application is stateless, you need to program to recognize from which screen/form the request came and manage the response/action based on that. It’s the first thing the script needs to do!
(editorial comment: submit is a really bad name for the submit button...I usually use Action or something like doThis)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.