0

I have to send text field value using href to php is something like below. But it is not correct way. Can anyone please give me any solution?

<input type="text" id="myText" value="Mickey">
<a href="test.php?id=javascript:document.getElementById('myText').value;">
<input type="button" value="Click"></a> 
1
  • that won't do anything - place it inside a form.. also I'd recommend using $_POST over $_GET - for me $_POST is cleaner and doesn't make the url look weird. Commented May 23, 2017 at 15:34

2 Answers 2

2

Put content inside a form. You can also change the button type input to a submit type, this way the form is sent automatically on click.

<form method="POST" action="yourURL.php">
    <input type="text" id="myText" name="myElement" value="Mickey">
    <a href="test.php?id=javascript:document.getElementById('myText').value;">
    <input type="submit" value="Click"></a> 
</form>

More information on forms: MDN

Whether you use GET or POST as a method, you'll be able to access the content of the form through PHP variables: $_GET, $_POST or the generic $_REQUEST.
More information in the PHP documentation

Note: PHP uses the name attribute of your HTML elements for those variables. Make sure to add this attribute to your HTML elements otherwise you'll have a hard time getting a value from $_REQUEST['myText']. I added the attribute holding the value "myElement" in the above code. It is accessible through PHP by typing $_REQUEST['myElement'].

Content sent through GET method is visible in the URL, like this: www.example.com/test.php?var1=test&var2=test

Sign up to request clarification or add additional context in comments.

Comments

1

<input type="text" id="myText" value="Mickey">
<a href="test.php" onclick="this.href = this.href +'?' + document.getElementById('myText').value;">test</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.