I have a search page with an input box in my index.php
. The form is using POST method.
<form id="someID" action="result.php" method="post">
<input name="name" id="name" type="text">
<input type="submit" value="Search">
</form>
Now I want to send a string to that input box from result.php
page using a link like this-
<a href="javascript:void(0)">Some Name</a>
I can get the content of this link using JS and can send the name to result page if I use GET method.
Example: http://example.com/result.php?name=value
But I cannot use GET here.
Can I send the content of this link as a string to the result page using POST method? Any ideas?
If you're using jQuery (to make AJAX simpler than vanilla JS) it would look like this:
$.post('http://example.com/result.php', { name: value }, function(data) {
// data -> returned HTTP request
});
U can do that with something like this, ajax here is not realy an option because you want to go to another page
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<a href="#" class="search">Foo</a>
<a href="#" class="search">Bar</a>
<a href="#" class="search">Foobar</a>
<form id="just_a_form" action="result.php" method="POST">
<input type="hidden" name="txt_name" id="txt_name" value="" />
</form>
<script>
$(function() {
$('.search').click(function(e) {
e.preventDefault();
$('#txt_name').val($(this).html());
$('#just_a_form').submit();
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['txt_name'])) {
$name = $_POST['txt_name'];
}else{
$name = '';
}
?>
<form id="someID" action="result.php" method="post">
<input name="name" id="name" type="text" value="<?php echo $name; ?>">
<input type="submit" value="Search">
</form>
</body>
</html>