Jump to content

checkboxes form submit


Guber-X

Recommended Posts

So what i am trying to do is based on a administrative section of the website to edit pictures in a section of the website. the page displays all the images in the database, each image has 3 inputs to them, all based under one form for all pictures. so each picture has two checkboxes and one number input. First checkbox is based on if the picture will be active or not for displaying on the website. second input is the order of when this picture will be displayed ( yes this is for a feature banner that changes images based on time ). the last input is the second checkbox for deleting the photo(this checkbox is working fine as it uses a different submit button). I have this page set up so it will have all the active pictures already checked. now when trying to "uncheck" the active picture, it will not change the status of active.

 

I will provide the code for the whole thing, maybe im going about this the wrong way. just mainly need some direction on how to go about what im trying to accomplish.

 

my database layout:

 

NAME: feature

|  id  |  position  |  pic  |  active |

========================

|  1  |      3         |  jpg  |   true   |

|  2  |      1         |  jpg  |  true    |

|  3  |    NULL    |  jpg  |  false   |

|  4  |       2        |  jpg  |  true    |

<form method="post" id="delete" name="delete">
  <?php
    $pic_query = mysqli_query($con, "SELECT * FROM feature")
        or die("Pic_Query Failed: ".mysqli_error($con));
    $count = mysqli_num_rows($pic_query);

    while($row = mysqli_fetch_array($pic_query)){
      extract($row);
      if($active=='true'){
	$checked = 'checked';
      }else{
	$checked = NULL;
      }
  ?>
  <div style="display:inline-block; margin: 4px; padding:5px; border:solid 1px #000000; border-radius:5px;">
    <img src="../images/feature/<?php echo $pic ?>" width="200px">
    <br>Active: <input type="checkbox" id="active[<?php echo $id ?>]" name="active[]" value="true" <?php echo $checked ?>>
    <br>Position: <input type="number" id="pos[<?php echo $id ?>]" name="pos[]" min="1" max="5" value="<?php echo $position; ?>">
    <br>Delete: <input type="checkbox" id="checkbox[<?php echo $id; ?>]" name="checkbox[]" value="<?php echo $id; ?>">
  </div>
  <?php
  }
  if(isset($_POST['delete'])){
    for($i=0;$i<count($_POST['checkbox']);$i++){
      $checkbox = $_POST['checkbox'];
      $del_id = $checkbox[$i];
      $byebye = mysqli_query($con, "SELECT * FROM feature WHERE id='$del_id'") or die("File Delete Error: ".mysqli_error($con));
      while($rows = mysqli_fetch_array($byebye)){
	extract($rows);
	unlink("../images/features/".$pic);
      }
      $sql = "DELETE FROM pictures WHERE id='$del_id'";
      $result = mysqli_query($con, $sql) or die("Delete Error: ".mysqli_error($con));
    }
    // if successful redirect to delete_multiple.php 
    if($result){
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=feature-pics.php\">";
    }
  }
  mysqli_close();
  if(isset($_POST['activate'])){
    for($n=0;$n<count($_POST['active']);$n++){
      $live = $_POST['active'][$n];
      $pos = $_POST['pos'][$n];
      $act_id = $active[$n];
      $update = "UPDATE feature SET position='$pos', active='$live' WHERE id='$act_id'";
      $result = mysqli_query($con, $update) or die("Active Error: ".mysqli_error($con));
      echo "<br>".$live;  //Testing whats being sent
      echo "<br>".$pos;   //Testing whats being sent
    }
    // if successful redirect to delete_multiple.php 
    if($result){
      echo "" //meta refresh here, took out for testing
    }
  }
  mysqli_close();
  ?>
  <br>
  <input type="submit" name="activate" id="activate" value="Activate"> 
  <input type="submit" name="delete" id="delete" value="Delete Selected">
</form>
Link to comment
Share on other sites

update

 

got a few more things working, now its the order position not sending value to post.

 

new code:

    	<form method="post">
			<?php
            $pic_query = mysqli_query($con, "SELECT * FROM feature")
                or die("Pic_Query Failed: ".mysqli_error($con));
			
            while($row = mysqli_fetch_array($pic_query)){
                extract($row);
            ?>
                <div style="display:inline-block; margin: 4px; padding:5px; border:solid 1px #000000; border-radius:5px;">
                	<img src="../images/feature/<?php echo $pic ?>" width="200px">
                    <br>Active: <input type="checkbox" id="act[]" name="act[]" value="<?php echo $id ?>" <?php if($active=='true'){echo 'checked';} ?>>
                    <br>
                    Order: <select id="pos[]" name="pos[]">
						<option value="" <?php if($position==NULL){echo 'selected';} ?>></option>
                    	<option value="1" <?php if($position=='1'){echo 'selected';} ?>>1</option>
                        <option value="2" <?php if($position=='2'){echo 'selected';} ?>>2</option>
                        <option value="3" <?php if($position=='3'){echo 'selected';} ?>>3</option>
                        <option value="4" <?php if($position=='4'){echo 'selected';} ?>>4</option>
                        <option value="5" <?php if($position=='5'){echo 'selected';} ?>>5</option>
                    </select>
                    <br>Delete: <input type="checkbox" id="checkbox[]" name="checkbox[]" value="<?php echo $id; ?>">
                </div>
            <?php
            }
			if($_POST['activate']) { // from button name="delete"
		 	  $act = $_POST['act']; //from name="checkbox[]"
			  $pos = $_POST['pos'];
			  $countAct = count($_POST['act']);
			  
		      for($n=0;$n<$countAct;$n++) {
			    $act_id = $act[$n]; 
				$pos_val = $pos[$n];
				
				$sql = "UPDATE feature SET position='$pos_val', active='true' WHERE id='$act_id'";
		 		$result = mysqli_query($con, $sql) or die(mysqli_error($con));
			  }
			  if($result) {	
			  	print_r($pos_val); //test results
				echo $act_id."\n"; //test results
			  }else{
		 		echo "Error: ".mysqli_error($con);
			  }
			}
			if($_POST['delete']) { // from button name="delete"
		 	  $checkbox = $_POST['checkbox']; //from name="checkbox[]"
			  $countCheck = count($_POST['checkbox']);
		 
		      for($i=0;$i<$countCheck;$i++) {
			    $del_id  = $checkbox[$i];
				$del = "SELECT * FROM feature WHERE id='$del_id'";
				$delQuery = mysqli_query($con, $del) or die(mysqli_error($con));
				while($delrow = mysqli_fetch_array($delQuery)){
					extract($delrow);
				}
		 		unlink('../images/feature/'.$pic);
				$sql = "DELETE FROM feature WHERE id = $del_id";
		 		$result = mysqli_query($con, $sql) or die(mysqli_error($con));
			  }
			  if($result) {	
			    header('Location: feature-pics.php');
			  }else{
		 		echo "Error: ".mysqli_error($con);
			  }
			}
            ?>
            <br>
            <input type="submit" name="activate" id="activate" value="Activate">
            <input type="submit" name="delete" id="delete" value="Delete Selected">
        </form>
Edited by Guber-X
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.