0

I am a newbie to php and learning now, can you please help me telling how can I take inputs from users in pHp instead of using forms (i.e., a replacement of scanf() in php. I am solving a question to take number as a input and print that number untill the number is 45.)

and

I want to use html and php on the same file, I am doing this which is showing error,how to correct that,don't know.
I think its due to null, please check my code

<html>
<body>
<form action="" method="post">
            <input type="text" name="user" placeholder="enter a text" />           
    </form>
</body>
</html>
<?php
    echo $_POST["user"]; 
?>
3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by cse.avinash
0

Hi,

for your form question, you can rewrite your codes like this..

<?php
    if(isset($_POST['submit']) && (!empty($_POST['user']))){


    echo $_POST["user"];

    }
    else{
        ## we show the form below

    ?>

    <html>
    <body>
    <form action="" method="post">
    <input type="text" name="user" placeholder="enter a text" />
    <br/>
    <input type="submit" name="submit" value="submit"/>
    </form>
    </body>
    </html>

    <?php

    }

    ?>

Pretty much everything are self explanatory.. all what is the script doing is to check if the submit button has been set, and then check if the name is not empty. If these two check points are true and we echo the submitted name.Otherwise, we show the form.

for the second question, you can use the while loop function of php.. as shown here.

Edited by veedeoo: more info added

0

Thanks for the reply.. I have understood but facing difficluties with input without using form..
I hve to covert this c code to php code, how to do this.:-
here is the c code :-

int main() {
  int num;
  while(1) {
     scanf("%d",&num);
     if(num==45)break;
     else printf("%d\n",num);
    }
    return 0;
}

I have no problem with the looping structure and variable declaration, what I am facing problem is how to take inputs. :(
As per your solution, I ve used while loop but getting infinite loop, please check this..

<?php
    while(1) {
      if((!empty($_POST['num']))){
          if($_POST['num']=="45")   break;
           else echo $_POST["num"];
     }
    else{
         ## we show the form below
?>
<html>
  <body>
    <form action="" method="post">
       <input type="text" name="num" />
      <br/>
   </form>
 </body>
</html>
<?php
    }
  } 
?>
0

instead of clicking submit button, you can press enter.. I have checked this..here is the veedeoo's code without submit button :-

    <?php
    if( (!empty($_POST['user']))){
    echo $_POST["user"];
    }
    else{
    ## we show the form below
    ?>
    <html>
    <body>
    <form action="" method="post">
    <input type="text" name="user" placeholder="enter a text" />
    <br/>
    </form>
    </body>
    </html>
    <?php
    }
    ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.