Jump to content

[SOLVED] text box without form?


Yeodan

Recommended Posts

Is it possible to add a text box on a page and send the value with GET together with another value to the next page?

 

I could easily do it with a form and add a send button, but I prefer a text link, or can I just use a text link in a form?

Link to comment
Share on other sites

Ya know never thought of doing that before.

But it does work :)

a simple example

<?php
$tp=$_SERVER['PHP_SELF'];
if(isset($_GET['data']))
$data=htmlspecialchars($_GET['data'])
?>
<a href="<?php echo $tp; ?>?data=<?php echo urlencode("The quick fox jumped over the lazy dog"); ?>">Lazy Dog</a><br />
<a href="<?php echo $tp; ?>?data=<?php echo urlencode("I'm a little teapot short and stout"); ?>">Tea Pot</a><br />
<textarea name='data'><?php if(isset($data)) echo $data; ?></textarea>

 

Note the use of urlencode, to encode my data for usage in a url.

 

Link to comment
Share on other sites

Hi there :-)

 

You don't actually need any PHP to do this. There's a couple of ways you could do it.

 

Firstly, you could just add an onclick handler to a text link that takes the value from the textarea, like so:

 

<textarea id="myData">This is the data</textarea>
<a href="#" id="nextPageLink">Go to next page</a>

<script type="text/javascript">
  document.getElementById('nextPageLink').onclick = function(){
    document.location = 'page.php?myData='
                      + document.getElementById('myData').value
                      + '&someOtherValue=whatever';
  };
</script>

 

Or you could do the same thing in a form and add a handler to a text link to submit the form, like so:

 

<form name="myForm" action="page.php" method="GET">
  <textarea name="myData">This is the data</textarea>
  <input type="hidden" name="someOtherValue" value="whatever"/>
  <a href="#" id="nextPageLink">Go to next page</a>
</form>

<script type="text/javascript">
  document.getElementById('nextPageLink').onclick = function(){
    document.myForm.submit();
  };
</script>

 

Personally I prefer the second method. If you add anything to the form later on: you don't have to change the JavaScript to make the value be carried to the next page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.