-3

I have php code I have used previously to collect information from users and store in a .txt document but I have pulled it back it seems to throw an error I got from my host customer sup.

Can anyone tell me whats wrong?

Thanks

Editing the code to what I thought the error was pointing to

The .php File vv

<?php
if(isset($_POST['Name']) && isset($_POST['Email']) && isset($_POST['PNumber']) && isset($_POST['PostC']) {
    $data = $_POST['Name'] . '-' . $_POST['Email'] . '-' . $_POST['PNumber'] . '-' . 

$_POST['PostC'] . "\n";
    $ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

$location = "index.html";
header( "Location: $location" );

The Html vvvv

 <script>
$(function() {
    $("#Subscribe").validate({
        rules: {
            Name: {
                required: true,
                minlength: 2,
                maxlength: 23,
                },
            Email: {
                required: true,
                email: true
                },
            PNumber: {
                required: false,
                number: true
                 },
            PostC: {
                required: false,
                 }
    }});

});
                    </script>

<form id="Subscribe" method="POST" action="subscribe.php">
<p class="subBox"><span>(Required) Name: &nbsp; &nbsp;</span><input id="Name" type="text" name="Name" size="20"></p>
<p class="subBox"><span>(Required) Email: &nbsp; &nbsp;</span><input id="Email" type="text" name="Email" size="20"></p>
<p><span>(Optional) Number: </span><input id="PNumber" type="text" name="PNumber" size="20"></p>
<p><span>(Optional) Postcode: </span><input "PostC" type="text" name="PostC" size="20"></p>
<p class="Submit"><span><input type="submit" value="Subscribe" name="Submit"></span></p>
</form>

It should store the text entered in the form to a mydata.txt file like previously but is giving the error :

PHP Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND) in (Removed for security)/subscribe.php on line 2

Note: I have replicated what was previously working before but this time i have changed the options

2 Answers2

1

You haven't bracket ")" in the condition in line 2

if(isset($_POST['Name']) && isset($_POST['Email']) && isset($_POST['PNumber']) && isset($_POST['PostC']) )

0

As many here have said, you seem to have an issue with your brackets. When you're writing out if statements with lots of conditions you should first ask yourself if this is the best set up (in my experience lots of conditions in the same if often point to something wrong, or can be easily simplified). If this isn't the case (sometimes you really do have to suck it up and write out all the conditions) you should break it out so that it's easier to read, easier to see where brackets 'balance' and easier to debug. I would have written your statement like this:

if(//opening bracket
   isset($_POST['Name']) 
   && isset($_POST['Email']) 
   && isset($_POST['PNumber']) 
   && isset($_POST['PostC']) )//extra incorrect bracket which is now easy to spot
 )//closing bracket - in line with the opening bracket {
//do some stuff
}

if you had more complicated conditions this would help you further e.g.:

if( 
    (//first set of conditions
      isset(condition_1)
      && isset(condition_2)
    ) &&
    (//second set of conditions
      isset(condition_3) 
      || isset(condition_4)
    )
  )

by doing this you can easily see where you conditions balance (i.e an open matches a close). Remember, you don't win any prizes for getting all your conditions on one line, and having fewer lines of code does not equal more efficient code, as your example demonstrates.

It's also worth noting that you don't need to write isset each time, with isset you can say isset($var1,$var2,$var3) and this is the same as writing all your && issset's.

I hope this is helpful in your future coding and will help you to debug things more quickly

imposterSyndrome
  • 896
  • 1
  • 7
  • 18