POST FormData to php using javascript and XMLHTTPRequest

前端 未结 2 1968
误落风尘
误落风尘 2021-01-17 03:30

At the moment I have two files, index.htm and accessdata.php. This is what I have in index.htm:





        
相关标签:
2条回答
  • 2021-01-17 03:37

    Tap to create a note You should try this…

    <form method=post action=accessdata.php>
        <input type=checkbox value=blue name=opt1>blue
        <input type=submit value=submit name=send>
    </form>
    

    In accessdata. PHP

    if❨isset❨$_POST[`send']❩❩ {
        $color=$_POST[`opt1'];
        echo $color."bala";
    }
    
    0 讨论(0)
  • 2021-01-17 03:51

    blue doesn't show up because you are claiming:

    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    

    But FormData objects encode data as multipart/form-data.

    Remove the code that explicitly sets the content-type and let the browser generate it for you. (Don't try to explicitly set it to multipart/form-data, you have to specify what the boundary marker is going to be in the header too).

    yellow doesn't show up for the same reason, but also because:

    • You are only looking at opt1 and it is associated with the name opt2 and
    • Checkbox controls are only successful (i.e. will be in the data that gets submitted) if they are checked (which the yellow one is not by default).

    Complicating matters further, your HTML is invalid. Use a validator. You can't have an input as a child of a table row, you need to create a table data cell between them. (Note that it looks like you are trying to use a table for layout, you should probably get rid of the table entirely).

    0 讨论(0)
提交回复
热议问题