Page 1 of 1

Display web page in php on click of submit Button

Posted: Wed Sep 26, 2007 5:59 am
by TechnoAtif
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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 honoured.
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
-----------------------------

Code: Select all

<html>
<title>Product CMS: Search Products</title>
<body>
<h1>Manage Products</h1>

<?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%'";
	}

?>
<table>
<tr> <th>Product url</th></tr>


<?php

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

	if (!$url) 
	{
	echo '</table>';
	exit('<p>Error retrieving urls from database!<br />'.
	'Error: ' . mysql_error() . '</p>');
	}

	while ($pdurl = mysql_fetch_array($url)) 
	{
	echo "<tr valign='top'>\n";
	$id = $pdurl['pcode'];
	$purl = htmlspecialchars($pdurl['purl']);
	//echo "<td>$id</td>\n";
	echo "<td> <a href='link:$purl'>$purl</a>";
	echo "</tr>\n";
	}
?></table>
<p><a href="productsearch.html">New search</a></p>

</body>

</html>
----------------------------------------------------------------------------------------
Productsearch.html
-----------------------------

Code: Select all

<html>

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

<body>

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

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


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

</label><br />

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

</form></body></html>
----------------------------------------------------------------------------------------

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


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Sep 26, 2007 6:10 am
by N1gel
You could use this code to divert you to a page once a url has succesfully been found. Oviously just replace google with your url variable

Code: Select all

header('Location: http://www.google.co.uk');

Posted: Wed Sep 26, 2007 6:15 am
by lnt
After query from database, do not output anything, use header to redirect

Code: Select all

header("Location: $pdurl['purl']");

Posted: Thu Sep 27, 2007 1:12 am
by TechnoAtif
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:

Code: Select all

<?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']");//This is the error line
		
	}

?>


Plz help If u can.

Posted: Thu Sep 27, 2007 2:20 am
by Hemlata
Hello,

Modify the line 43 of your code FROM

Code: Select all

header("Location:$pdurl['purl']");
TO

Code: Select all

header("Location:".$pdurl['purl']);
to get the script working. Hope this might solve your issue.

Regards,

Posted: Thu Sep 27, 2007 5:53 am
by anchises
Trouble with header() is that you have to be careful about how you use it. If any output has already been sent (including an error message, or even a single element of white space before the opening php tag) it can

I often jump out of php and use a <script> instead:

Code: Select all

... php?><script>document.location.replace(<?echo $thisurl?>)</script><?php ...
I know there are tehcnical issues with this which make it non-perfect in some people's eyes, but it does work and it's often quicker to slap this into a script than try to work out what output has already been sent and why