LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-27-2020, 06:43 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Rep: Reputation: 12
What determines whether firefox will open a file (PHP or HTML) in the browser


Using Ubuntu 18.04

SAME PAGE PROCESSING
Quote:
How can you process a form on the same page vs using a separate process page.
I got this code, copied here from the internet, as an example of having a form and php open on same page.

I am trying to open it as: test2.php. It does not work. A window comes up asking if you should open the file with firefox.When you say yes the samw window asks the saqme question. Over and Over and Over.



Code:
<?php
//Form submitted
if(isset($_POST['submit'])) {
  //Error checking
  if(!$_POST['yourname']) {
    $error['yourname'] = "<p>Please supply your name.</p>\n";
  }
  if(!$_POST['address']) {
    $error['address'] = "<p>Please supply your address.</p>\n";
  }

  //No errors, process
  if(!is_array($error)) {
    //Process your form

    //Display confirmation page
    echo "<p>Thank you for your submission.</p>\n";

    //Require or include any page footer you might have
    //here as well so the style of your page isn't broken.
    //Then exit the script.
    exit;
  }
}
?>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <?=$error['yourname']?>
  <p><label for="yourname">Your Name:</label><input type="text" id="yourname" name="yourname" value="<?=($_POST['yourname'] ? htmlentities($_POST['yourname']) : '')?>" /></p>
  <?=$error['address']?>
  <p><label for="address">Your Address:</label><input type="text" id="address" name="address" value="<?=($_POST['address'] ? htmlentities($_POST['address']) : '')?>" /></p>
  <p><input type="submit" name="submit" value="Submit" /></p>
</form>

Last edited by pizzipie; 07-27-2020 at 06:50 PM.
 
Old 07-27-2020, 07:24 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,277
Blog Entries: 24

Rep: Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225
It sounds as if you may be trying to open the file with your browser from your filesystem, or perhaps by simply clicking on it, but NOT from your web server (i.e. via HTTP request).

Neither Firefox nor your DE knows anything about PHP. Firefox decides whether to display the content itself or ask about other applications based on the mime type and/or response headers returned by the server. Your DE relies on mime type. When you open it as a file instead of via HTTP request there are no response headers and the PHP code is not executed but simply opened as a text file and Firefox may ask what to do with it if you have not set a PHP handler (but even if you have set a handler, it is still just text, not executed code).

So, first question is how exactly are you trying to open it?

Last edited by astrogeek; 07-27-2020 at 07:40 PM.
 
1 members found this post helpful.
Old 07-28-2020, 03:36 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Same as previous poster:
Firefox cannot parse PHP, full stop (even though some web pages end in .php, it's still server-side scripting).
Even if you somehow manage to shoehorn it into opening that file, it will only open as text. And what would be the use in that.
 
1 members found this post helpful.
Old 07-28-2020, 05:26 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
You want to install LAPP or LAMP stack on your machine.¹ There are many guides on the Internet describing how to do it and with Ubuntu it’s probably just a matter of a few simple ‘apt install’ commands. Once you have Apache running with PHP configured, you’ll be able to open the script through http://localhost/ link.

¹ You probably can skip the database for the time being to make things a little bit simpler.
 
Old 07-28-2020, 08:03 AM   #5
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Depending on your version of PHP (I'm looking at 7.4), it might have a built-in webserver. Then you won't need to set up Apache or Nginx.
Code:
php -S <addr>:<port> <script.php>
 
1 members found this post helpful.
Old 07-28-2020, 11:16 AM   #6
captain_sensible
Member
 
Registered: Apr 2010
Posts: 352

Rep: Reputation: 145Reputation: 145
Also because sqlite3 is server-less unlike MySQl you can also do crud stuff with php using sqlite3 using dev server as per last post.
 
Old 07-28-2020, 07:09 PM   #7
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Original Poster
Rep: Reputation: 12
OK. got the php file (test3.php) running with my virtual host. Now if I want to run just test3.php I get this see attachment: partialName.png my code:
PHP Code:

<?php
$Fname 
$_POST["Fname"];
$Lname $_POST["Lname"];

print_r($_GET);
if(isset(
$_GET)) { 
$querytype=$_GET['query'];
echo 
$querytype;
}

?>

<html>
<head>
<title>Personal INFO</title>
<script  src="../jquery-3.3.1.js" ></script> 
<style>

form {
    width: 400px;
    height:250px;
    border:3px solid black;
    background-color: lightgray;    
}

input[name="submit"] {
display: none;
}

</style>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Enter Partial Name:<input type="text" name="Fname"><br />
<!--Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />-->
<input class="hov1" type="submit" name="submit"> <br />
</form>
<?php

echo ($Fname);
?>

If I run test3.php through my regular program selectQuery.php which calls test3.php with AJAX
like:
I get see attachment partName2.php

Code:
<script type="text/javascript" >

//  ============= submit ======================================

$(function () {

var key="";
var val="";

$('#revdelbox').hide();
$('#warning').hide();

  	$('input[name="query"]').on('change', function() {          // get value from checkbox
  		$('input[name="query"]').not(this).prop('checked', false);
   		selectquery= $('input[name="query"]:checked').val();
   		//alert("query="+selectquery);
   		
   		
  
 if (selectquery=="searchforname") {
	   			
  var request=$.ajax ({
    url: "test3.php",
    type: "GET",
    data:  "query="+selectquery,
    dataType: "text"
 }).done(function(text) {
alert(text);
return false;
});
  
} else {
  var request=$.ajax ({
    url: "selectQ.php",
    type: "GET",
    data:  "query="+selectquery,
    dataType: "json"
 }) // var 	
 	
} // else

 		
request.done(function(retval) {  //  ====================  REQUEST DONE ==================
Makes no sense to me what I am doing wrong REALLY FRUSTRATED

R
Attached Thumbnails
Click image for larger version

Name:	partial_name.png
Views:	19
Size:	5.9 KB
ID:	33750   Click image for larger version

Name:	partialName2.png
Views:	18
Size:	32.2 KB
ID:	33751  
 
Old 07-28-2020, 08:38 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,814

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
I don't think you understand how ajax works.

Basically test3.php is a standalone all in one script. When you "run" the script from the browser you see the empty form (will ignore the Array() and all the other errors for now). When you enter something in the text box and press enter, the script is called and starts from the top which reloads the entire page. The variable Fname is assigned the string you entered in the text box ( $Fname = $_POST["Fname"]; ) and then displayed below the form using the echo ($fname) statement;. All the other stuff is CSS.

ajax in a nutshell is a way to send and retrieve data from a server (database in your case) asynchronously (in the background) without interfering with the display of the existing page which allows you to change content dynamically without the need to reload the entire page. I'm not much of a javascript programmer but java is just calling the test3.php script versus the web browser actually running the CSS commands which is why you see that text from your partialname2.php image.

Since I am not much of an ajax/javascript programmer I believe your javascript will need to create the dynamic partial name form in your selectQuery.php. Hopefully others will reply with some example code.
 
1 members found this post helpful.
Old 07-29-2020, 08:31 PM   #9
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Original Poster
Rep: Reputation: 12
Brainstorm: Just got a hint about a Javascript function I had forgotton about; Window.prompt()

Solve all my problems with three lines or so of code!!

Thank you all once again for your time and help.

R
 
Old 07-29-2020, 08:37 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,763

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
I have several O’Reilly books, but I almost always go to w3schools these days.
 
  


Reply



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
Selenium: Check whether button exist or not and if exist whether the the text is in it is desired one or not daichiash General 0 07-03-2020 10:17 AM
What determines whether I should enable ipv6 when installing Linux? veeruk101 Linux - Newbie 1 06-14-2011 03:24 AM
K3b can't determines size of image file : Brasero won't mult-session theKbStockpiler Linux - Software 1 09-22-2010 12:05 PM
Question on how linux determines drive "names" bpalmer Linux - Hardware 4 03-01-2005 05:56 AM
How do I write a make install Makefile that determines if user is root? SheldonPlankton Programming 3 08-13-2004 05:39 PM

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

All times are GMT -5. The time now is 10:31 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