dongyuling0312 2017-02-24 00:23
浏览 65
已采纳

标准语法$ _POST ['']在解析变量之前返回false / drops数据

Sup stackies.

Okay, i have got a real head scratcher for you guys! I've been trying to solve this puzzle for a week now (apparently i was on stack cooldown). Ok so whats going on is that i have 2 scripts, that goes under the same if/elseif statement. They both work 100% individually (multiple tests were taken to conclude this). The script is supposed to start the if/elseif depending on one of 2 submit buttons. the scripts themselves is not the problem. It's the process of starting the scripts. On the same page as the 2 submit buttons, there is 2 "text" fields, that both of the scripts utilize to complete.

The buttons is build like this:

<input type="SUBMIT" Name="Udlaan"  VALUE="Udlån" METHOD="POST" class="Aside-Udlån">
<input type="SUBMIT" Name="Aflever"  VALUE="Aflever" METHOD="POST" class="Aside-aflever" >

They work.

The text fields are build like this:

<input Type="Text" Name="bunnr" Method="POST" Class="Skriv_bun" Placeholder="bun Nummer">
<input Type="Text" Name="Ordreren" Method="POST" Class="Ordre_No_Skriv" Placeholder="Ordre Nummer">

The variables that holds the data from the textfield looks like this:

$Ordre_Nr=$_POST['Ordreren'];
$bun_Nr=$_POST['bunnr'];

So the problem is that the data from the text field is never getting parsed into the variablesm, which causes my scripts to never run . (I mind you that the input fields and the variables are 3 lines apart with nothing inbetween nor above the textfields that has any relevance to this problem.) I added in or die($conn); on one of the variables, which gave me "Object of class mysqli could not be converted into string". Without it, the above just returns false with undefined index.

I tried making new scripts that is in no way connected to my real script. This is one of them:

<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
  </head>
  <body>
  <form METHOD="POST">  <input type="submit" Method="POST" NAME="WORLD" Placeholder="World">
  <input type="text" Method="POST" NAME="Hello" Placeholder="hej">

  </form>
 <?php
 $f = $_POST['World'];
 $e = $_POST['hej'];
 if(isset($f)){echo $e;}
 ?>

  </body>
</html>

Do we agree that this should work? well it doesn't. Nothing, sip, nada. except the same "Undefined index". I made another quite like the one above(Almost exactly like it) using POST aswell, the data i put in the text field came out as GET (I could see everything in the url bar).

This isn't just a standard error, i had a guy with 20 years (on/off) of experience look at it, and he could not find a single syntax error, that could be related to this issue.

If anyone has any really good suggestions, please come forth and prevail! And should you be in need for more information, i will try my best to provide ^^

Fyi, i don't care much for "SQL injection comments, since it's not important for this issue. Fyi 2. This is NOT a duplicate. I have checked multiple questions with answers for this issue, nothing has worked.

展开全部

  • 写回答

1条回答 默认 最新

  • douduoting8408 2017-02-24 00:29
    关注

    When using a form the method should only go into the <form> tag as shown in my example below:

    Note that I have used name="sendPost" as the name for submit button.

    <form method="post">
        <input type="text" name="world" placeholder="World">
        <input type="text" name="hello" placeholder="hej">
        <input type="submit" name="sendPost" value="Submit">
    </form>
    

    Then within the PHP you need to check if the sent has been submitted:

    <?php
    
    if (isset($_POST['sendPost']))
    {
        $f = $_POST['world'];
        $e = $_POST['hello'];
    
        echo $e, $f;
    }
    
    ?>
    

    Therefore, If isset sendPost (if sendPost has been pressed run this query)

    Also note, the PHP should go above the html rather than below.

    Added to:

    Else if's again run pretty simple, here's an addition to my initial answer:

    Take a form and add 2 submits instead of one.

    <form method="post">
        <input type="text" name="world" placeholder="World">
        <input type="text" name="hello" placeholder="hej">
        <input type="submit" name="sendPost" value="Submit">
        <input type="submit" name="reset" value="Reset Form">
    </form>
    

    Then within the PHP after your IF has closed you can run elseif (something else) such as my added example:

    <?php
    
    if (isset($_POST['sendPost']))
    {
        $f = $_POST['world'];
        $e = $_POST['hello'];
    
        echo $e, $f;
    } elseif (isset($_POST['reset']))
    {
        echo 'the form has been reset';
    }
    
    ?>
    

    Therefore, if sendPost has been submitted run whats within the brackets and echo out $e and $f. Else if you've pressed Reset run the echo of the form has been reset.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部