0

Hi I try to research my problem via Stack about 3 hours but I still not found. So I decide to create the topic to ask about my problem.

I am creating search engine and the below are the result:

  1. If I type test text into input form then click "enter" button from keyboard, the search result will working correctly.
  2. If I type test text into input form then click "Search" button from webpage, the search result is not working.

My problem is result No 2.

This is my code:

 <form action="search_content.php" method="POST" >  
    <div class="input-group mainsearch-home">           
         <input type="text" class="input-group-field" name="homesearchfield" id="homesearchfield2" placeholder="What are you looking for?" autocomplete="off">
         <div class="input-group-button">
              <button type="button" class="button button--search" >search</button>              
              <input type="hidden" name="homesearchfield" value="search">
         </div>
    </div>
 </form>

What I do wrong? I thought that my problem is happens from input type hidden data. So I would like to know how to get value from input text box and send value to my target page.

I have added some php code from my "response" page on below.

 $viewstate = isset( $_POST["homesearchfield"] ) ? $_POST["homesearchfield"] : "" ;
 $sql="SELECT *  FROM `article` WHERE topic_article LIKE '%$viewstate%' order by id_article DESC";
2
  • where is the php for this? Commented Apr 11, 2018 at 21:11
  • I've put php in this case, if I have to show my response page later, Sorry if it wrong. Commented Apr 11, 2018 at 21:13

2 Answers 2

2

Currently your form doesn't know that the button is meant to submit the form, which can be fixed by changing the type on the button:

<button type="submit" class="button button--search" >search</button>

You could also use:

<input type="submit" class="button button--search" value="search" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer but it is not working for me. :)
0

An option for controlling the data is using JavaScript / jQuery to control the action of the form. This way also allows you to view the data being posted before its actually sent and you can even comment out the post and just work on getting the form with the right data you are looking to get back.

also for serialize to work, you need to have a name for each item you want to pass back data.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script lang="JavaScript">
$(function(){
    $("#button").click(fuction()
    {
        var formData = $("#form").serialize();
        alert(formData);
        /*
        $.post({"search_content.php",
                formData,
                function(returndata)
                {
                    //do something

                    //this will load the return data into the div tag on the fly
                    $("#divReturn").html(returndata); 
                },
                "text"
          });
          //*/
    });

});
</script>
<form id="form" onsubmit="return false;" >  
    <div class="input-group mainsearch-home input-group--search inputs--raspberry">         
         <input type="text" class="input-group-field" name="homesearchfield" id="homesearchfield2" placeholder="What are you looking for?" autocomplete="off">
         <div class="input-group-button">
              <button type="button" class="button button--search" id="button" name="button" value="search" >search</button>              
         </div>
    </div>
</form>
<div id="divReturn">
</div>

2 Comments

I am a newbie for JavaScript / jQuery, Could you please share url link to me for studying this case? :)
the information i posted can be easily found across the stackoverflow.com and for the jquery, the api is at api.jquery.com, api.jquery.com/jquery.post. w3schools.com has tutorials on js and jQuery. having access to client side scripting with server side scripting (php) will improve your process significantly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.