Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

on Click event in PHP

Status
Not open for further replies.

seek4need

Technical User
Aug 12, 2008
14
US
Hi,

I would like to include on click event in the below PHP code.

The below PHP code will retrieve products data from MYSQL database into a table. and I have a sub-products in the next page which is also retreive from database.

I need help when I click on product on page1 the same product as to pass as a parameter to the next page which will be input for the query for sub-products on page2.

Can you please help how to use on Click event in the below PHP code.


<?php
session_start ();

$con = mysql_connect('localhost','root','xxxxxx');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result_product = mysql_query("SELECT * FROM products where product_no between 133 and 171 order by product_no");
echo "<table border='0'>";
$lenproduct = mysql_num_rows($result_product);
$maxLen = 0;
echo "<tr>";
echo "<th align ='left'>" . 'PRODUCTS LIST' . "</th>";
echo "</tr>";

$productCount =0;
$row_product = mysql_fetch_array($result_product);
while( $productCount <= $lenproduct)
{

$row_product = mysql_fetch_array($result_product);
echo "<td>";
echo $row_product['HYPERLINK'] . $row_product['MAIN_PRODUCT'];
echo "</a></td> </tr>";
$productCount = $productCount + 1;

}
echo "</table>";

?>



Thanks a lot in Advance....!

Thanks,
Srinivas
 
OnClick is a Javascript event not PHP.

You can have the OnClick autosubmit the form and then Have PHP do its thing, but the OnClick side of it has nothing to do with PHP and PHP has no control over it.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
you are right onClick is a Javascript event and I am able to use onclick inside the PHP function and it worked but my issue is I have to get a value with the onclick event and that value need to pass to next page. How do I do that?


I have 10 products

product1
product2
product3
product4
.
.
.
product10

when I click the product4. The product4 value has to pass as a parameter to the page2 (next page). which is input for the sub-products in page2.

Can you please help. How to pass a parameter to next page with onclick event.


 
Again this is a Javascript thing not PHP.

You can echo anything you want with PHP including javascript functions but they don't interact.

The only way to get the value to the next page is to submit the form where the Drop Down is, and PHP can't do that. Javascript can:

So something like this should work:

Code:
<form name="[red]myform[/red]"...>
<select OnClick="document.[red]myform[/red].submit();"
...
</form>
</form>

This will submit the form, and the selected value will be available to PHP there, in the POST or GET variable depending on what method your form is using.

But once again this is a Javascript issue and should be posted in the JS forum here:
forum216



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Can we use PHP Sessions to pass the values to next page? as per the above requirement.
 
Javascript cannot interact with PHP, it cannot alter PHP's sessions variables, and it cannot directly send a value to PHP.

You could use the JS window.location to redirect to the new page OnChange instead of OnClick and tack on the value selected from the drop down.

Code:
<form name="myform"...>
<select OnChange="window.location='someotherpage.php?myvariable=' + this.value;"
...
</form>

It will be available in the PHP page from the GET variable.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top