How to create a forums post via submit button on HTML & PHP

I have a site that is currently running HTML and Have a info page for users to fill out after this I would like for the submit button to create a new post on our forums with the info they have provided any idea how to do this

Hi Angelsking

OK several things here - first assume the info your users to fill out is in a form then you have to submit the form, and process the data. If your site is using jQuery then you can do that via an AJAX call something like:

create a ‘submit’ function a bit like this:

jQuery(document).on('submit','form',function(e){
e.preventDefault();
e.stopPropagation();

Declare a data object

var data = {}

Then for each field you want to submit get it’s value and add to the data object

data['database-column-name'] = jQuery('#field_id').val()

jQuery.ajax({
method:‘POST’,
url:‘path/to_your/processing_script.php’,
data:data
}).done(function(msg){

Get a message (msg) on success or NO message (msg) if failure so you can do something :slight_smile:

})

Close the submit function

})

The you have to create a php processing page - simple example:

<?php
Connect to your database

  define('DB_NAME','THE DATABASE NAME');
  define('DB_USER','THE DATABASE USERNAME');
  define('DB_PASSWORD','THE DATABASE PASSWORD');
  define('DB_HOST','THE DATABASE HOST'); - usually localhost

// CREATE PDO OPTIONS FOR CONNECTION STRING //

$PDOoptions = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );

try{

 $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, $PDOoptions);

// THIS IS ASSUMING 3 FIELDS ARE POSTED
$statement = $dbh->prepare("INSERT INTO tablename (list,of,fields) VALUES(?,?,?)");
if($statement->execute(array( $_POST['field1'],$_POST['field2'],$_POST['field3'] ) ) == true) {
// THIS IS THE SUCCESS MESSAGE
echo $dbh->lastInsertId();
} else {
// THIS IS THE FAILURE MESSAGE - THERE ISN’'T ONE :slight_smile:
echo false;
}

} catch(PDOException $e ){
echo "Error: ".$e;
}

?>

The above gets the data from the form and submits it to your database.

HOWEVER DO NOT USE THIS CODE :slight_smile: AS THERE IS/ARE NO VALIDATION ELEMENTS AND IS ONLY AS A SIMPLE - VERY - GUIDE/IDEA

You should always validate and sanitize any data being sent to a database the above code is just to give you some idea how do do what you want and without knowing what the fields are and what content they SHOULD contain it is difficult to give you any accurate clues in that direction.

If you need any help - just shout :slight_smile: