Where to put functions to be called into a form

Hi all,

Still very new to this and trying to find my feet. I received a comment the last time I posted that I should be looking into PDO, which I can now say that I have been looking into.

The tutorial that I followed was very basic (good for me) and had me put most of the code in the index.php page.

My question is, if I am going to add this code to a button on a form, where should I put this bit of php.

Should it be stored in another php document in order to keep the workflow tidy and if so how do I go about that in relation to calling it into the document when the user hits the submit button.

My only other experience with ā€˜coding’ was in Visual Basic where you would put this sort of information into a module that could be called when required.

The code that I have got currently on my index page is as follows.

<?php 
include 'connection.php';
 
try {
 
    $conec = new Connection();
    $con   = $conec->Open();
 
    $sql = "INSERT INTO `user`(`name`, `email`) VALUES (:name,:email)";
    $pre  = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
 
    if($pre->execute(array(':name' => 'adam', ':email' => 'email@email.com'))){
        echo "Successful";
    }
} catch (PDOException $ex) {
    echo $ex->getMessage();
}
 ?>

I am aware that I won’t be leaving the actual information that I intend to go into my database in this bit of code, it will be a field name.

Thanks for any help you guys have to offer.

Thanks Again

Adam

IMHO it isn’t so much about right or wrong ways, but more about personal preferences. Well, as long as you’re the only one that will ever be working with the code anyway.

I like to start with something like

<?php 
. 
?> 
<html> 
<head> 
<style> 
. 
</style> 
</head> 
<body> 
<script> 
. 
</script> 

That is, I find it easier with everything in one file while I’m making a lot of changes as opposed to switching back and forth between files.

But once things are ā€œdoneā€ (lol, if there is such a thing) I usually prefer to separate the code into other files as much as is easily possible and seems to make sense to me to do so.

A key, and something I like to think I have gotten better at over the years, is to be consistent, have good naming and leave comments when things might later become vague.

You look to have a handle on the naming, that’s good.

One thing I would suggest is to not hesitate putting code inside functions. eg.
function insert_name_email($name, $email) {
Instead of including code inside of conditional structures. Then you can do something like

If ($_SERVER['REQUEST_METHOD'] === 'POST') { 
insert_name_email($validated_name, $validated_email); 
} 

Whether or not you want to ā€œmodularizeā€ your code is up to you. It is possible to have everything in one big file, I cut my teeth on Basic before it had a ā€œVā€ and I still tend to have a procedural mindset. This may be the reason I think of separating code as more for ease of reuse than I do for improved maintainility and have a kind of hybrid procedural-OOP way of file structure

1 Like

Thanks so much for this, really useful.

I am quite keen to learn about calling from other files as I feel it will be something that I will end up doing as its the way I am ā€œused toā€ from other languages and something that I would like to keep in this language, providing it is not a terrible idea.

If I was to move this to a functions.php file for example, how would I call it/run it from a button on a form.

Thanks again for your input Mitt, really appreciated.

Thanks

Adam

What I generally like to do with my setups is, I split parts of the page. If all pages contain the same exact navbar and footer, I would create a separate file for those. Then include these files in all the pages so that I don’t have to later edit every single page just to edit the footer or the navbar.

It is a lot easier for me to also have an editor that remembers opened files, this helps me find my way through files that I am currently working on. If a text editor doesn’t have this function, I’d normally switch over.

Yes, I agree with you there.

I currently use sublime on the Mac which I think is great.

I have a header and footer that I ā€œrequireā€ in my pages (not included in the example above)

Thanks

Adam

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.