每当我将变量回显或写入文本文件时,它都会输出" 1"要么是webapge,要么是文本文件。

时间:2014-09-04 13:17:24

标签: php

这是我的代码,因为你可以看到传递给变量的输入是我想要的。请帮助我现在只是填补空间,因为我需要问问题,我目前正在重新安装xammp希望它解决问题,但如果它是一个代码问题,任何帮助最感激。

<?php
$Name = isset($_POST['name']);
$Car  = isset($_POST['car']);
$Night = isset($_POST['yes']);
$Siteid = isset($_POST['siteid']);
$Date = isset($_POST['datepicker']);


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <title>Engineers Work </title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

  <script type="text/javascript" src="jquery.timepicker.js"></script>
  <link rel="stylesheet" type="text/css" href="jquery.timepicker.css" />

  <script type="text/javascript" src="lib/bootstrap-datepicker.js"></script>
  <link rel="stylesheet" type="text/css" href="lib/bootstrap-datepicker.css" />

  <script type="text/javascript" src="lib/site.js"></script>
  <link rel="stylesheet" type="text/css" href="lib/site.css" />

</head>
<body>
<div id="center">
<br><br><br>
<!-- Allows User to who they are -->
<p>You are: </p><form action="" method="post">
<select name="name" id="name">
<option value="Noname"> </option>
<option value="Tom">Option 1</option>
<option value="Ben">Option 2</option>
<option value="Aaron">Option 3</option>
<option value="Sally">Option 4</option>
<option value="Paul">Option 5</option>
</select><br/><br/>

<!-- Allows User to pick the Vehicle -->
<p>You drive </p>
<select name="car" id="car">
<option value=""> </option>
<option value="Truck 1">Option 1</option>
<option value="Truck 2">Option 2</option>
<option value="Truck 3">Option 3</option>
<option value="Truck 4">Option 4</option>
<option value="Truck 5">Option 5</option>
</select><br/><br/>



<!-- Allows the user to say if they stayed overnight -->
<input type="checkbox" name="yes" value="yes">I Stayed Over night<br><br/>



                <p id="datepairExample">
                    <input type="text" class="date start" id="datepicker" name="datepicker" value="" />


                </p>


            <script src="http://jonthornton.github.io/Datepair.js/dist/datepair.js"></script>
            <script src="http://jonthornton.github.io/Datepair.js/dist/jquery.datepair.js"></script>
            <script>
                $('#datepairExample .time').timepicker({
                    'showDuration': true,
                    'timeFormat': 'g:ia'
                });

                $('#datepairExample .date').datepicker({
                    'format': 'm/d/yyyy',
                    'autoclose': true
                });

                $('#datepairExample').datepair();
            </script>
<br>
<br>


<!-- Allows User to pick site id -->

<p>Site Id: </p> <input type="text" id="siteid" name="siteid"><br/>
<br/><br/>

<!-- Allows User to pick the date -->

     <p>Left Hastings At: <input id="stepExample1" type="text" class="time" /> <br><br> Arived At first site at: <input id="stepExample2" type="text" class="time" /> <br><br> Left Site at: <input id="stepExample3" type="text" class="time" /></p>


            <script>
                $(function() {
                    $('#stepExample1').timepicker({ 'step': 15 });
                    $('#stepExample2').timepicker({ 'step': 60 });

                });
            </script>

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

</form>
<?php
echo  $Name ."<br/>";
echo "". $Car ."<br/>";
echo "". $Night ."<br/>";
echo "". $Date ."<br/>";
echo "". $Siteid ."<br/>";
print_r($_POST);

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = $Name;
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);


?>

</div>



</body>
</html>

2 个答案:

答案 0 :(得分:2)

isset()返回布尔值,并且您将isset()函数的值赋给参数,因此当您尝试回显布尔值时,您会看到&#34; 1&#34;。试试这段代码:

$Name = isset($_POST['name']) ? trim($_POST['name']) :'';

答案 1 :(得分:1)

isset()返回一个布尔值(true或false),可以表示为1或0。

这就是为什么你一直看到1。

您需要正确设置变量:

//wrong
$Name = isset($_POST['name']);

//correct
$Name = isset($_POST['name'])?$_POST['name']:'';