This is definately a novice question, but if you could be of any help i would be very grateful.
Basically, i'm building a database management page, and it of course includes a search function.
So, the search form looks something like this
<form name="name" function="search.php" method="get">
But, whenever i use it, i will of course get redirected to search.php. What i want is a way to display the results on the same page i did the search from (let's say index.php), without having to build an entire identical page around search.php
Thankful for answers.
Use a hidden field in the form that indicates that the form has been submitted.
In your form page (e.g. index.php)
<form name="name" action="index.php" method="post">
{OTHER_FORM_FIELDS}
<input type="hidden" name="doSearch" value="1">
</form>
So in your php code (could be in the index.php page or in a php script included)
<?php
if($_POST['doSearch']==1) {
//query database
//get results
} ?>
in your index.php page
<?php if($_POST['doSearch']) { //a search request was made display my search results ?>
HTML_CODE
<?php } ?>
Let the page submit to itself:
<form name="name" function="index.php" method="get">
In the handler for the page, check whether or not you have parameters and display either the input box or the results as appropriate.
You could even take it one step futher. You could use AJAX to insert the results directly into the page content when the submit button is pressed, rather than causing a page refresh.
Related
I need to simply get a search term from a form into a variable. I have set up a basic form within a template file, that template file is then linked to a page within Wordpress admin. The problem I am getting is that the form doesn't submit so I am unable to use the variable. If I remove get_header(); from the template then the form will submit but obviously it break wordpress stuff.
Here is my form:
<form action="<?php the_permalink(); ?>" method="post" autocomplete="off" >
<label>
<input placeholder="Search…" name="qcsearch" type="text">
</label>
<input type="submit" name="submit" value="Submit">
</ul>
</form>
I have tried leaving out the action, using the template name which is qccerts.php and using $_SERVER['PHP_SELF']
Here is then what I am trying to do with the output:
if(isset($_POST['submit'])){
$searchterm = $_POST["qcsearch"];
}else{
$searchterm = '';
}
Its basically a simple search which tells the users if there is a file by the name they search. So I need to populate $searchterm so I can use it later down the page.
Any help appreciated.
It's difficult to determine what your exact problem is without a reproducible scenario. For example, without seeing your problem, I'm not sure whether the form is really not submitted at all, or submitted, but you did not see it being executed, or there is some Javascript which prevents your form from submitting. There is a possibility that the form is submitted to the wrong action as well.
However, if you intend to keep your search term accross the pages, you could add it into session. Let's imagine these functions:
function storeSearchTerm($searchTerm) {
$_SESSION["searchterm"] = $searchTerm;
}
function getSearchTerm() {
return isset($_SESSION["searchterm"]) ? $_SESSION["searchterm"] : "";
}
By calling these functions you can manage the search term, initializing it via storeSearchTerm($_POST["qcsearch"]) or something.
As about your actual form, if it does not work, then you can submit the form in Javascript, such as
document.getElementById("myForm").submit();
and make sure that this is triggered either via an onclick attribute, or a click event listener on the button created via addEventListener.
EDIT
It turns out that a class name was not well formed (case-sensitivity issue).
I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.
I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}
I'm building a page that has a form that's submitting to my database. The form needs to post to the current page to process, but since I'm using this same form on many pages, I would like a way to tell the form to use the user's current page to submit to. I know the code below can get this information, it's what I'm using to send url data to my database for another use.
$_SERVER['REQUEST_URI']
I'm just not sure how to use that within the code that loads the page. I suppose I could write something that will pull out the submitted url info that was just submitted to my database and then plug it in, but is there a way to just use the above code to do it? That seems easier, but when I tried plugging it in, it fails and just gives me a "404 not found" error, so I know I'm not doing it correctly.
It just needs to go in the simple form post action code below:
<form action="comment_box.php" method="post">
I've looked up similar questions, but I couldn't find anything that worked with what I needed, I just got more 404 errors. Any advice would be great, thanks!
If your posting to the same page the user is on you can do
<form action="" method="post">
Leaving the form action blank it will post to what ever the page the form is on.
Not sure what language your using but I assume php you then use
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
// form submit button was pressed your php code here
} else {
// no form submit button was press do nothing maybe show form html code here
}
?>
or what your trying to achieve
$url = $_SERVER['REQUEST_URI'];
Then in PHP
<form action="<?php echo $url; ?>/comment_box.php" method="post">
for example you have a following code
<input type="submit" name="click_me" value="Login">
then in the php, you have to check like
<?php
if(isset($_POST,$_POST['click_me']) && $_POST['click_me']="Login"){
//do your stuff
}
?>
I am looking for a bit of code to do the following:
A form containing a single text field and a submit button, must send the value of the text field to a landing page that automatically counts how many html tags that this page contains.
E.g. if the text field states stackoverflow.com, the landing page should say (H1 tags = 20) with many more parameters to come.
How is this done? I know how to make a form, but I do not know how to make it send its value to the landing page.
<form action="landingpage.php/" method="post">
The URL
<input type="text" name="cf_name">
<input type="submit" value="Submit">
</form>
This piece of code is a perfect answer to your question.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
Type In Something: <input name="random-info" type="text" size="25">
<input type="submit" value="Submit">
</form> <br>
<?php
echo "You Typed: " . $_GET['random-info'];
?>
you get the method into the url, then you can use them on another page.
To access data from a form it depends on the method. Since your code shows a post message you simply access it in the php on the landing page by user $POST_['cf_name'].
To learn more you can check out:
http://w3schools.com/php/php_post.asp about the post method and http://w3schools.com/php/php_get.asp about the get method.
Also an invaluable source is php manual itself.
As far as counting the tags, not really sure what you are trying to achieve.
If you are counting the tags in the page you create, just make a variable and add to it each time you put that specific tag on the page.
Then you can put those values in a hidden field of the form to be passed into your landing page.