homeUrl doesn't work in form action - php

I have created a form with a search button above it.
I write something in the search box and submit it but it returns to web/index?search='something'.
I want it to return to where I am now. I mean, index and I use the following code:
<form action="<?=Yii::$app->homeUrl?>?r=phone/index">
<input type="text" name="search" placeholder="Search..">
<input type="submit">
</form>
phone is my controller and index is the page I have my form in.
Thanks

homeUrl will send you to where you've configured it or to it's the default location if you haven't. Try this if you want to send the form data to the same URL where the form is:
yii\helpers\Url::to();

Change
"<?=Yii::$app->homeUrl?>?r=phone/index"
By:
"<?=\yii\helpers\Url::to(['phone/index'])?>"

Related

Next page can't be found if input on previous page was filled

I'm making a form on Wordpress website. My problem is easiest shown in practice: try submitting empty form (first one, black) on http://newzapchasti.evella.ru/ and it will redirect to the next page. Then try filling out the last input field of this form and submit it again. The same page (http://newzapchasti.evella.ru/quest.html) isn't found this time.
My code is very simple:
<form action="/quest.html" method="post">
<input name="year" type="text">
<button type="submit">send</button>
</form>
Why does it happen?
On the second page the form action is
<form action="http://newzapchasti.evella.ru/" method="post">
While on the first one is
<form action="/quest.html" method="post">
That may be the reason :)
Try changing input field name attribute to other values, some names could make existing page display as 404.

Display form input value as part of a new page url

I'm trying to display user entered value in form A to be part of the url of page B. For example:
Form A:
<form method="post" action="display_annotation_for_pdbid.php" target="_parent">
<fonttdresult><b>Search Database by PDB ID:</b><br/></fonttdresult>
<input type="text" name="pdbid" id="pdbid" placeholder="Enter a PDB ID" style="width:
106px;">
<input type="submit" name="submit" value="Submit">
</form>
So instead of displaying the new page url as display_annotation_for_pdbid, I want it to display something like 'display_annotation?StructureID:1y26' when the user enters 1y26 in form A. Can someone help me with this?
if you use GET method iin your form to post data , variables will automaticlly be placed in URL
Create another file display_annotation_for_pdbid.php which will contain code to redirect to required url. Try below code
display_annotation_for_pdbid.php
<?php
$pdbid = $_POST['pdbid'];
header("Location : display_annotation.php?StructureID=".$pdbid);
?>

Submit button showing in GET URL

I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

Send form results to another page php mysql

Hello I have a form working properly using
php echo $_SERVER['PHP_SELF']
The results are shown on same page as form.
I need to be able to send the form data and results to another page (eg results.php) when the user submits the form.
How is this achieved?
Just point the form action to results.php
<form action="results.php">
<!--Inputs-->
</form>
Or if you need to do a redirection store $_GET at $_SESSION['parameters'] .
Here are some attributes of form tag <form> you need to set them..
<form action="controller.php" method="post">
<!--form elements-->
<input type="submit" name="submit" value="Submit Form" />
</form>
Here when ever the submit button clicked, it will submit the form to its action i.e. controller.php by post method (i.e. form elements' value won't display in query string).
Later you can access their value on controller.php by $_REQUEST['element_name'] or $_POST['element_name'] or $_GET['element_name'] according to the form method type.

Categories