i want to get the values from a form before its action redirect it.
for example in this form, i want to grab the "text_one" and send it to database before it be redirected to google. I also want "text_one" in google too.what should i do?
<form method="post" action="google.com">
<input type="text" name="text_one">
<input type="submit">
</form>
try this ...
<form method="post" onsubmit="return getdata()" action="google.com">
<input type="text" name="text_one" id="text_one">
<input type="submit">
</form>
<script>
function getdata(){
var txtOne = document.getElementById('text_one').value;
// Do Something
}
</script>
You can change the action to "yourscript.php" and do s.th. like:
<?php //yourscript.php
//save $_POST['text_one'] to Database
header('Location: http://google.com');
?>
Or you can call the "yourscript.php" with ajax to do it in the background.
Try this :
echo (isset($_POST['text_one']) ? $_POST['text_one'] : '');
or Use Ajax Ajax is the answer of your question
For a pure PHP solution, you can work with the idea presented by #v.eigler. In order to create a POST request to a Google server (or what ever server you want), you just need to use some library to make the HTTP request, I strongly recommend you to take a look at the Guzzle library.
Using this should be easy enough, you just need to redirect the form handling to a script that you own, do your own processing and then create an HTTP post to the real destination.
I do it with little complexity.
I change your form action handler.
<form method="post" action="yourscript.php">
<input type="text" name="text_one">
<input type="submit">
</form>
Now, Its time to handle it server side. I have put comments for explanation.
<!-- yourscript.php -->
<?php
echo $_POST['text_one'];
// also do required server side operation.
?>
<!-- note: Action part is google.com -->
<form id="myform" method="post" action="google.com">
<!-- Note: value of input already set.-->
<input type="text" value=<?php echo $_POST['text_one'];" ?> name="text_one">
<input type="submit">
</form>
<script language="JavaScript">
// submit your form as soon as page loaded.
document.myform.submit();
</script>
Related
Hi guys im quite newbie on coding but i want to ask How to post HTML FORM Data to a http link given to you by a client thanks.
Try creating a .html file as below:
<html>
<form method="post" action="urclientlink">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="text" name="submitButton" value="Submit Form"/>
</form>
</html>
Please elaborate on your question for a better answer. Thanks!
This is no different from submitting form data to a page on your own site. You simply amend the form's action attribute:
<form action="http://another-server.com/another-page">
You also need to be sure you get the right METHOD: e.g. GET or POST:
<form action="http://another-server.com/another-page" method="GET">
You will need to check with the client to see which page and method they need.
I've been working for several hours trying to get this to work properly. The page I have a form on is /index.php?action=pagename. I have a form that needs to get a variable from the following /index.php?action=pagename&thing=something. My HTML form going like this:
<form role="form" action="pagename" method="get">
<input type="text" name="thing">
</form>
This form is located on /index.php?action=pagename and I want to get &thing from that URL.
Any ideas?
The problem I'm having is that when the form is submitted, the URL redirects to index.php?thing instead of staying on index.php?action=pagename.
It looks like you might be having some trouble with <forms> in general and not just PHP so here is an overview:
<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->
<form action="index.php" method="GET">
<!-- add a hidden value for pagename -->
<input type="hidden" name="action" value="pagename">
<!-- the name called "thing" will be appended to the URL and it's value as well -->
<input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
<br>
<!-- click this button to submit the form to itself -->
<!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
<input type="submit" value="submit" />
</form>
You can simply use the PHP-variable $_GET['thing'] to get the value.
The action attribute of the form element is the target script which will be called on submit. If blank it will be the current script which is showing the form. You also can only give url parameters beginning with ?.
<form role="form" action="?action=pagename" method="get">
<input type="text" name="thing">
</form>
Exerpt from php.net: http://www.php.net/manual/de/tutorial.forms.php
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element will automatically be available to your PHP scripts.
Please read the manual section on Variables from external sources for
more information and examples on using forms with PHP. Here is an
example HTML form:
Example #1 A simple HTML form
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
A sample output of this script may be:
Hi Joe. You are 22 years old.
Basic script I made to get the variables from the URL:
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
This will output the following for the URL /index.php?action=pagename&thing=something
pagename
something
Though you should probably learn how to use forms properly first.
i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank
while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.
<form action ="somepage.php" method="post">
<!--all your input elements-->
<input type="submit" name ="international" value="international"/>
<input type="submit" name ="domestic" value="domestic"/>
</form>
determine which button have been clicked and act accordingly.
if(isset($_POST['domestic']) {
//include dom.php
}
else if(isset($_POST['international']) {
//include int.php
}
and then you can include the necessary file.
or the other way is to go with AJAX/jQuery way
you can just use switch in php for differ or
use javascript
Do it with jquery! First, dont create submit buttons just create
<input type="button" />
Than give them an id like:
<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />
and with jquery bind the action
$(document).ready(function(){
$("#InternationalBTN").bind('click',function(){
$('#idOfYourForm').attr('action','setTheDestinationPageHere');
document.forms['nameOfYourForm'].submit();
});
});
That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).
If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:
if(isset($_POST["name_of_international_button"])){
//actions to perform for this --
}
if(isset($_POST["name_of_domestic_button"])){
//action to perform for this
}
In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts
You can do it in this way:
In form tag please leave empty action action=""
2 buttons to send:
<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>
and use ajax:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
$('#International ').click(function() {
// send to file 1 using ajax
});
$('#Domestic').click(function() {
// send to file 2 using ajax
});
});
</script>
Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked
<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
<input type="text" name="field1" />
<input type="text" name="field2"/>
<input type="submit" value="international "name="international"/>
<input type="submit" value="domestic "name="domestic"/>
</form>
Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,
Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...
Easy as that :D
I have following code in my php file
<?php
if(isset($_POST['updatebtn']))
{
mysql_query("update table......");
}
?>
<script type="text/javascript">
window.addEvent('domready', function(){
new FormCheck('myform');
});
</script>
<form name="myform" method="post" id="myform" action="">
<input type="text" class="validate['required']" />
<input type="submit" value="updatebtn" />
</form>
as you can see i have added mootools form validation in my file. mootools form validation work only if i add id="myform" in my form. but if i add that (i. e. id="myform"), it is creating problem, means it is not executing update query that i have written at top.
if i remove id="myform" from my form tag, it is executing that update query
do anyone have any idea??
The javascript would validate the input and prevent the form to submitting so it's normal that your server side script won't run.
I think you should make a difference between things run on server side, such as PHP and things run on client side, i.e. javascript.
After I add a button and a text field, how can I program that button to simply take what's in the text box and put it into a variable? I have no idea how the button click event works.
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="xxxxxxxxx " />
</label>
<label>
<input type="text" name="txtboxsearchbydistro" id="txtboxsearchbydistro" />
</label>
</form>
Would I put a PHP statement in the space where the xxxxxxxx is at?
Any help would be great!
You can't execute PHP code in onclick() statements because PHP gets executed on the server, before the page is sent to the browser, and the onclick() function is exectued at the browser.
Solution would be (assuming this page is form.php) set the action of the form for "form.php" and on that page have
if(isset($_POST)){
$variable = $_POST['txtboxsearchbydistro'];
// Here you can run validation on $variable, sanitize it and pass it to a DB query
}
No, php is server-side, and onClick is client-side event.
I'm not entirely sure what are you trying to accomplish. If you wish to submit your txtboxsearchbydistro value to some PHP script, you would put something like this:
<form id="form1" name="form1" method="post" action="somePhpScript.php">
Then you would use something like Bobby proposed.
If you wish to do something before you actually send the form, or you want to do something on client side (i.e. in visitor's browser), you'd need to do something like
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="myScript();" />
You could then need to define your script, and assign your value there.
Hope it helps.