LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2020, 08:08 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 18.04
Posts: 335

Rep: Reputation: 9
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:

Quote:
return value

<!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>
<option value='1'> 2020Oct14-14:40:24_accounts.sql </option>;
<option value='2'> 2020Oct16-09:36:26_accounts.sql </option>;
<option value='3'> 2020Sep18-13:31:43_accounts.sql </option>;
<option value='4'> 2020Oct13-13:17:01_accounts.sql </option>;
<option value='5'> 2020Oct13-13:16:03_accounts.sql </option>;
<option value='6'> 2020Oct13-13:38:05_accounts.sql </option>
;
</select>
<input type='submit' name='submit' value='Get DB' >
</form>

</body>
</html>

Last edited by pizzipie; 10-16-2020 at 08:10 PM. Reason: add word
 
Old 10-17-2020, 02:24 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,080

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
At 'success:', when you have the new html-page in 'retval', do something like this:
Code:
document.open("text/html", "replace");
document.write(retval);
document.close();
or
Code:
document.innerHTML= retval;
 
Old 10-18-2020, 05:12 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 18.04
Posts: 335

Original Poster
Rep: Reputation: 9
Thanks NeVem Teve,

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>
Code:

//restoreBakups.php

<?php

include './include/myPhpFunctions.inc';

error_reporting(-1);
ini_set("display_errors", true);

   $hostname = "localhost";
   $database = "accounts";
   $username = "rick";
   $password = "w78yt94jglh";
   $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);
  }
}

?>
<!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'];
        $choice=$pickFile[($selected-1)];
        echo $choice;     
    } else {
        echo 'No Database to Restore ... Aborting in 5 seconds.';
        sleep(5);
         
    }
} // isset


//shell_exec(mysql-u$username -p$passwd < $choice);

//echo "finished";
?>
Attached Thumbnails
Click image for larger version

Name:	Selection_001.png
Views:	4
Size:	12.6 KB
ID:	34334  
 
Old 10-18-2020, 06:16 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,216

Rep: Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913
You need to check the value of submit to decide to call/go to the recovery code instead of the init.php

Do that first...pseudocode
Code:
If submit == ‘Get DB’
Go to recover
// if  not equal, will drop thru to login.
You have to evaluate what’s being sent every time to know what to do next...

Last edited by scasey; 10-18-2020 at 06:17 PM.
 
Old 10-19-2020, 06:36 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 18.04
Posts: 335

Original Poster
Rep: Reputation: 9
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

?>
 
Old 10-19-2020, 07:30 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,216

Rep: Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913Reputation: 1913
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use web to invoke php program, and the php program call c program bellhuang Linux - Server 10 01-13-2014 06:09 AM
want to display database values in select box using php sunlinux Programming 3 08-08-2012 03:26 AM
how to show results of a php query from a form on the main form texmansru47 Programming 2 06-27-2008 01:26 PM
LXer: Sun Microsystems Delivers Three-in-One Punch to the Competition With World Record Results on Three Operating Systems, Including Solaris 10 LXer Syndicated Linux News 0 02-07-2006 09:46 PM
PHP show query results Gerardoj Programming 1 05-11-2004 12:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration