i need help calling js script i php variable

Discussion in 'PHP' started by john hashim, Jan 14, 2019.

  1. #1
    hi i have a script that is in php and this is how the code is like
    $contents .= "
        //html code here the code here
       ";
    PHP:
    and this is one of my js function
    function domain_form_submit()
                {
                  
                    document.theform.submit();
                }
    Code (markup):
    and i want to add this form however is facing a problem because this js function is not loading and i get a php error
    <form method="post" action="root.php" name="theform">
                    <input type="text" id="metanav-search-field" value="enter domain name" onclick="javascript:this.value='';" name="url">
                    <button id="domain_search_btn" class="oBtnPrimary oBtnInline" onclick="domain_form_submit();">Search</button>              
                    </form>
    Code (markup):
    please i need help on how i can solve this
     
    john hashim, Jan 14, 2019 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Every buttons should be given a type,... no?
     
    hdewantara, Jan 14, 2019 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,803
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #3
    What is the actual problem?
     
    sarahk, Jan 14, 2019 IP
  4. john hashim

    john hashim Active Member

    Messages:
    70
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #4
    hey problem here is that i have to put the form in a php variable as this

    <?php
    $content .="
    <form method=\"post\" action=\"root.php\" name=\"theform"\>
                    <input type=\"text"\ id=\"metanav-search-field\" value=\"enter domain name\" onclick=\"javascript:this.value=\'';" name=\"url\">
                    <button id=\"domain_search_btn\" class=\"oBtnPrimary oBtnInline\" onclick=\"domain_form_submit();\">Search</button>           
                    </form>";
    Code (markup):
    and im get errors

    since the script templating is that every code has to be inside $content .=" ";

    so the problem is how can target the class instead of adding the js in the form. I'm not that experienced in coding so i need help
     
    Last edited by a moderator: Jan 15, 2019
    john hashim, Jan 15, 2019 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,803
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #5
    There are a few different ways to get the form into the variable but this might make it a bit easier

    <?php
    $content .="
    <form method='post' action='root.php' name='theform'>
    <input type='text' id='metanav-search-field' value='enter domain name' onclick='javascript:this.value=';' name='url'>
    <button id='domain_search_btn' class='oBtnPrimary oBtnInline' onclick='domain_form_submit();'>Search</button>
    </form>";
    HTML:
    but something else I do when building up a complex string like this is to take a building blocks approach like this:
    <?php
    $blocks = [];
    $blocks[] = "<form method='post' action='root.php' name='theform'>";
    $blocks[] = "<input type='text' id='metanav-search-field' value='enter domain name' onclick='javascript:this.value=';' name='url'>";
    $blocks[] = "<button id='domain_search_btn' class='oBtnPrimary oBtnInline' onclick='domain_form_submit();'>Search</button>";
    $blocks[] = "</form>";
    $content .= implode($blocks);
    HTML:
     
    sarahk, Jan 15, 2019 IP
  6. john hashim

    john hashim Active Member

    Messages:
    70
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #6
    Hey thanks, I was still getting PHP error because the script coding style is old but i used this and it worked since it getting the form to query info from the other site which is not on my server
    $content    .="
    <form method=\"get\" action=\"https://www.example.com/reports.php\" name=\"theform\">
            <div class=\"input-group\">
                <input type=\"text\" class=\"form-control\" name=\"domain\" placeholder=\"Type domain name..\">
                <span class=\"input-group-btn\"><button type=\"submit\" name=\"submit\" class=\"btn btn-primary\">get info</button></span>
            </div>
    
            </form>";
    Code (markup):
    i just got lead of the js
     
    john hashim, Jan 15, 2019 IP