18

I am making an HTML form. I want the results to appear in the PHP script.

<form action="chk_kw.php" method="post"> <br />
    <select> name="website_string" 
        <option value="" selected="selected"></option>
    <option VALUE="abc"> ABC</option>
    <option VALUE="def"> def</option>
        <option VALUE="hij"> hij/option>   
    </select>
    <input type="submit" name="website_string"  >
</form>

The problem is I cannot get the value passed to the PHP. if I use value:

<INPUT TYPE="submit" name="website_string" value="selected" >

It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?

6 Answers 6

40

Try this:

<form method="post" action="check.php">
    <select name="website_string">
        <option value="" selected="selected"></option>
        <option VALUE="abc"> ABC</option>
        <option VALUE="def"> def</option>
        <option VALUE="hij"> hij</option>
    </select>
    <input TYPE="submit" name="submit" />
</form>

Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.

check.php

<?php
    echo $_POST['website_string'];
?>

Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.

Sign up to request clarification or add additional context in comments.

4 Comments

It sends blank. Because value is blank.
use this php with my html to test.
Still blank. Iused your php code. Here is the exact code: <html> <body> <form action="chk_kw.php" method="post"> <br /> <select> name="website_string" <option value="" selected="selected"></option><option VALUE="www.kalibo.org-ph">www.kalibo.org-ph</option><option VALUE="www.kalibo.org-us">www.kalibo.org-us</option><option VALUE="www.seoplannow.com-ph">www.seoplannow.com-ph</option><option VALUE="www.seoplannow.com-us">www.seoplannow.com-us</option></select> <INPUT TYPE="submit" name="submit"/> </form> </body> </html>
don't declare the "name" of the select outside the <select> tag, look how I write <select> above.
6
<form method="POST" action="chk_kw.php">
    <select name="website_string"> 
        <option selected="selected"></option>
        <option value="abc">abc</option>
        <option value="def">def</option>
        <option value="hij">hij</option>   
    </select>
    <input type="submit">
</form>


  • As your form gets more complex, you can a quick check at top of your php script using print_r($_POST);, it'll show what's being submitted an the respective element name.
  • To get the submitted value of the element in question do:

    $website_string = $_POST['website_string'];

1 Comment

How about an upvote, if not for me then for the answer you accepted.
3

It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].

You will probably want the following HTML:

<select name="website_string">
  <option value="" selected="selected"></option>
  <option value="abc">ABC</option>
  <option value="def">def</option>
  <option value="hij">hij</option>   
</select>
<input type="submit" />

With some PHP that looks like this:

<?php

$website_string = $_POST['website_string']; // or $_GET['website_string'];

?>

4 Comments

$website = $_POST["website_string"]; Yes thats the code. When I had it as a text box it sent the proper text. But the free form style let people make mistakes. So I wanted to change to a drop down box. But I cannot get the right text passed.
I edited my answer a bit more. Does that help? Is it still not working given that?
I changed it to your code. in the php file I added print_r($_POST); The result is: Array ( [website_string] => Submit ) . So its sending the text Submit.
@Joe: Oh wow, my bad. Please remove 'name="website_string"' from the submit input. I edited my code to remove this; it should function correctly now. Sorry.
0

Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.

2 Comments

I changed each and get the same results. If I change the first to website_str, I still get the text returned as submit instead of the string I want.
I changed the location of the /select. It does not change the result. The problem is submit is not sending the proper value. If I put value="fff" it will send fff. otherwise it is sending submit. There must be a way to specify the value as selected.
0

For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:

<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>

Comments

0

Here is what I find works

  1. Set a form name
  2. Use a default select option, for example...

    <option value="-1" selected>Please Select</option>

So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.

  1. Try to use HTML5 attributes now they are supported.

This input

<input type="submit">

should be

<input name="Submit" type="submit" value="Submit">

whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.

You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.