0

My task is i want to create form for search field. want to transfer the value from search to required php file which is 'result.php'. But I am getting bug that I am unable to solve this. please anyone help me. I am new to php..

HTML CODING :

 <form action="result.php" method="POST">
    <input type="search" name="query" size="10" id="searchfield" title="searchfield" />
    <input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" />
    </form>

PHP CODE :

//result.php

@session_start();
include 'connections.php';
//echo "hai"; // I debug this code with the help of echo statements. and  successfully printing hai

if (isset($_POST['query']) && $_POST['query'] != ""){// unable to enter in `if` condition 
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
............`

value that entered in search field is not going from 'HTML' form to 'PHP' file to perform required operations please anyone help me to solve this bug..

7
  • Are your both files in same directory ? Commented Feb 13, 2015 at 11:44
  • In your comments where you are echo'ing out the $query, you are assigning incorrectly, you have $query = var_dump($_POST['$query']); but further down you assign it correctly using $query = $_POST['query']; Commented Feb 13, 2015 at 11:45
  • with the help of var_dump() i am just checking what value is I am getting... and its just debugging.. I did debugging by echoing the simple statements Commented Feb 13, 2015 at 11:49
  • you might want to change how your using mysql - code.tutsplus.com/tutorials/… Commented Feb 13, 2015 at 11:49
  • to check the value, just do : $query = $_POST['query']; echo $query; Commented Feb 13, 2015 at 11:52

3 Answers 3

1

If the values is printed with var_dump($_POST['query']);then it's actually sent by form. And why are you testing twice for lenght of it. Try testing if value is set in file where you got form. Test it with jquery. Then in result.php just check if value is posted. And what are you trying to do with value in result.php?

Try

var_dump($_POST['query']);
if (isset($_POST['query'])){// unable to enter in `if` condition due to NULL value.
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
}
}else
{
echo 'novalues';
}

EDIT

You can try sending value with ajax. Add below code in script tag in your file with form and thats it.

    $("#searchbutton").click(function(){ 
    var value = $("#searchbutton").val(); 
    $.ajax({
    url: "result.php",
    type:"POST",
    data: value, 
    success: function(result){
     $("#div1").html(result); 
    }
    }); 
   });
Sign up to request clarification or add additional context in comments.

7 Comments

var_dump($_POST['$query']); in your explaination, lose the $ for query ;)
we are developing simple library website. we have books in our database. developing search operation.. for that...
not worked... same as before I am getting NULL with var_dump($_POST['$query']);
Hmm. Try removing @ before session, so you will see if any errors there. Then try putting type of input to text instead of search (html5).
I am sorry to say, It doesn't work for me @NejcRodošek
|
1

Try to change type of your input like this

<input type="text" name="query" size="10" id="searchfield" title="searchfield" />

just to be sure it doesn't cause your problem :)

4 Comments

No use... @Branimir Durek
well your code looks fine... press f12 and open network tab. Then submit your form and check your request POST data to be sure that your form sends "query" value. If it works, then try to comment everything and then echo $_POST["query"] just to be sure that rest of your code doesn't cause the problem...
nothing worked for me. I don not know why.. even I checked with ajax and javascript. value from 'html' form to php file is not getting.......
are you sure you don't have duplicated project?? So you edit one, but you test functionality on other...
0

finally I resolved my problem.. problem occurred due to "access permissions" I rewrite the access permissions.. now it is working thank to everyone

Comments

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.