1

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>
MMILAD
  • 129
  • 1
  • 2
  • 14
  • Ajax should be able to do this without too much trouble, just right before you submit the form let Jquery catch your request and before doing the default action let it do an Ajax call to some php file which saves your data. – Gerton Jun 19 '15 at 11:42
  • possible duplicate of [How to get input field value using PHP](http://stackoverflow.com/questions/13447554/how-to-get-input-field-value-using-php) – hlh3406 Jun 19 '15 at 11:44
  • Use `onchange="yourfunction()"` – Bruce Jun 19 '15 at 11:50
  • i can't work with ajax – MMILAD Jun 19 '15 at 11:51
  • possible duplicate of [html submit without submit button or JavaScript](http://stackoverflow.com/questions/3077350/html-submit-without-submit-button-or-javascript) – Sagar Naliyapara Jun 19 '15 at 11:51

5 Answers5

3

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>
Sarath
  • 2,318
  • 1
  • 12
  • 24
1

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.

codeneuss
  • 905
  • 4
  • 12
0

Try this :

echo (isset($_POST['text_one']) ? $_POST['text_one'] : '');

or Use Ajax Ajax is the answer of your question

hy_sultani
  • 27
  • 3
  • 14
0

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.

mTorres
  • 3,590
  • 2
  • 25
  • 36
0

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>
jagad89
  • 2,603
  • 1
  • 24
  • 30