0

I'm trying to get the value from this HTML input tag :

<form name="form" action="" method="post">
<input name="search" id="search" type="text" class="typeahead" />
</form>

and SpellCorrector::correct function in the following PHP get the value as a string, because it's only accept value from a string type :

<?php
$box = $_POST("search");
include 'SpellCorrector.php';
$correct = SpellCorrector::correct($box);
echo $correct;
?>

The code above isn't working, so I guess I should find a way to convert the $box variable into string type but I don't know how to. Or maybe there's a better solution for my problem.

I'm using this for SpellCorrector.php file

2 Answers 2

1

You have a syntax error in the PHP part of the code. $_POST returns array and you should access it that way. Use $box = $_POST["search"] instead.

<?php
    $box = $_POST["search"];
    include 'SpellCorrector.php';
    $correct = SpellCorrector::correct($box);
    echo $correct;
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Same method as above, not working. Undefined index: search comes out
How are you submitting the form? You are missing submit button in the HTML snippet you shared. <form name="form" action="" method="post"> <input name="search" id="search" type="text" class="typeahead" /> <input type='submit' value="Send" name="submit" /> </form> Then, since the action attribute is "" I assume you are handling them in the same .php file. You should check if isset($_POST['submit']) before trying to get the $_POST['search']
Copy paste the whole file so I would be able to assist further
now I know operator isset is really useful, anyway, can I submit the form without using the submit button? thanks @ToshoTrajanov
Glad you fixed it!
1

You need Square brackets. []

<?php
$box = $_POST["search"];
include 'SpellCorrector.php';
$correct = SpellCorrector::correct($box);
echo $correct;
?>

1 Comment

It turns out to be Undefined index : search

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.