PHP/JS Form/Query problem (probably simple one)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tryin
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:22 pm

PHP/JS Form/Query problem (probably simple one)

Post by Tryin »

I have limited knowledge in PHP, Javascript coding, but I was trying to solve this on my own, browsing forums and reading tutorials and examples. Unfortunately, my head is now full - probably too many information for a day. I hope somebody can help me with this.

I would like to pass variable value to another php file that runs a query. Query work perfectly alone, yet when I try to pass a form input I don't get the result I would like to. To make things more confusing, I'm using two separate php files in two different Joomla positions.

Here is the search form code:

Code: Select all

<html>
<head>
<script>
/*** Here is the form javascript code that builds a Calendar form
/*** Works perfectly
</script>
</head>
<body>
<form action="http://www.mysite.com/joomla/modules/mod_my_module/mod_my_module.php" method="post">
 
From:<script>DateInput('startdate', true, 'YYYY-MM-DD','2008-09-01')</script><br> /* This line calls a javascript function that builds the form. It will also create 
                                                                                                                  /*a hidden field using the designated name (in this case, "startdate") containing
                                                                                                                  /*the selected date's value.
 
<input type="button" onClick="return this.form.startdate.value" value="Potvrdi"> 
 
</form>
</body>
</html>
I tested the confirmation button with alert function (without form action) and it returns the desired date correctly.

And here is the mod_my_module.php code:

Code: Select all

<?php   
defined('_JEXEC') or die('Restricted access'); /* Joomla required line
 
mysql_connect('localhost','username','password');
mysql_select_db('my_database');   
$sd = $_POST[this.form.startdate.value];
if(isset($sd)){
$query = "SELECT * FROM My_Table" WHERE Date > $sd;   
$result = mysql_query($query) or die("Query ($query) failed!");
$fields = mysql_num_fields($result);
 
echo "<table border=1>\n<tr>";
for ($i=0; $i< mysql_num_fields($result); $i++)
{
print "<th style=color:red>".mysql_field_name($result,$i)."</th>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
for ($f=0; $f < $fields; $f++)
{
echo "<td style=white-space:nowrap; align=center>$row[$f]</td>";
} 
echo "</tr>\n";}
echo "</table>";
}
 
?>
I'm guessing this php file is the problem, but can't figure it out on my own. Any assistance would be much appreciated.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: PHP/JS Form/Query problem (probably simple one)

Post by spider.nick »

Code: Select all

$sd = $_POST[this.form.startdate.value];
Should be:

Code: Select all

$sd = $_POST['startdate']
And, if you want your code to secure:

Code: Select all

$sd = mysql_real_escape_string($_POST['startdate']);
Nick
Tryin
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:22 pm

Re: PHP/JS Form/Query problem (probably simple one)

Post by Tryin »

Thanks for your quick reply Nick, I changed the line as you suggested.

I also changed query line to correct syntax, with WHERE clause properly under quotes:

Code: Select all

$query = "SELECT * FROM My_Table WHERE Date > '$sd'"
The difference after the changes is that I see complete table from query even before submiting the desired date from form ($sd isset always?). After submiting - no changes (still full table). Do I need to reload mod_my_module.php in some way? Are dates comparable in a way I'm trying to compare them?
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: PHP/JS Form/Query problem (probably simple one)

Post by spider.nick »

I will let someone else reply about the date questions, as I would have to look it up. As for the full table issue, instead of:

Code: Select all

if( isset($var) )
Try:

Code: Select all

if( !empty($var) )
Nick
Tryin
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:22 pm

Re: PHP/JS Form/Query problem (probably simple one)

Post by Tryin »

Yes, I understand the difference between isset and empty functions. The problem still remains - clicking the button doesn't change a thing :(
Maybe I should use submit button, but I don't know how to pass the startdate value with submit button.
Thanks for your suggestions!
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: PHP/JS Form/Query problem (probably simple one)

Post by spider.nick »

Tryin wrote:Yes, I understand the difference between isset and empty functions. The problem still remains - clicking the button doesn't change a thing :(
Maybe I should use submit button, but I don't know how to pass the startdate value with submit button.
Thanks for your suggestions!
Take a look at Line 8 in your mod_my_module.php file. Post back if you do not see the problem.

Nick
Tryin
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:22 pm

Re: PHP/JS Form/Query problem (probably simple one)

Post by Tryin »

I corrected that line 8 to a proper syntax, as I've said above Nick. Query still doesn't get the $sd variable or doesn't use it.

I tried to pass the startdate value and refresh page with submit button:

Code: Select all

<input type="submit" onClick="return this.form.startdate.value" value="Confirm">
and got 500 Internal Server Error:

The server encountered an internal error or misconfiguration and was unable to complete your request.
...
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Tryin
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:22 pm

Re: PHP/JS Form/Query problem (probably simple one)

Post by Tryin »

I just realized that the problem is here:

Code: Select all

<form action="http://www.mysite.com/joomla/modules/mod_my_module/mod_my_module.php" method="post">
My_module.php handles only one joomla module, and when I did the previous submit experiment, instead of reloading the joomla page in which that php file handles one module (something like this: http://www.mysite.com/joomla/index.php? ... &Itemid=72), a new page tries to load: http://www.mysite.com/joomla/modules/mo ... module.php.

I hope someone can help me in further actions.
Post Reply