简体   繁体   中英

Wordpress search form action

I need to do a custom search function in my wordpress page, some javascript codes needed to put into header and use wordpress search form to call that function.

Under the theme folder of wordpress, I have added some javascript codes between tag in header.php

<head>
  <script>
      var test123456 = function() {
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        }

     </script>

</head>

The default theme searchform.php was:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form> 

I would like to ask how do I call that javascript function by searchform.php ?

my form part:

<form action="javascript:test123456()">
    <input type="text" name="firstname" value="" id="curlinput">
    <input type="submit" value="Submit">
</form> 

many thanks and appreciated !!

change your code to

<form onsubmit="test123456()">
<div><label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="Search" />
</div>

You can put form line this.

<form role="search" method="get" id="searchform" action="javascript:void(0);" onsubmit="test123456();">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

OR like below

<form role="search" method="get" id="searchform" action="javascript:test123456()">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

If you also want to set action file then try below code.

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>" onsubmit="test123456();">
        <div><label class="screen-reader-text" for="s">Search for:</label>
            <input type="text" value="" name="s" id="s" />
            <input type="submit" id="searchsubmit" value="Search" />
        </div>
    </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM