1

When my page loads, it displays:

Notice: Undefined variable: titleErr in index.php on line 141

I defined $titleErr at the beginning of my PHP, what else am I missing?

PHP:

<?php
    $titleErr = ''; 
    $title = '';

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    if(isset($_GET)) {

        if (empty($_GET['page_title'])) {
            $titleErr = 'Name is required';
        } else {
            $title = test_input($_GET['page_title']);
        }

    }
?>

$(function() {
 $('form').submit(function(e) {
  $page_title =  $('input[name="page_title"]').val();
  e.preventDefault();
  $.ajax({
   method: 'GET',
   url: 'save_to_wp.php',
   datatype: 'json',
   data: {
    page_title: ($page_title)
   }
  });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <div id = "save-dam" class = "tiny reveal" data-reveal>
        <form action = '<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' class = "row">
            <div class = "small-12">
                <label>Save as</label>
            </div>

            <div class = "small-12">
                <input type = "text" placeholder = "Page Name" name="page_title" />
                <span class="error"><?php echo $titleErr;?></span>
            </div>

            <div class = "small-12">
                <button type = "submit" class = "button pill">Save</button>
            </div>

            <div class = "small-12">
                <button id="form-cancel" class = "button pill cancel">Cancel</button>
            </div>
        </form>
    </div>

Help or suggestion would be greatly appreciated!

crinkledMap
  • 1,192
  • 1
  • 8
  • 8
  • Is this all of your code for index.php? – Joseph_J Jun 06 '18 at 07:01
  • @Joseph_J No, it's a big-ish page - would be huge to post. The page has existed and been working until I added the form. The form even works except for this error msg. The form is in a Foundation pop-up, and that is where the error is displayed. – crinkledMap Jun 06 '18 at 07:29
  • Well we need to see more code. Especially around line 141 on the index.php file. – Joseph_J Jun 06 '18 at 07:32
  • I edited the snippet to include the whole div containing the pop-up form. "Line 141" is the line with the undefined variable. There's not any more PHP in the markup associated with the form. My edit just inserted a bunch more HTML. – crinkledMap Jun 06 '18 at 07:47
  • what line is 141? – Joseph_J Jun 06 '18 at 07:58
  • The line with the undefined variable: – crinkledMap Jun 06 '18 at 08:09

1 Answers1

0

Another member posted an answer and suggested that scope was an issue but deleted it. He may have been right despite the fact that no where in the code you have provided us does $titleErr get called out of scope. Maybe somewhere else on your page idk. You could trouble shoot that by declaring global $titleErr; at the top of you page.

I did see some errors in the code that you posted unrelated to $titleErr.

Your javascript should look like this:

<script>
$(function() {
    $("#myForm").submit(function(e) {
        e.preventDefault();
        $.ajax({
            method: 'GET',
            url: 'save_to_wp.php',
            datatype: 'json',
            data: {
                page_title: $('input[name="page_title"]').val()
            }
        });
    });
});
</script>

The way that you were interchanging js variables with php variables does not work.

Secondly the javascript needs to be loaded after your form.

Joseph_J
  • 3,654
  • 2
  • 13
  • 22