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-26-2020, 01:59 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Get input data from html form inside php function


I am working on a program which includes a query to search for a partial name in a SQlite3 database. I need to get partial name input from the user to search for. Therefore I am trying to create a form inside a php function to get that data. Don't know whether this is possible.

Web Developer Tools Says " An error occurred: 200 OK " and shows the input form. How can I make this form available in the browser (firefox) so I can use the <input> to query the database?


Code:
switch ($querytype) {
	
    case "showdisplayname":
        $query.=" ORDER BY `Display_Name`;";
        break;
    case "showfirstname":   
        $query.=" ORDER BY `First_Name`; ";
        break;
    case "searchforname":
    	  $partname=getname();
    	  	   
        $query.=" ORDER BY `First_Name`; ";
        break;             
    case "showidaho":   
        $query.=" WHERE `State`='ID';"; 
        break;
    case "showarizona":   
         $query.=" WHERE `State` ='AZ';";
         break;
     case "showstate":     
         $query.=" ORDER BY `State` desc ;";
         break;         
    case "again":
    		gotoselectQuery();
    		exit();
    		break; 
     case "exit":
    		gotoindex();
    		exit();
    		break;    		  
    default:
        $query="SELECT * FROM ".$table. " WHERE 1";
        
} // switch
Here is the function getname():

Code:
function getname() {
	
echo '<!DOCTYPE HTML>'; 	
echo "<html>";
echo '<form action=# method="POST">';
echo 'Partial Name: <input type="text" name="username">';
echo '<input type="submit">';
echo '</form>';
echo "</html>";

}
 
Old 07-26-2020, 05:08 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
As far as I know it isn't possible.

HTML returns form data in the global variable $_POST. When you click on the submit button the form data is sent to the URL in the form action attribute. In your case the "action=#" will the send the post data back to itself however the script starts from the beginning.

Code:
 <html>
<body>

<form method="post" action="#">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_POST['fname'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>

</body>
</html>
 
Old 07-26-2020, 05:29 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
If the input type=submit is given a name and a value
Code:
<input type="submit" name="button" value="Begin Survey">
Then the variable button can be tested to see if it contains "Begin Survey"

I do that if there are several forms to be displayed in a script. Each looks like that above, and the value in button is used to control which section of the script is to be run next. I write in perl, however.

Point is, if you make the submit uniquely identifiable to the script, you can control what happens next in the script.
(Same as what michaelk's example demonstrates)
 
Old 07-26-2020, 05:51 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks, for replies however you are way ahead of me.

First: Am I correct in trying to put the form in the function via echo statements?
Second: Nothing shows up on the browser like a normal form would. It only shows up on the console.
 
Old 07-26-2020, 06:05 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Are you running the script from the console or web browser? Forms will only be displayed/work if run from a web server / browser.

PHP does have a read line function to read input from cli.

Last edited by michaelk; 07-26-2020 at 06:30 PM.
 
Old 07-26-2020, 07:02 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
If you put the form in a function, you have to call that function at some point.
Been awhile since I used PHP, but I don’t think you need to use echo to create the form. Again, see michaelk’s example.
 
Old 07-26-2020, 07:27 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Since the form HTML is within <?php ?> tags (I assume) it needs to be valid PHP code so using echo is necessary.
 
1 members found this post helpful.
Old 07-26-2020, 07:30 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by michaelk View Post
Since the form HTML is within <?php ?> tags (I assume) it needs to be valid PHP code so using echo is necessary.
Good point...it’d have to be in php tags to function as a function
 
Old 07-26-2020, 08:02 PM   #9
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Time Out!!

What I'm doing:

selectQuery.html script sends an AJAX call to selectQ.php. The script selectQ.php via the switch statement:

Code:
    case "searchforname":
    	  $partname=getname();
    	  	   
        $query.=" WHERE Display_Name like '%'".$partname.`%' ; ";
        break;
needs to read from somewhere input from the user which in turn is converted into a query and sent to SQlite3. I just had a thought that I might create another form inside selectQuery.html that would get the data then send that to AJAX. Does that sound feasable?

So.. lets forget all previous discussion. Then can you tell me how to get the input I need from the user inside the browser?
 
Old 07-26-2020, 10:29 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
I don't use javascript/ajax much so can't say what is the best method. I think you would need to create the a dynamic form in your javascript or use jquery in your selectQuery.html script.

Last edited by michaelk; 07-26-2020 at 10:58 PM.
 
Old 07-27-2020, 12:51 AM   #11
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
First time when the form is displayed, the value got in the _POST will be null (obviously).

Now,
Quote:
HTML returns form data in the global variable $_POST. When you click on the submit button the form data is sent to the URL in the form action attribute. In your case the "action=#" will the send the post data back to itself however the script starts from the beginning.
The global variable @_POST referred to above is a series of name=value pairs.

So when the form is displayed again
Quote:
In your case the "action=#" will the send the post data back to itself however the script starts from the beginning.
, you can scan $_POST and get the value into your php variable
AND
then branch off to another part of the code, where you proceed further ..

This php code segment would usually be at the top of the <BODY></BODY> paragraph .ie. before rendering the form again.

OK

Last edited by AnanthaP; 07-27-2020 at 12:53 AM.
 
Old 07-27-2020, 03:47 PM   #12
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
SORRY PEOPLE - I'm getting nowhere with this except more and more confused.

SO.... I'm going to shut this down and try another scheme.

Thanks for your time and help - ALL.

R
 
Old 07-27-2020, 04:12 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
I think your initial description obfuscated what you were actually trying to accomplish - assuming I understand it myself...

I think that what you are actually looking for is indeed an AJAX method, one often called auto-suggest.

If I understand your most recent posts, what you want is for a user to begin typing a name or other value in a form field, and as they type selections from the database which match the partial data will be suggested as possibilities to complete the field value. If so, search for ajax and auto-suggest as key words. It is not actually difficult, but it can be a little confusing.

The basic operation is that when the page with empty form loads it includes javascript targets (functions) which are called from the form element to be filled on each key click. When the user types a character the javascript function is called and makes the AJAX request (really just an HTTP request like any other) to another target URL (i.e. php page) which searches for matching options and returns them to the client. When the javascript callback function receives the reply it updates the target form element with the returned value or values. When the form is actually submitted it works as normal, usually being submitted back to the original page URL.

As such, thre are two (or more) php targets: The form page and its submit target, and the AJAX target for javascript to call.

So, search for ajax and auto-suggest and return to this thread when you are better armed for the task! Here are a few which I found with search for auto-suggest ajax (looks like autocomplete is a more common term nowadays, but same idea):

HowTo - Autocomplete

PHP - AJAX Autocomplete

Autocomplete using PHP MySQL and AJAX

Good luck!

Last edited by astrogeek; 07-27-2020 at 04:38 PM. Reason: typos
 
  


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
[SOLVED] html form - Unable to have 2 form 'submit' on same line. rblampain Programming 3 11-26-2015 09:57 PM
html: submitting form data as GET parameters instead of POST? stateless Programming 7 02-15-2013 01:17 PM
Two elements inside a form in HTML - how? (interfacing with PHP) resetreset Programming 3 09-18-2010 10:24 AM
HTML form data in PHP without postback? linuxlover.chaitanya Programming 18 02-11-2010 11:10 PM
PHP: html Form Select function problem camlinux Programming 2 05-15-2005 08:23 PM

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

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