views:

107

answers:

3

I have a advertisement website with alot of records stored in a mysql db.

On my main page, I have a form where users may specify their search criteria. Action is set to myPhp.php file.

myPhp.php contains NO HTML, only php code, and it is basically something like this:

1- get values from FORM and build a SQL query
2- query MYSQL db. 
3-display all results in a table using
`while($row=mysql_fetch_array($res))`
4- echo the table.

In this table which is created in PHP, I have several links also, and whenever they are clicked, a javascript function on the parent page is called, which sets a hidden input value to something, and then submits the form again with the chosen variable.

Ex: Whenever users want to go to the next page in the search results, they have to click on a 'next' link created in PHP, and then the javascript gets called, which sets a hidden input value to 'next', and then the form is submitted again, and PHP file GETS the variable from the hidden input and detects that its value is set to 'NEXT' and then displays the next results.

Is there really not another way to do all this ? (that is, a better way when it comes to performance and reliability)

Im still learning so I am very thankful for your help! I will update this Question whenever you need more input.

Thanks

+1  A: 

Replace the next javascript call with a link to

http://yourdomain/myPhp.php?page=2

Then check the get parameter in myPhp.php:

if(isset($_GET['page']) AND is_numeric($_GET['page'])){
    $limit = (int) $_GET['page'] * MAX_RECORDS . ', ' . MAX_RECORDS;
} else {
    $limit = MAX_RECORDS;
}

//...
$query .= 'LIMIT '. $limit;

Of course you have to change it as your constants change and such.
This method called paginating. Take a look at the Zend Framework's Paginator module to the better understanding.

erenon
Ok, but if I do send the variable like you mention above to the PHP file again, wont the other variables which was submitted by the form dissappear? (like querystring, prices, and other user inputs)
pesar
Foa: Don't send query strings. 2: If you don't post your data you couldn't get them from the POST superglobal.
erenon
Please comment after downvote.
erenon
A: 

If these links have at least one same class across board (with a title attribute) you can attach to them to can attach all these links to a single click event and test to see what title is on the element clicked.

With that you can use a switch to trigger the right function to perform the task (I suppose using JQuery/Ajax).

This should work. At least no hidden fields.

Example:

<a href="myPhp.php?whatever you querystring is" title="NEXT">Next</a>

<a href="myPhp.php?whatever you querystring is" title="AdDetails">Ad Details</a>
Colour Blend
+1  A: 

When creating the link you can use a querystring to specify the parameter that you would like to send to the server rather than submitting a form. So if you have a hidden input in a form called next then you can accompish the same thing like this:

<a href="myPhp.php?next=NEXT">Next</a>

On the myPhp.php page you can get the value of next in a similar way as when you send a form.

Vincent Ramdhanie
Ok, but if I do send the variable like you mention above to the PHP file again, wont the other variables which was submitted by the form dissappear? (like querystring, prices, and other user inputs)
pesar
Vincent Ramdhanie