467,188 Members | 1,357 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,188 developers. It's quick & easy.

Display web page in php on click of submit Button

Hi to all and sundry. I'm Atif a newbie to this forum

I've a question regarding php. if anyone could answer it i would be hononured.
thanx in advance.
---------------------------------------------------------------

I have a table in MYSQL named product.
Columns in product are named as pcode(product code ) and purl (product url).

i have to generate a search code such that,

if i enter the pcode in the search text for the corresponding purl,

the webpage corresponding to that url gets displayed as i click the submit
button.
I tried out my best ,in the end i was able to display the url only and not the web page corresponding to that url.

here is the code i tried:.
-------------------------------------------------------------------------
Productmanage.php
-----------------------------
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>Product CMS: Search Products</title>
  3. <body>
  4. <h1>Manage Products</h1>
  5.  
  6. <?php
  7.  
  8. include "dbconnect.php";
  9.  
  10. // The basic SELECT statement
  11. $select = 'SELECT DISTINCT pcode,purl ';
  12. $from = ' FROM product';
  13. $where = ' WHERE 1=1 ';
  14. $searchtext = $_POST['searchtext'];
  15.  
  16.     if ($searchtext != '') 
  17.     { // Some search text was specified
  18.     $where .= " AND pcode  LIKE '%$searchtext%'";
  19.     }
  20.  
  21. ?>
  22. <table>
  23. <tr> <th>Product url</th></tr>
  24.  
  25.  
  26. <?php
  27.  
  28. $url = @mysql_query($select . $from . $where);
  29.  
  30.     if (!$url) 
  31.     {
  32.     echo '</table>';
  33.     exit('<p>Error retrieving urls from database!<br />'.
  34.     'Error: ' . mysql_error() . '</p>');
  35.     }
  36.  
  37.     while ($pdurl = mysql_fetch_array($url)) 
  38.     {
  39.     echo "<tr valign='top'>\n";
  40.     $id = $pdurl['pcode'];
  41.     $purl = htmlspecialchars($pdurl['purl']);
  42.     //echo "<td>$id</td>\n";
  43.     echo "<td> <a href='link:$purl'>$purl</a>";
  44.     echo "</tr>\n";
  45.     }
  46. ?>
  47. </table>
  48. <p><a href="productsearch.html">New search</a></p>
  49.  
  50. </body>
  51.  
  52. </html>
  53.  
----------------------------------------------------------------------------------------
Productsearch.html
-----------------------------
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <title>Joke CMS:URL Search </title>
  4.  
  5. <body>
  6.  
  7. <h3>Search Product URLs from Product Code</h3>
  8.  
  9. <form action= "productmanage.php" method="post">
  10.  
  11.  
  12. <label>Containing ProductCode: <input type="text" name="searchtext" />
  13.  
  14. </label><br />
  15.  
  16. <input type="submit" value="Search" />
  17.  
  18. </form>
  19.  
----------------------------------------------------------------------------------------

Please help me to sort out this problem as eary as possible, i have to report to boss to the earnest regarding this work.

Thanx.........yours .........Atif
Sep 25 '07 #1
  • viewed: 6209
Share:
15 Replies
nathj
Expert 512MB
Hi TechnoAtif,

I think you are heading along the right lines, though next time please use the code tags to display your code - it makes it easier to read.

Back to the problem.

I think what you need to do is set the header once you have the location. The process would be:
1) A page with the form on for the user to enter to product code and then click search.
2) The seacrh (submit) button would go to a php page that queries the database based on the $_POST[] data
3) If the record is foudn the set the header
[php]
header("Location:$URL"); // where $URL is a variable containing the FULL url required
[/php]
4) If the record is not found then you need to display some text instead that indicates this problem - possibly returning the user to the search form.

I think the code you have is pretty close it's just a case of the missing step and ensuring the logic is right. I'm sure there are many ways to solve this problem and this is just the way I would tackle it.

Cheers
nathj
Sep 25 '07 #2
hi nathj and thanx for such immdiate reply.
your statement upto step 2 is very much clear to me.
How ever in the step 3 u r saying to include a header :

[PHP]header("Location:$URL");[/PHP] // where $URL is a variable containing the FULL url required.

can u plz tell me where to include this tag. i mean the exact location in the productmanage.php script.

Thanx
Sep 26 '07 #3
nathj
Expert 512MB
hi nathj and thanx for such immdiate reply.
your statement upto step 2 is very much clear to me.
How ever in the step 3 u r saying to include a header :

[PHP]header("Location:$URL");[/PHP] // where $URL is a variable containing the FULL url required.

can u plz tell me where to include this tag. i mean the exact location in the productmanage.php script.

Thanx
Sure thing, the plan is to code the line setting the header immediatley after you have found the URL for the product the user is searching for.

So in the code for the form you would set the action to search.php which would be structured as follows
[php]
// NOTE: this assumes you are using POST as the method on the form

// test the user entered something and search for it (this is your original code)
include "dbconnect.php";

// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM product';
$where = ' WHERE 1=1 ';
$searchtext = $_POST['searchtext'];

if ($searchtext != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtext%'";
}

// execute the query
$url = mysql_query($select . $from . $where)

// test the result
if ($url)
{
// retreive the url from the result and set the variable $lcURL
// redirect using the header
header("Location:$lcURL") ;
}
else
{
echo '<p> no product found</p>' ;

[/php]

This is only really psuedo code but it should explain a bit clearer what I was sugessting.

Have a go with this and if you get stuck post back with as much information as possible and the full code set again and I'll take another look for you.

Cheers
nathj
Sep 26 '07 #4
hi there . As per your able guidelines i 've edited a little bit of my code. But its now givinh the follwing error:
Parse error: parse error, unexpected T_IF in C:\Apache2\WWW\localhost\search1.php on line 24
Here is the code:



// NOTE: this assumes you are using POST as the method on the form

// test the user entered something and search for it (this is your original code)

<?php
include "dbconnect.php";

// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM product';
$where = ' WHERE 1=1 ';
$searchtext = $_POST['searchtext'];

if ($searchtext != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtext%'";
}

// execute the query
$url = mysql_query($select . $from . $where)

// test the result

if ($url)
{
while ($lcURL = mysql_fetch_array($url))
{

$id = $lcURL['pcode'];
$purl = htmlspecialchars($lcURL['purl']);
echo "<td> <a href='hyperlink:$lcURL'>$lcURL</a>";

}

header("http://localhost:8080/home.html:$URL") ;
}
else
{
echo '<p> no product found</p>' ;
}
?>



And sorry bcoz its too time consuming to enter all the codes within tags.
Sep 26 '07 #5
nathj
Expert 512MB
Hi,

It's even more time consuming to read code that is not formatted by the code tags so do please use them. Other people will beless polite and to continually and intentionally not use them is against the rules of the forum

As for the problem you get, I don't know which line is line 24 as there are no code tags and so no line numbers.

I did notice that you use the header() wrong. It needs to be header("Location:$variable)

The word Location is needed that's how the header system knows what to do. If you are using an IDE there may be syntax checker built in that may help you. Alternativley post back using the code tags for php and I'll take another look

Cheers
nathj
Sep 26 '07 #6
Sorry for the convenience caused . and if it is so. then i have no qualms in sending the tagged code.it was just due to lack of knowledge of doing so. that's why i send it in crude form. never mind. here is the updated code.

Parse error: parse error, unexpected T_IF in C:\Apache2\WWW\localhost\search1.php on line 20
[PHP]<?php
include "dbconnect.php";

// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM product';
$where = ' WHERE 1=1 ';
$searchtext = $_POST['searchtext'];

if ($searchtext != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtext%'";
}

// execute the query
$url = mysql_query($select . $from . $where)

// test the result

if ($url)
{
while ($lcURL = mysql_fetch_array($url))
{

$id = $lcURL['pcode'];
$purl = htmlspecialchars($lcURL['purl']);
echo "<td> <a href='hyperlink:$lcURL'>$lcURL</a>";

}

header("http://localhost:8080/home.html:$URL") ;
}
else
{
echo '<p> no product found</p>' ;
}
?>[/PHP]
.
Sep 26 '07 #7
nathj
Expert 512MB
Hi ,

It would seem that the following line is the problem:
[php]
if ($url)
[/php]
I'm not sure what the trouble with is but lets try something different to test the result:
[php]
if (!$url)
{
echo'<p> no match </p>'
}
else
{
// redirect to the URL for the product
}
[/php]
This is just inverting the logic which should work. Give it a try and let me know, if this fails we may have a problem with the SQL and what, if anything, is being returned.

Cheers
nathj
Sep 26 '07 #8
Hi there! I've corrected previous errors and they are gone now .But here is a new
trouble. IT's Saying :


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache2\WWW\localhost\search1.php on line 43
The updataed code is as follows:

[PHP]

<?php


include "dbconnect.php";

// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM product';
$where = ' WHERE 1=1 ';
$searchtext = $_POST['searchtext'];

if ($searchtext != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtext%'";
}
?>
// execute the query

<?php

$url = mysql_query($select . $from . $where);

// test the result

if (!$url)

{

exit('<p>Error retrieving urls from database!<br />'.
'Error: ' . mysql_error() . '</p>');

echo '<p> no product found</p>' ;
}



while ($pdurl = mysql_fetch_array($url))

{

$id = $pdurl['pcode'];
$purl = htmlspecialchars($pdurl['purl']);
header("Location:$pdurl['purl']");

}

?>




[/PHP]

Plz help If u can.
Sep 27 '07 #9
nathj
Expert 512MB
[PHP]

$id = $pdurl['pcode'];
$purl = htmlspecialchars($pdurl['purl']);
header("Location:$pdurl['purl']");

?>


[/PHP]
Hi,

Change the header line shown above to:
[php]
header("Location:$purl");
[/php]
I think that should solve the problem.

If you get any errors about setting headers after they have already been set work through the code and remove blank lines as they count as output to the browser which sets the headers. That's more of a tip just in case really.

Cheers
nathj
Sep 27 '07 #10
Hi Nathj. You help seems to be really workng now. Thanks for that. I've managed to run the cose but now:
As i run the code it says:

Notice: Undefined index: searchtext in C:\Apache2\WWW\localhost\search1.php on line 7
and when i refresh the page, it displays:

Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Seems as if i'm so near yet so far.Here goes the code.

[PHP]<?php
include "dbconnect.php";
// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM product';
$where = ' WHERE 1=1 ';
$searchtxt = $_POST['searchtext'];
if ($searchtxt != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtxt%'";
}
?>
// execute the query
<?php
$url = mysql_query($select . $from . $where);
// test the result
if (!$url)
{
exit('<p>Error retrieving urls from database!<br />'.
'Error: ' . mysql_error() . '</p>');
echo '<p> no product found</p>' ;
}
while ($lcurl = mysql_fetch_array($url))
{
$id = $lcurl['pcode'];
$pdurl = htmlspecialchars($lcurl['purl']);
header("Location:$pdurl");
}
?>[/PHP]


I think the problem area is this part of the code:

[PHP]$searchtxt = $_POST['searchtext'];
if ($searchtxt != '')
{ // Some search text was specified
$where .= " AND pcode LIKE '%$searchtxt%'";
}[/PHP]

Any suggestions?
Sep 27 '07 #11
nathj
Expert 512MB
Hi,


I'm glad we are making progress.

The issue with the SQL I think will be over come by the following change:
[php]
if($searchtext != "")
{
$searchtext = mysql_real_escape_string($searchtext) ;
$where .= " AND pcode = '$searchtext'";
}
[/php]

As for the second problem, the refresh, it may be that the data required to run the SQL is not present and so the return is wrong and the URL it tries to load is invalid or doesn't exist. If that's the case then we need to tighten up the exception handling on this page and ensure that appropriate messages are set.

Cheers
nathj
Sep 27 '07 #12
yes i think u r right. We have the problem in enetering the url.

what i did is. i entered product code and product url manually through mysql .at mysql command prompt. the urls were like www.yahoo.com,www.rediffmail.com. ets.. and then i'm trying to open these web pages through that code using the url string.I guess here is something wrong with my procedure that's why it prompts : The requested URL was not found on this server. There is nothing wrong with this code. am i right?
Sep 27 '07 #13
nathj
Expert 512MB
yes i think u r right. We have the problem in enetering the url.

what i did is. i entered product code and product url manually through mysql .at mysql command prompt. the urls were like www.yahoo.com,www.rediffmail.com. ets.. and then i'm trying to open these web pages through that code using the url string.I guess here is something wrong with my procedure that's why it prompts : The requested URL was not found on this server. There is nothing wrong with this code. am i right?
I think the code you have should work just fine. Try runnning it with some pages on your own web-server and see what happens. Just create a dimple dummy html page and use that instead. This should give you a more accurate impression of the end result.

Cheers
nathj
Sep 27 '07 #14
Hi nathj. Its great!!! Realy great. Your timely help and guidance helped me to make an impresiion on my boss. I Finally achieved my target. Many Many thanx to you. i will bother you with oads of queries in future as well.

The final code is as follows:

Product Search.html
[HTML]<html>

<title>Joke CMS:URL Search </title>

<body>

<h3>Search Product URLs from Product Code</h3>

<form action= "search1.php" method="post">


<label>Containing ProductCode: <input type="text" name="searchtext" />

</label><br />

<input type="submit" value="Search" />

</form>



</body>

</html>[/HTML]

search1.php
[PHP]<html>
<title>CMS:Search Product</title>
<body>
<?php
include "dbconnect.php";
// The basic SELECT statement
$select = 'SELECT DISTINCT pcode,purl ';
$from = ' FROM products';
$where = ' WHERE 1=1 ';
$searchtext = $_POST['searchtext'];

if($searchtext != "")
{
$searchtext = mysql_real_escape_string($searchtext) ;
$where .= " AND pcode = '$searchtext'";
}

?>
// execute the query
<?php
$url = mysql_query($select . $from . $where);
// test the result
if (!$url)
{
exit('<p>Error retrieving urls from database!<br />'.
'Error: ' . mysql_error() . '</p>');
echo '<p> no product found</p>' ;
}


while ($lcurl = mysql_fetch_array($url))
{
$id = $lcurl['pcode'];
$pdurl = htmlspecialchars($lcurl['purl']);
header("Location:$pdurl");
}
?>



</body>
</html>[/PHP]
Sep 27 '07 #15
nathj
Expert 512MB
Hi,


I'm glad to have helped. If you do have any further queries then post here at TSDN.

I looked over your code and one suggestion, that has no bearing on function just on code development. Have a look at using CSS, there's loads of tutorial and you'll be able to move all your style definitions into one file. It makes the HTML easier to read when you look at it in the future and it makes it easier to change the style of web page.

Check out this tutorial to get you started.

Cheers
nathj

Ps that's just some advice and has no bearing on your current project and how it works.
Also, sorry iof you do use CSS, I'm not trying to be patronizing, I just like to try and help.
Sep 27 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by dominosly | last post: by
5 posts views Thread by Miguel Dias Moura | last post: by
2 posts views Thread by Benedict Teoh | last post: by
4 posts views Thread by James | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.