-1

I have my $dbConnection variable set at the very top of the page. And I have a contact form included on the same page. The contact form works fine.

However, when submitting, it gives me an undefined error; that's weird because I'm 199% sure I have the variable set correctly.

The form is going through a script.

function ubbreplace($text){
    if (strpos($text, "[contact-form]") !== false) {
       ob_start();
       include("contactform.php");
       $replace = ob_get_contents();
       ob_end_clean();
       $text = str_replace("[contact-form]", $replace, $text);
    }
    return $text;
}

and my guess is that this script prevents the connection from connecting. Is that possible?

I have defined $dbConnection as a global, added these `` to the SQL, etc etc nothing worked. The error goes away while the $dbConnection is defined as a global but does not put the data in the database.

J. Doe
  • 503
  • 1
  • 6
  • 19
  • Where is `$dbConnection` defined as a global; and where is your `ubbreplace()` function telling PHP that it should access the global `$dbConnection`? – Mark Baker May 16 '16 at 21:45
  • @MarkBaker I had removed those earlier because I didn't think it would work. – J. Doe May 16 '16 at 21:46
  • If you don't have `global $dbconnection` in your function, then PHP doesn't recognise it as existing in the function, because it doesn't exist inside the function scope – Mark Baker May 16 '16 at 21:47
  • @Fred-ii- Derp, derp. Let's say it's still in pre alpha. I'll edit that, thanks. – J. Doe May 16 '16 at 21:49
  • that's because I take posted code literally ;-) – Funk Forty Niner May 16 '16 at 21:50
  • that global definition wouldn't happen to be inside `include("contactform.php");` by any chance, now would it? Yet you say *"I have my $dbConnection variable set at the very top of the page."* but that isn't posted in your question. – Funk Forty Niner May 16 '16 at 21:57
  • @Fred-ii- I have a db.php file at the top of my page, yes - with `$dbConnection` + db details defined there, I'm not totally sure what you mean – J. Doe May 16 '16 at 21:59
  • I suggest you go over your files again and the "logic" also which may stand to be key here. Check for errors again and use var_dump/echos placed here and there up until things start to break. Do this on a smaller scale and build up from there. It's probably something really silly (I didn't say "you" were silly here lol) ;-) that's the best advice I can offer. Wishing you well here ;-) *Cheers* – Funk Forty Niner May 16 '16 at 22:03
  • @Fred-ii- Sounds good, Fred. RIP my coffee machine. – J. Doe May 16 '16 at 22:06
  • @J.Doe Instant's going to have to do "for now" ;-) or "on-the-fly" espresso. – Funk Forty Niner May 16 '16 at 22:10
  • @Fred-ii- Hello Fred, it's me. I was wondering if after all these weeks you like to help me with this issue. I'm still trying to find out how to solve it. Thanks in advance. – J. Doe May 20 '16 at 17:26
  • @J.Doe Did you try the answer given below? Plus, can you update your question as to where it is defined as global? It's kind of hard to tell without seeing how everything's tied up together. It could also be an include that's not happening, hard to say really. You could also try placing that variable in the function itself, if global doesn't work. – Funk Forty Niner May 20 '16 at 17:31
  • @Fred-ii- I have tried placing the variable in the `include` itself, and it's submitting without error, but it doesn't add to the database. Same goes for the `global`, there's no error, there's a success message but there's no database row being added. I will update the question with all the associated files. – J. Doe May 20 '16 at 17:34
  • @J.Doe Then I'd replace the `$contact->execute();` with `if(!$contact->execute()){trigger_error("There was an error.... ".$dbConnection->error, E_USER_WARNING);}` to see if there are errors, or something about a constraint somewhere or some other type of error. – Funk Forty Niner May 20 '16 at 17:37
  • @Fred-ii- I've tried that but then I'm getting the undefined error and it surely is defined at the top of the page: Notice: Undefined variable: dbConnection on line 18 -- Also the text "There was an error...." does not appear. – J. Doe May 20 '16 at 17:44
  • @J.Doe I can't find the problem. I suggest you start by removing that function for `ubbreplace()` it could also be called only when you're "calling" that function when another file is relying on connecting to db first. Place the codes you have in your included files inside the same file. One by one, start adding the includes and once something breaks, you'll know what to go after. However you updated your question but did not show where `global $dbConnection;` is located and in which file. – Funk Forty Niner May 20 '16 at 17:59
  • @Fred-ii- I had removed the global because it didn't work, that's why it's not in the files. I will add it so you can see where it was located. The issue is in the `ubbreplace` function, I'm 100% sure of that but is there any other way to include `contactform.php` when [contact-form] is entered in `row['content']`? – J. Doe May 20 '16 at 18:00
  • @J.Doe `global $dbConnection;` it's in the wrong spot and in the wrong file. that belongs like the guy posted the answer below for `function ubbreplace($text){ global $dbConnection;` – Funk Forty Niner May 20 '16 at 18:06
  • @Fred-ii- Yeah, I was playing with the code after I knew it didn't work. Any chance you know a script which converts [contact-form] to `include("contactform.php");`? – J. Doe May 20 '16 at 18:10
  • @Fred-ii- Hey Fred, wanted to let you know that I have solved the problem! That means that we deserve a beer for all the effort. Thanks for all the suggestions, I'll post the solution in the answers. Edit: nevermind. I cannot post a answer. Well then :P – J. Doe May 20 '16 at 21:43
  • @J.Doe Glad to hear it. You can always edit your question and mark it at the bottom of it if you wish. *Cheers* – Funk Forty Niner May 20 '16 at 21:54
  • @Fred-ii- Added it to the question. If you're curious what went wrong you can check it out. If not then have a good weekend. Cheers. – J. Doe May 20 '16 at 21:57
  • @J.Doe I saw that and seen that this wasn't just a variable scope issue so I was able to reopen the question myself. You can post your answer now. Thanks and have a great weekend also. – Funk Forty Niner May 20 '16 at 22:17

2 Answers2

0

Try to make global your $dbConnection inside your function

function ubbreplace($text){
    global $dbConnection;
    if (strpos($text, "[contact-form]") !== false) {
       ob_start();
       include("contactform.php");
       $replace = ob_get_contents();
       ob_end_clean();
       $text = str_replace("[contact-form]", $replace, $text);
    }
    return $text;
}
Luigi Pressello
  • 937
  • 4
  • 9
0

Added global $dbConnection; inside the ob_start()

<?php
function ubbreplace($text){
    if (strpos($text, "[contact-form]") !== false) {
        ob_start();
        global $dbConnection; // <-- added
        include("contactform.php");
        $replace = ob_get_contents();
        ob_end_clean();
        $text = str_replace("[contact-form]", $replace, $text);
    }
    return $text;
}
?>
J. Doe
  • 503
  • 1
  • 6
  • 19