Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP-generated functions not defined

    Hello again, CodingForums. I have a .php page that generates the following javascript which is intended to hide/show a form somewhere on the page. (It only shows right now because it's not done yet.) However, after the page is rendered and given to the client, the function calls do not work, and the error console reports that "[function name] is not defined."

    Code:
        <script type="text/javascript">
          function showEdit(num)
          {
            var elem = document.getElementById("form-wrap"+num);
            var str = "<form action=\"./blog.php\"  method=\"POST\" class=\"blogform\">";
            str = str + "<input type=\"hidden\" name=\"u\" value=\"Webmaster\"/>";
            str = str + "<input type=\"hidden\" name=\"q\" value=\"edit\"/>";
            str = str + "<input type=\"hidden\" name=\"n\" value=\""+num+"\"/>
            str = str + "<label for=\"title\">Title</label>";
            str = str + "<input type=\"text\" name=\"title\" value=\""+document.getElementById("title"+num).innerHTML+"\"/>";
            str = str + "<label for=\"picture\">Picture</label>";
            str = str + "<input type=\"file\" name=\"picture\"/>";
            str = str + "<label for=\"caption\">Picture Caption</label>";
            str = str + "<input type=\"text\" name=\"caption\" value=\""+document.getElementById("post-image"+num).getAttribute("title")+"\"/>";
            str = str + "<label for=\"content\">Entry Text</label>";
            str = str + "<textarea name=\"content\" cols=\"100\" rows=\"20\" class=\"blogtextarea\">"+document.getElementById("content"+num).innerHTML+"</textarea>";
            str = str + "<input type=\"submit\" value=\"Edit Post\"/>";
            str = str + "</form>";
            elem.innerHTML = str;
          }
    
    
          function showDelete(num)
          {
            var elem = document.getElementById("form-wrap"+num);
            var str = "<form action=\"./blog.php\"  method=\"POST\" class=\"blogform\">";
            str = str + "<input type=\"hidden\" name=\"u\" value=\"Webmaster\"/>";
            str = str + "<input type=\"hidden\" name=\"q\" value=\"delete\"/>";
            str = str + "<input type=\"hidden\" name=\"n\" value=\""+num+"\"/>
            str = str + "<label for=\"really\">Delete this post?</label>";
            str = str + "<input type=\"radio\" name=\"really\" value=\"Yes\">Yes</input><br/>";
            str = str + "<input type=\"radio\" name=\"really\" value=\"No\" checked=\"checked\">No</input><br/>";
            str = str + "<input type=\"submit\" value=\"Confirm\"/>";
            str = str + "</form>";
            elem.innerHTML = str;
          }
    
    
          function showNew(num)
          {
            var elem = document.getElementById("new-form-wrap");
            var str = "<form action=\"./blog.php\"  method=\"POST\" class=\"blogform\">";
            str = str + "<input type=\"hidden\" name=\"u\" value=\"Webmaster\"/>";
            str = str + "<input type=\"hidden\" name=\"q\" value=\"new\"/>";
            str = str + "<input type=\"hidden\" name=\"n\" value=\""+num+"\"/>";
            str = str + "<label for=\"title\">Title</label>";
            str = str + "<input type=\"text\" name=\"title\"/>";
            str = str + "<label for=\"picture\">Picture</label>";
            str = str + "<input type=\"file\" name=\"picture\"/>";
            str = str + "<label for=\"caption\">Picture Caption</label>";
            str = str + "<input type=\"text\" name=\"caption\" />";
            str = str + "<label for=\"content\">Entry Text</label>";
            str = str + "<textarea name=\"content\" cols=\"100\" rows=\"20\" class=\"blogtextarea\"></textarea>";
            str = str + "<input type=\"submit\" value=\"Create Post\"/>";
            str = str + "</form>";
            elem.innerHTML = str;
          }
        </script>
    These are all the elements that call the javascript functions.

    Code:
    <a onclick="showNew('2');" class="edit-delete">Create New Entry</a>
    <a onclick="showDelete('1');" class="edit-delete">Delete</a>
    <a onclick="showEdit('1');" class="edit-delete">Edit</a>
    <a onclick="showDelete('0');" class="edit-delete">Delete</a>
    <a onclick="showEdit('0');" class="edit-delete">Edit</a>
    I have no idea why my functions aren't being recognized by the interpreter as such, but it's probably something simple. Thanks in advance for the help, and I'll be happy to post more code if it might be relevant.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Did you try to provide a "dummy" href for the <a> anchor tags? I remember something about either href or name being mandatory...

    Second guess: ANY syntax error in Javascript will hide EACH AND EVERY javascript function even if the function is correct. So try searching for syntax errors.

  • Users who have thanked devnull69 for this post:

    elarsen7823 (06-06-2011)

  • #3
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I haven't tried providing dummy hrefs for the anchor tags, no. What are some that would keep me on the current page without reloading anything? And to your second guess, where should I be looking for syntax errors? In the function definitions themselves?

  • #4
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Never mind! I found the syntax errors. They were here:

    Code:
    str = str + "<input type=\"hidden\" name=\"n\" value=\""+num+"\"/>
    and here:

    Code:
    str = str + "<input type=\"hidden\" name=\"n\" value=\""+num+"\"/>
    Thanks for the suggestion!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •