472,961 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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
15 6468
nathj
938 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
938 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
938 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
938 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
938 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
938 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
938 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
938 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

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

Similar topics

13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
0
by: dominosly | last post by:
Okay, this is a rather complicated problem, so here is the short of it: I am using a custom designed user control that simply writes out a plain old html submit button to the page. When the...
5
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.net / VB page with a Script (submitValues) and a Form. 1. The form has 20 input texts and a "Submit" button. 2. The script declares 20 variables. The value of each...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
4
by: James | last post by:
Hello, I have a RequiredFieldValidator for several textbox controls on a form. Here's an example with the RequiredFieldValidator. EnableClientScript, Enabled, and Visible are set to true for...
3
by: remya1000 | last post by:
i'm using ASP with MSAccess as database. i have two buttons and two textbox in my page. when i press my first button (First month) i need to display the current month in one textbox and last one...
2
by: wreed06 | last post by:
Hello, I have 2 problems. In my webpage, I have a dropdown list with a button that takes the user to a popup window specific to the option. I am using Firefox 2.0.0.13. I have successfully...
5
by: wreed06 | last post by:
I have 2 problems. In my webpage, I have a dropdown list with a button that takes the user to a popup window specific to the option. I am using Firefox 2.0.0.13. I have successfully validated my HTML...
1
by: ponna | last post by:
u click submit button then display one input box.and then u click another submit button then display the value in previous box.now u click first submit button more than one time dislay more...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.