Newbie in php,,,please help

Hello all,

my browser page is blank after hitting my submit button.seems like my php is not working…please take a look at my code and let me know where i went wrong…
i am trying to save my form data in a local text file.
i am new to php…
thanks in advance
my html:


<form method="post" action="Input1.php" name="form">


 <label> New Data set: </label>

    <input type="radio" name="url" value="NetOptInput2.html" id="ex1" required/> Yes
    <input type="radio" name="url" value="NetOptResult2.html" id="ex2" required/> No

<br><br><br>
 <label>Dataset description:
</label>
 <input type="text" name= "Dataset" id="field1" size="30" placeholder="" readonly><br><br><br>
 <label>Token Number : </label><input type="text" name="Token Number" id="field2" size="6" placeholder="" readonly><br><br><br>

<div style="text-align: center"><br>
  <input type="Submit" name="submit" value="Submit" class="submit">
 <div class="spacer"></div>
</form>

My PHP:


<?php
if (isset($_POST['submit'])) {
   file_put_contents('C:\\Users\\usiui\\Desktop\\input.txt', $_POST['Dataset'], FILE_APPEND | LOCK_EX););
   file_put_contents('C:\\Users\\usiui\\Desktop\\input.txt', $_POST['Token Number'], FILE_APPEND | LOCK_EX););
   };
   ?>

please help

If the page is blank, that typically means there is a PHP error somewhere.

To view the error message, you will need to enable error reporting:

http://php.net/manual/en/function.error-reporting.php

error_reporting(E_ALL);

Looks like you’ve got a few extra semi-colons in there

<?php
	if (isset($_POST['submit'])) {
		file_put_contents('C:\\Users\\usiui\\Desktop\\input.txt', $_POST['Dataset'], FILE_APPEND | LOCK_EX);
		file_put_contents('C:\\Users\\usiui\\Desktop\\input.txt', $_POST['Token Number'], FILE_APPEND | LOCK_EX);
	}
?>

here is my HTML. please take a look at the code below and let me know the flaws…thanks


<form method="post" action="Input1.php" name="form">
 
 
 <label> New Data set: </label>
 
    <input type="radio" name="url" value="NetOptInput2.html" id="ex1" required/> Yes
    <input type="radio" name="url" value="NetOptResult2.html" id="ex2" required/> No
    
<br><br><br>
 <label>Dataset description:
</label>
 <input type="text" name="Dataset" id="field1" size="30" placeholder="" readonly><br><br><br>
 <label>Token Number : </label><input type="text" name="Token Number" id="field2" size="6" placeholder="" readonly><br><br><br>
 
<div style="text-align: center"><br>
  <input type="Submit" name="submit" value="Submit" class="submit">
 <div class="spacer"></div> 

My new PHP


<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){ 
$data = $_POST['Dataset'];
 
$file = "C:/wamp/www/NetOptUI2/input.txt"; 
 
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 
 
fclose($fp); 
echo "Saved to $file successfully!";
 
}
?>

my javascript inside html


<script type="text/javascript">
  $(function(){
   
     $('form').submit(function(event){
   event.preventDefault();
   window.location = $('input[type=radio]:checked').val();
   });
   });
 </script>
 <script type="text/javascript">
 $(function(){
    $("#ex1, #ex2").change(function(){
        $("#field1, #field2").val("").attr("readonly",true);
        if($("#ex1").is(":checked")){
            $("#field1").removeAttr("readonly");
            $("#field1").focus();
        }
        else if($("#ex2").is(":checked")){
            $("#field2").removeAttr("readonly");
            $("#field2").focus();   
        }
    });
});
</script>

This works fine if $_POST[‘Dataset’] has a value.

<?php
if (isset($_POST['Dataset'])){
$data = $_POST['Dataset'];
$file = "C:/wamp/www/NetOptUI2/input.txt";

$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");

fclose($fp);
$message = "Saved to $file successfully!";
}
?>
<html>
<head>
<script type="text/javascript">
  $(function(){

     $('form').submit(function(event){
   event.preventDefault();
   window.location = $('input[type=radio]:checked').val();
   });
   });
 </script>
 <script type="text/javascript">
 $(function(){
    $("#ex1, #ex2").change(function(){
        $("#field1, #field2").val("").attr("readonly",true);
        if($("#ex1").is(":checked")){
            $("#field1").removeAttr("readonly");
            $("#field1").focus();
        }
        else if($("#ex2").is(":checked")){
            $("#field2").removeAttr("readonly");
            $("#field2").focus();
        }
    });
});
</script>
</head>
<body>
<?php
if (isset($message)){echo $message;}
?>

<form method="post" action="" name="form">


 <label> New Data set: </label>

    <input type="radio" name="url" value="NetOptInput2.html" id="ex1" required/> Yes
    <input type="radio" name="url" value="NetOptResult2.html" id="ex2" required/> No

<br><br><br>
 <label>Dataset description:
</label>
 <input type="text" name="Dataset" id="field1" size="30" placeholder="" value="testing123" readonly><br><br><br>
 <label>Token Number : </label><input type="text" name="Token Number" id="field2" size="6" placeholder="" readonly><br><br><br>

<div style="text-align: center"><br>
  <input type="Submit" name="submit" value="Submit" class="submit">
 <div class="spacer"></div>
 </div>
 </form>
</body>
</html>

Thanks for your response… i copied the code posted by you and tried.but, still its not working with the Dataset value set. please help.

Try a relative path to file as that was what I was using for testing.

tried giving relative path too…No luck.please suggest.

None of the errors are showing, i.e. “Couldn’t open input.txt for writing!”?

… and input.txt is not open in any editor?