Searched hours online to no avail.

This should be fairly easy for anyone experience with PHP (aka not me!)

All i'm trying to do is create a text search box and send button.


1. Users will add a keyword and click send.

2. Link to an external website that loads with keyword in the url. eg, Searching for: Paper


action="http:www.domain.com/st=123&129ss=PAPER"

I got as far as (and being a complete PHP noob):

<?php
$keyword = htmlspecialchars($_POST['keyword']);
?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="post" id="searchform" action="http://www.domain.com/default.aspx?st=FT&ss=<?php echo 'keyword'; ?>" />
<input type="text" name="keyword" id="se" size="35"  onblur="if (this.value == '') {this.value = 'search...';}" onfocus="if (this.value == 'search...') {this.value = '';}" value="search..." class="text" />
<input type="submit" id="searchsubmit" class="submit" value="Send" />
</form>


</body>
</html>

so damn frsutrating despite getting close. With above example, entering and submitting a keyword does direct to the new site, but the echo doesn't show.

Any advice?? Would be appreciated.

Dani AI

Generated

Short answer: let the browser build the query string (use GET with properly named inputs) or build the final URL and redirect with JavaScript. That avoids the server-side timing issue that hit (PHP runs before the form is submitted). was right to suggest a client-side redirect and pointed toward fixing the form action; caught the common quoting mistake. Two safe, simple patterns follow.

HTML-only (simplest — no PHP or JS required)

<form id="searchform" method="get" action="https://example.com/default.aspx">
  <input type="hidden" name="st" value="FT" />
  <input type="text" name="ss" id="se" placeholder="search..." />
  <input type="submit" value="Send" />
</form>

This submits to:

JavaScript (build and redirect; useful if needing client-side validation or encoding)

<script>
document.getElementById('searchform').addEventListener('submit', function(e){
  e.preventDefault();
  var kw = document.getElementById('se').value.trim();
  if (!kw) return; 
  var url = 'https://example.com/default.aspx?st=FT&ss=' + encodeURIComponent(kw);
  window.location.href = url;
});
</script>

A short jQuery variant (wrap in DOM-ready):

<script>
$(function(){
  $('#searchform').on('submit', function(e){
    e.preventDefault();
    var kw = $.trim($('#se').val());
    if (!kw) return;
    window.location.href = 'https://example.com/default.aspx?st=FT&ss=' + encodeURIComponent(kw);
  });
});
</script>

Notes and cautions: always use encodeURIComponent before inserting user text into a URL. Prefer the placeholder attribute instead of onfocus/onblur hacks. If the goal is server-side processing with PHP, have the form POST to a PHP endpoint on the same site and then redirect server-side (read $_POST, validate, then header("Location: ...")). Also avoid printing literal 'keyword' — the PHP variable must be echoed as the variable (and sanitized) when appropriate.

Recommended Answers

All 10 Replies

your form is submitting a value but php is not getting it .
so echo will print nothing

post data is sent to action =" " site

The problem is that $keyword isn't set until after you have sent the user to the other page. You should use javascript so send the user to the url instead of a form.

jQuery code would look like this:

$('#searchsubmit').click(function(){
var keyword = $('#se').val();
window.location = http://www.domain.com/default.aspx?st=FT&ss=+keyword;
});

Then just take the form tag off altogether and leave the input and submit fields.

Hi xylude,

I tried the above but can't get it working:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="http://code.jquery.com/jquery-1.5.js"></script>

</head>

<body>

<input type="text" name="keyword" id="se" size="35"  onblur="if (this.value == '') {this.value = 'search...';}" onfocus="if (this.value == 'search...') {this.value = '';}" value="search..." class="text" />
<input type="submit" id="searchsubmit" class="submit" value="Send" />

<script type="text/javascript">
$('#searchsubmit').click(function(){
var keyword = $('#se').val();
window.location = http://www.domain.com/default.aspx?st=FT&ss=+keyword;
});
</script>

</body>
</html>

The form is generate, but clicking with search term doesn't do anything.

your form is submitting a value but php is not getting it .
so echo will print nothing

post data is sent to action =" " site

Hi, Thankls for your reply. Would you know how to fix this with PHP?

and yes if you want to directly do it
then look at javascript

The javascript method (above) isn't working... :(

<script type="text/javascript">
$('#searchsubmit').click(function(){
var keyword = $('#se').val();
window.location = http://www.domain.com/default.aspx?st=FT&ss=+keyword;
});
</script>

You need jquery library to work the above method. And window.location = 'http://www.domain.com/default.aspx?st=FT&ss='+keyword; is wrong. Should be window.location = 'http://www.domain.com/default.aspx?st=FT&ss='+keyword; You must link the jquery library to work the above method, or try with simple js.

<script type="text/javascript"
var keyword = document.getElementById('se').value;
window.location = 'http://www.domain.com/default.aspx?st=FT&ss='+keyword;
</script>

Hope this help.

Well, that is my fault. You didn't place the quote in your syntax, right ?

<script type="text/javascript">
$('#searchsubmit').click(function(){
var keyword = $('#se').val();
window.location = http://www.domain.com/default.aspx?st=FT&ss=+keyword;
});
</script>

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.