Forums

Issues with Form - POST / Custom Code inside Module

marcodeluca 29 Jul, 2012
I have a simple form that's running some custom PHP to save some fields in my DB. Now, the form is working just fine when I load it as a page. But when I load it into a module, I am getting some strange behaviour.

I'm using the form to add categories to K2. The form has a bunch of fields, most are hidden, the only two textboxes are for the category name and description. The PHP script first checks if the name exists. If it does, it returns a message "sorry, this category already exists".

This works fine in both page and module instance of the form. Now the funny part:

If the category does not exist, it is added to the DB and a success message is returned. This works in the page instance but it fails in the module instance of the form. I'll paste the code below... I've got debug on maximum and I'm not even getting errors. Firebug's console doesn't show any issues either. I even added some echo statements AFTER the lines of code that fail, and all of the values I'm collecting are echoed just fine.

From what I can tell, the module is not evaluating 3 lines of the PHP script. Here's the code:

 $checkQuery = "SELECT * FROM db_k2_categories WHERE name='$name'";
  $resultCheck = mysql_query($checkQuery);
  if( mysql_num_rows($resultCheck) ) echo "<span class='alreadyExist'>Sorry $name already exists!</span>";
  
else  {
	$resultInsert = mysql_query("INSERT INTO db_k2_categories( id, parent, published, access, name, alias, description, params) VALUES('$id', '$parent', '$published', '$access', '$name', '$alias2', '$description', '$params')");
	if($resultInsert) echo "New record saved successfully";

}


Any thoughts as to why the script works on the full page, but fails to run "resultInsert" when published as a module? I realize there's a lot of points of failure here, but I thought I'd just throw it out there to see if anyone had some ideas... even of just how to troubleshoot.
GreyHead 29 Jul, 2012
Hi marcodeluca,

Where so you have this code and where do the values come from? I suspect that you may be picking up an article id from the page URL rather than the category id from your form but it's impossible to say from the information here.

Bob
marcodeluca 29 Jul, 2012
You're on the right track, it has something to do with this page as the module works on other pages. I can't find any duplicate IDs, Im just going over and over the code until I can figure it out.

The form code was built using the chronoforms wizard, and then I added some inline JS and edited some IDs of the textboxes. The PHP code that connects to MSQL and makes the queries is in the custom PHP code of the chronoforms wizard.

The page is K2 Categories page, so I guess it has something to do with that but I can't tell how yet. Anyway, I'll keep searching until I figure it out.
GreyHead 29 Jul, 2012
Hi marcodeluca,

Please post a link to the form so I can take a quick look.

Bob
marcodeluca 29 Jul, 2012
Okay I think I'm getting close to the problem now... the form is somehow picking up some data from something on the page. If you go into one of the K2 categories and then fill out the form, the PHP script is echoing all the values it's getting from the form, and I see this:

ID= 11:ottawa 


So for some reason, it looks like the names from some of the K2 categories on the page are getting inserted into the form fields.
marcodeluca 29 Jul, 2012
And its solved. Thanks for your guidance earlier:)
GreyHead 30 Jul, 2012
Hi marcodeluca,

Well done, just out of curiosity what was the problem?

Bob
marcodeluca 30 Jul, 2012
The problem was that I had a hidden field in the form passing the name="id" value="null" variable for the K2 category being created. The form gets posted to a php script that had a php variable $categoryId... and I don't actually know how or why, but the name of a K2 category was getting inserted into that PHP variable and breaking the script.

MySQL auto-increments the ID columns anyway so I just removed the ID statements and everything worked fine. Sorry, I shoulda posted the solution. So brutal when you find your problem on a forum and follow it through to the last post where someone says "I solved it" and thats it.
GreyHead 30 Jul, 2012
Hi Marcodeluca,

Thanks for posting that.

I suspect this may be to do with a little technical bug in ChronoForms where it adds both the GET variables from the URL and the POST variables from the form into the $form->data array. It does the POST variables first and sometimes they can then be be over-written by a GET variable. If the republisher is on the resulting values will be set in the form. This may be what is happening to your hidden input when the form loads.

Bob
This topic is locked and no more replies can be posted.