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.

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.