Jump to content

How to send search keyword to external url in PHP Form


PSM

Recommended Posts

Hi guys,

 

I'm not so good with PHP (hence my post here).  Other forums have suggested using javascript, but if not enabled by the user, then it's not much good).

 

I'm trying to set up a search box with submit button.  For any keywords searched for, I would like the search terms sent to an external site in the format:

 

http://www.domain.com/default.aspx?st=FT&ss=XXXXX

 

(where XXXXX is the keyword).

 

 

So far, I have:

 

<?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>

 

 

.and although it directs to the url, the keyword(s) is not present.  Any help would be appreciated.

Link to comment
Share on other sites

Try using

 $_GET['keyword'] 

I think he wants to do it the other way around.

 

At the OP'er:

You need to process the $_POST['keyword'] into an array (f.e. accept comma's to separate keywords and convert whitespace and other symbols with urlencode())

 

Then you can use

header("Location:http://www.domain.com/default.aspx?st=FT&ss=$keyword")

to send the user to that page. (my example will only work when there is only 1 keyword, you need more processing to add multiple keywords to that URL)

 

Vincent

Link to comment
Share on other sites

<?

//This is only displayed if they have submitted the form

if ($searching =="yes")

{

echo "<h2>Results</h2><p>";

 

//If they did not enter a search term we give them an error

if ($find == "")

{

echo "<p>You forgot to enter a search term";

exit;

}

 

// Otherwise we connect to our Database

mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error());

mysql_select_db("database_name") or die(mysql_error());

 

// We preform a bit of filtering

$find = strtoupper($find);

$find = strip_tags($find);

$find = trim ($find);

 

//Now we search for our search term, in the field the user specified

$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");

 

//And we display the results

while($result = mysql_fetch_array( $data ))

{

echo $result['fname'];

echo " ";

echo $result['lname'];

echo "<br>";

echo $result['info'];

echo "<br>";

echo "<br>";

}

 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that

$anymatches=mysql_num_rows($data);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query<br><br>";

}

 

//And we remind them what they searched for

echo "<b>Searched For:</b> " .$find;

}

?>

use this code and solve your problem..

Link to comment
Share on other sites

@hebeeb

 

this is nothing like what I'm trying to achieve (thanks though!).

 

All I need is:

 

1.  Search box + submit button in html page

 

2.  I enter in the search:  telephone

 

 

When i submit, I want the user to be directed to page with the keyword in the url like:

http://www.domain.com/default.aspx?st=FT&ss=telephone

 

I don't need to display results as the link is already waiting for this incoming url.

 

Link to comment
Share on other sites

@ Muddy_Funster

 

To answer your questions:

 

1.  Where, other than the page that you have posted the code from, is 'keyword' assigned?

Nowhere - just this search form

 

2.  is the page that you have posted the code from actualy default.aspx?

Yes, and it is working if I navigate to :

http://www.domain.com/default.aspx?st=FT&ss=keyword1

http://www.domain.com/default.aspx?st=FT&ss=keyword2

http://www.domain.com/default.aspx?st=FT&ss=keyword3

 

That side is ready to go, just need to get the keyword search to convert into the end of the url.

 

 

 

@zenlord

 

As mentioned, I'm not so good with PHP, but following your advice, I tried:

 

<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"  value="search..." class="text" />
<input type="submit" id="searchsubmit" class="submit" value="Send" />
</form>

<?php
$keyword = htmlspecialchars($_POST['keyword']);
array($keyword , create_function('&$v,$k', '$v = $k."=".$v ;'));
echo '<a href="http://www.domain.com/default.aspx?st=FT&ss=', urlencode($userinput), '">';
?>


</body>
</html>

 

But that failed miserably.  This is like learning ancient chinese...(!)

 

I'd appreciate your guidance and time zenlord

 

Link to comment
Share on other sites

right, lets get this line sorted out first,

<form method="post" id="searchform" action="http://www.domain.com/default.aspx?st=FT&ss=<?php echo 'keyword'; ?>" />

[code=php:0]

what you want this to do is :

[code=php:0]

<form method="post" id="searchform" action="http://www.domain.com/default.aspx?st=FT&ss=<?php echo $keyword; ?>" >

[code=php:0]

I also took the / out of your closing > (form is not a self closing tag)

 

let me know what you get with that

Link to comment
Share on other sites

right, so we can safely say that $keyword is not getting assigned properly.

 

sitck this code in just before the form and let me know what you get back, click your submit button a couple of times as well to see what happens for each run through.

echo '$keyword Assigned Value = <table border="1"><tr><th> '.$keyword.' </th></tr></table><br>';
echo '$_POST['keyword'] Assigned Value = <table border="1"><tr><th> '.$_POST['keyword'].' </th></tr></table><br>';

 

you will also need to change your action page to just default.aspx, so that you can see the results.

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.