Hi..
im PHP user..
i want to get a text box value to the Php variable by clicking hyperlink and i want to send that value to database using mysql query. i want to know that is how can i get text box value to the PHP variable clicking by hyperlink. not by clicking submit button..
thank you..

Dani AI

Generated

If you want a hyperlink to carry the textbox value to PHP (without a form submit), build a query string in JavaScript and redirect. Use encodeURIComponent (not the deprecated escape) so spaces and special characters are encoded correctly; URLSearchParams makes this cleaner (MDN, encodeURIComponent).

<input id="txt" type="text" />
<a id="go" href="/save.php">Save</a>

<script>
document.getElementById('go').addEventListener('click', function (e) {
  e.preventDefault();
  const v = document.getElementById('txt').value.trim();
  if (!v) return; // optional: prevent empty
  const url = new URL(this.href, location.origin);
  url.searchParams.set('textboxname', v); // ?textboxname=...
  location.href = url.toString();
});
</script>

On the PHP page, read the value from $_GET, validate it, then use a prepared statement to store it safely (avoid SQL injection). See PHP docs for $_GET and PDO prepared statements.

<?php
$name = $_GET['textboxname'] ?? '';
if ($name === '' || strlen($name) > 255) {
    http_response_code(400);
    exit('Invalid input');
}

$pdo = new PDO('mysql:host=localhost;dbname=app;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare('INSERT INTO your_table (col) VALUES (?)');
$stmt->execute([$name]);

Notes: GET exposes data in the URL; do not use it for sensitive info. If you want to save without leaving the page, send a POST via fetch to your PHP endpoint (MDN fetch).

Recommended Answers

All 9 Replies

hmm... if I understand you, you have a text box that a user will enter data into and when they click a link (<a href='blah.php'>) you want the contents of that text field passed as a parameter to the file on the link (blah.php). Is that right?

I think you would do this with javascript on the page with the text box using an onclick="do this stuff here" function to get the contents of the text box and modifying the href.

I'm just guessing here but thats where I'd start...

try to use
<a href='blah.php?textboxname='+formname.textboxname.value>
something like that.

yep... what he said! ^^^^^^

yeah ppetree u got my prob.. i can get that value using java script . bt thing is i dont knw hw to get value from java script variable to php variable. coz i need that value pass to php variable. can u plz tell me..

<a href='blah.php?textboxname='+formname.textboxname.value>

just read using $_GET

alternatively, use an ordinary FORM, and submit it using javascript.

onclick = "document.forms[0].submit()"

<input type="text" name="url_param" id="url_param" /> 
<input type="button" onclick="prepare_link();" value="Do it!" /> 
 
<a href="http://whatever.com" id="target_link" onclick="append_link()">Click Your New Link!</a>
<script type="text/javascript"> 
function append_link() { 
  var url_param = document.getElementById('url_param'); 
  var target_link = document.getElementById('target_link'); 
 
  if ( ! url_param.value ) { 
      return false;   
  } 
 
  target_link.href = target_link.href + '/=url_param?' + escape(url_param.value); 
} 
</script>
<script type="text/javascript">
function ftest(){
window.location="totherecievingpagedatabasesave.php?test="+document.getElementById('test').value;
}
</script>

<input type="text" name="test" id="test" />
<a href="javascript:void(0);" onclick="ftest()">click me</a>

then get the text box variable on the receiving end via $_GET;

commented: thank you +0

I think I like Vault's better! LOL

Obviously, there is more than one way to do this!

Thank you for all to answering me.. i did it.. and thanx vaultdweller123.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.