Jump to content

Simple PHP problem


bogchi

Recommended Posts

This is just a simple script, but i still having a problem with it. I hope anyone could help me with this. The problem was on the visible radio button, cause it won't work. When i change the visible to "No" it will load and go back to "Yes" again. Again hoping someone could help me with this.

 

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>


<?php
if (intval($_GET['subj']) == 0) {
	redirect_to("content.php");
}

if (isset($_POST['submit'])) {
	$errors = array();

	$required_fields = array('menu_name', 'position', 'visible');
	foreach($required_fields as $fieldname) {
		if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_int($_POST[$fieldname]))) {
			$errors[] = $fieldname;
		}
	}
	$fields_with_lengths = array('menu_name' => 30);
	foreach($fields_with_lengths as $fieldname => $maxlength ) {
		if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
		$errors[] = $fieldname; }
		}

		if (empty($errors)) {
			// Perform Update
			$id = mysql_prep($_GET['subj']);
			$menu_name = mysql_prep($_POST['menu_name']);
			$position = mysql_prep($_POST['position']);
			$visible = mysql_prep($_POST['visible']);

			$query = "UPDATE subjects SET
						menu_name = '{$menu_name}',
						position = {$position},
						visible = {$visible}
					WHERE id = {$id}";

			$result = mysql_query($query, $connection);
			if (mysql_affected_rows() == 1) {
				// Success
				$message = "The subject was successfully updated.";
			} else {
				// Failed
				$message = "The subject update fialed.";
				$message .= "<br />" . mysql_error();
			}

		} else {
			// Errors occured
			$message = "There were " . count($errors) . " errors in the form.";
		}


	}	

?>
<?php find_selected_page();	?>
<?php include("includes/header.php"); ?>

    <table id="structure">
    	<tr>
    		<td id="navigation">
            	<?php echo navigation($sel_subject, $sel_page); ?>
                              
            </td>
            <td id="page">
            	<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>

<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php
// output a list of the fields that had errors
	if (!empty($errors)) {
		echo "<p class=\"errors\">";
		echo "Please review the following fields:<br />";
		foreach($errors as $error) {
			echo " - " . $error . "<br />";
		}
		echo "</p>";
	}
?>

                <form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post">
                	<p>Subject name:
                    	<input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" />
                    </p>
                    <p>Position:
                    	<select name="position">
                        	<?php
							$subject_set = get_all_subjects();
							$subject_count = mysql_num_rows($subject_set);
							// $subject_count + 1 b/c we are adding a subject
							for($count=1; $count <= $subject_count+1; $count++) {
								echo "<option value=\"{$count}\"";
								if ($sel_subject['position'] == $count) {
									echo " selected";
								}
								echo ">{$count}</option>";
							}
						?>
                        	
                        </select>
                    </p>
                    <p>Visible:
                    	<input type="radio" name="visible" value="0"<?php 
					if ($sel_subject['visible'] == 0) { echo " checked"; }
					?> /> No
                         
                        <input type="radio" name="visible" value="1"<?php 
					if ($sel_subject['visible'] == 1) { echo " checked"; }
					?> /> Yes
                    </p>
                    <input type="submit" name="submit" value="Edit Subject" />
                </form>                
                <br />
                <a href="content.php">Cancel</a>
            </td>
        </tr>
</table>

<?php require("includes/footer.php"); ?>

Link to comment
https://forums.phpfreaks.com/topic/134405-simple-php-problem/
Share on other sites

The correct way is to use checked="checked" rather than just checked. Same applies to selected too (selected="selected").

 

If your radio buttons still not being checked then check the value of the $sel_subject['visible'] variable is being set correctly.

Link to comment
https://forums.phpfreaks.com/topic/134405-simple-php-problem/#findComment-699712
Share on other sites

$sel_subject['visible'] doesn't even have a value to begin with.  So in this piece of code:

 

if ($sel_subject['visible'] == 0) { echo " checked"; }
?> /> No
 
if ($sel_subject['visible'] == 1) { echo " checked"; }
?> /> Yes

 

You're comparing nothing to 1 and 0.

 

 

Link to comment
https://forums.phpfreaks.com/topic/134405-simple-php-problem/#findComment-699753
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.