1

How do I get the the value from an input text box using PHP? My code is below:

<body>
    <form action="new.php" method="post">
        Date: <input name="date" type="text" value="Saturday, October 11, 2014" />
              <input name="ok" type="submit" value="Submit" />
     <?php      
        if(isset($_POST['ok'])){
            $day=$_POST['date'];
            echo "today's date is".$day;//how do I echo only the word Saturday?
        }else   
    ?>
    </form>
</body>

I would like to echo only the word "Saturday", but I am unsure of how to do this given that my $day variable contains the entire date string. Any help?

Adam Link
  • 2,783
  • 4
  • 30
  • 44
melly
  • 61
  • 3
  • 7

3 Answers3

0

YOU can get any data from form by super global var: $_POST['name_of _input'] IF YOU make method post or $_GET['name_of_input'] if you make method get. this html tags

<html>
<body>
<form action="name_of_page_process.php" method="post">
<input type="text" name="days" placeholder="enter name of day" />
<input type="submit" name="sub" />
</form>
</body>
</html>



    this code of process by php :

<?php 
if(isset($_post['sub'])){
$day=$_post['days'];
echo "name of day is".$day;

}

?>
hamdy
  • 46
  • 4
0

You can use explode function:

<body>
<form action="new.php" method="post">
    Date: <input name="date" type="text" value="Saturday, October 11, 2014" />
          <input name="ok" type="submit" value="Submit" />
 <?php      
    if(isset($_POST['ok'])){
        $day=$_POST['date'];
        $pieces = explode(",", $day);
        echo "today's date is ".$pieces[0]; // show only word "Saturday"
    }else   
?>
</form>

k3nny
  • 18
  • 5
0

This should work for you

<body>
    <form action="" method="get">
        Date: <input type="text" name='day' value="Saturday, October 1<?php ?>1, 2014" />
<input type="submit">
    </form>
</body>


<?php  


     $day = $_GET["day"] ;
     if(isset($day)):
     echo strtok($day, ",");

     endif;   


?>
Mubo
  • 1,078
  • 8
  • 16