[SOLVED] Get input data from html form inside php function
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.
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
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>
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)
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.
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.
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?
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.
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.
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):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.