0

Error below: I'm pretty much trying to see what the user chose from the checkboxes.

Notice: Undefined variable: ch1 in /Applications/XAMPP/xamppfiles/htdocs/ProjectOne/index.php on line 42 ch1

HTML CODE :

<form action='submit.php' method="GET">

<div id="self">
<input type='text' name='Name' value='Name' />
    <br>
<input type='text' name='Cwid' value='CWID' />
    <br>
</div>

<div id='gender'>
<strong>Gender:</strong><input type="radio" name="sex" value="male"checked>Male
Or
<input type="radio" name="sex" value="female">Female
<br>
<div>

<div id='class'>
<strong>Class:</strong> <select name='class'>
<option value='Freshman'> Freshman </option>
<option value='Sophomore'> Sophomore </option>
<option value='Junior'> Junior </option>
<option value='Senior'> Senior </option>
</select>
</div>
<br>
<div id='pref'>
<strong>Student Preferences</strong>
<br>
<!-- line 42 -->
<input type="checkbox" name="ch1" value="ch133" /> <?PHP echo $ch1; ?>  ch1 <br />
<input type="checkbox" name="ch2" value="Laundry on Premise" /> Laundry on Premise<br />
<input type="checkbox" name="ch3" value="Fully Equipped Kitchen" /> Fully Equipped Kichen<br />

</div>
<div id='submit'><input type="submit"  name='submit' value="Submit" />   </div>

</form>

PHP CODE BELOW :

<?php
$ch1 = 'unchecked';
$ch2 = 'unchecked';
$ch3 = 'unchecked';
if(isset($_GET['submit'])) 
{
$name = $_GET['Name'];
$cwid = $_GET['Cwid'];
$sex = $_GET['sex'];
$class = $_GET['class'];
$ch1 = $_GET['ch1'];
$ch2 = $_GET['ch2'];
$ch3 = $_GET['ch3'];
    if (isset($ch1)) {
    $ch1 = $_GET['ch1'];

    if ($ch1 == 'ch1') {
        $ch1 = 'checked';
    }
}
}
?>
4
  • What's line 42 ch1 ? Commented Oct 9, 2015 at 16:46
  • 1
    Is your php code under the html also in your page? Commented Oct 9, 2015 at 16:46
  • 2
    Possible duplicate of Reference - What does this error mean in PHP? Commented Oct 9, 2015 at 16:47
  • what is <?echo $ch1;?> doing there ? Commented Oct 9, 2015 at 17:01

2 Answers 2

1

You call an echo on a inexistant variable. When you submit your form, $ch1 does not exist yet. Add this :

<?php $ch1 = 'unchecked'; ?>

in the line before :

<input type="checkbox" name="ch1" value="ch133" /> <?PHP echo $ch1; ?>  ch1 <br />
Sign up to request clarification or add additional context in comments.

Comments

0

You have several inconsistencies in your code.

Notice: Undefined variable: ch1 in /Applications/XAMPP/xamppfiles/htdocs/ProjectOne/index.php on line 42 ch1

Read this, it says a NOTICE, which is Not an error, that the variable ch1 ($ch1) is undefined. so there has been no value set as $ch1.

So, what you need to do is set a value for $ch1 before the script reaches line 42.

Your html says:

 <input type="checkbox" name="ch1" value="ch133" />

Which means when you submit the data to the page, the $_POST/$_GET/$_REQUEST system you are using will return a value of ch133 for variable ch1 .

Because it's a checkbox you can get either this value, or nothing, so your code in your PHP will never be the output you want because:

$ch1 = $_GET['ch1'];

    if (isset($ch1)) {
    //you can only reach this point if the value is already set, 
    //Below, so there's no point setting the value twice. 
    $ch1 = $_GET['ch1']; 
    if ($ch1 == 'ch1') {
        //$ch1 can only be the value of $_GET['ch1'] and so it can 
       // never ever be 'ch1' as it's value, it can be NULL or ch133
       //only. So this IF statement will never ever run. 
        $ch1 = 'checked'; 
    }
}

Also note that your echo $ch1 statement comes outside of the HTML checkbox, so the checkbox will never be marked as ticked. You need to adjust your code to be:

  <input type="checkbox" name="ch1" value="ch133"  <?php print $ch1;?> />

I hope this helps clarify your issues and your approach. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.