Jump to content

Module Won't Save Data (using external DB)


Recommended Posts

I am creating (or at least trying) a module to collect some data from a form in the admin that saves that data into a table/database that is not the PW database. Data has to be inserted into a table that is shared among several sites. I have successfully added a menu item, created and displayed the form in the admin, but when I click save, nothing happens, it just returns to the blank form without saving the data. I am new to this and probably missing something obvious, can you help?!

<?php

class AddSpecialPropertyListing extends Process {

	public function init() {
		parent::init(); // always remember to call the parent init
	}

	/** Executed when root url for module is accessed */
	public function ___execute() {
		$form = $this->buildForm();
		if($this->input->post->submit) {
			$result = $this->processForm($form);
			return $result;
		} else {
			return $form->render();
		}
	}

	/** Build the "Listing Input" form */
	protected function buildForm() {
		$form = $this->modules->get("InputfieldForm");
		$form->method = 'post';

		//Add Property Street Address
		$f = $this->modules->get("InputfieldText");
		$f->label = 'Property Address';
		$f->attr('name','prop_add');
		$f->columnWidth = 40;
		$f->requiredLabel = '*';
		$f->required = true;
		$f->requiredAttr = true;
		$f->placeholder = 'Example: 123 Main Street';
		$form->add($f);

		//Add Lot Size
		$f = $this->modules->get("InputfieldText");
		$f->description = "Input numbers only (ie 1234, not 134 acres). Enter as number of acres.";
		$f->label = 'Lot Size';
		$f->attr('name','prop_acres');
		$f->columnWidth = 25;
		$f->type = number;
		$f->placeholder = 'Example: 1234';
		$f->required = false;
		$form->add($f);

		$f = $this->modules->get('InputfieldSubmit'); 
		$f->attr('name', 'submit_save'); 
		$f->attr('value', $this->_('Save')); 
		$f->addClass('head_button_clone');
		$f->icon = 'save';

		$form->add($f);

		return $form;
	}

	/** Process the form **/
	protected function processForm(InputfieldForm $form) {

		// Connection details
		$dbhost = 'localhost';
		$dbuser = 'xxx';
		$dbpass = 'xxx';
		$db = 'prop_data';
		$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);

		// Check connection
		if($conn === false){
			die("ERROR: Could not connect. " . mysqli_connect_error());
		} else {
			die('success');
		}

		// Attempt insert query execution
		$sql = 'INSERT INTO listings_res (num_acres, st_address) VALUES (';
		$sql .= '"'.mysqli_real_escape_string($conn, $_POST['prop_acres']).'",';
		$sql .= '"'.mysqli_real_escape_string($conn, $_POST['prop_add']).'"';
		$sql .= ');';
		
		echo $sql; exit();

		if(mysqli_query($conn, $sql)){
			$success = "Records added successfully.";
		} else{
			$success = "ERROR: Could not able to execute $sql. " . mysqli_error($link);
		}

		// Close connection
		mysqli_close($conn);

  		return $success;
	}

}

This is my module.php file. I am running in PW 3.0.123.

Link to comment
Share on other sites

	/** Executed when root url for module is accessed */
	public function ___execute() {
		$form = $this->buildForm();
		if($this->input->post->submit) { <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			$result = $this->processForm($form);
			return $result;
		} else {
			return $form->render();
		}
	}

 

Link to comment
Share on other sites

Beautiful!! That was it exactly. Still have some other logic/syntax problems to work out, but they are of the php/mysql variety and I can (hopefully) handle that part! 🙂

Now the big question, can you tell me or point in the direction of WHY that code change you suggested was the solution? What I had before is working in another module I did last year (far simpler and writing to a table within the PW database).

Link to comment
Share on other sites

2 hours ago, SoccerGuy3 said:

Beautiful!! That was it exactly. Still have some other logic/syntax problems to work out, but they are of the php/mysql variety and I can (hopefully) handle that part! 🙂

Now the big question, can you tell me or point in the direction of WHY that code change you suggested was the solution? What I had before is working in another module I did last year (far simpler and writing to a table within the PW database).

@SoccerGuy3 Glad that it helped. 

The name of your button is not 'submit', but 'submit_name', but in your initial code you check '$this->input->post->submit'. 

$f->attr('name', 'submit_save');

Also if you want to make your code more PWish you can rewrite it with https://processwire.com/api/ref/wire-database-p-d-o/

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...