How to make multiple checkbox values for a form sticky

Hello again everyone .

i am new to PHP but steadily improving my skills. i am building my first application and hope to complete it within a week.

i have two questions to ask. But before i start , i first i wish to thank everyone at sitepoint- particularly Anthony Sterling for their kind support and help.


QUESTION ONE

HOW TO MAKE MULTIPLE CHECKBOX VALUES IN A FORM STICKY

i am building a sticky multi checkbox value for a form.

i have adapted a function that was previously given to me at sitepoint but i can’t seem to get it to work.




$jobs = ''; 




if(isset($_POST['submit']))
  {

           require_once ('db_fns.php'); // Connect to the db.



$job_sought = $_POST['job_sought'];
foreach ($job_sought as $job) 
	{$source .= $jobs.", ";}
	$jobs_sought = substr($source, 0, -2);




		
		$q3 = "INSERT INTO typeworksought ( user_id, job_sought  ) 
		VALUES ( '$id','$jobs_sought' )";		

	
		$R3 = mysqli_query ($dbc, $q3); // Run the query.      


}




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

	
	 $job = 'job';  
		
		
	function selected($job){
		  
		  if(empty($_POST['job'])){
		    return;
		  }
		  
		  if(!is_array($_POST['job'])){
		    return;
		  }
		  
		  if(!in_array($job, $_POST['job'])){
		    return;
		  }
		  
		  return 'selected="selected"';
		}




   
   if(isset($_POST['job_sought'])){  
   	   
     echo
      	' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"  
          '.selected($job);'    /> job' ; 
  		 }
          else
	  
             { 
          
          echo '<input type="checkbox" name="job_sought[]" value="job"   /> job'; 

          	
          } 
          
         
</form>            




Two questions regarding my script.
1; any ideas why it is not sticky.

2; is there a better formula for a checkbox sticky form. my concern with my function is that it will be cumbersome if i have many checkboxes in a category. it will means that i will have to draft the same function for each of the checkboxes.

i want to insert all the full values of the submitted checkboxes into the mysql database so that those values can be indexed and searched upon later on

QUESTION TWO;

How do i maintain a session ID in non PHP pages

i have build a website comprising php and html pages. for all the PHP pages i have placed a

   
session_start(); 


at the top of the page.

i did this to maintain a session ID for the user. however, the site also contains non php pages, i.e html. the problem is that if a user goes to the Html page and then want to come back to the php page later on they will lose their session Id.

is there a way that i can maintain their session ID even when they are in non php pages? is it possible? or most i convert all the pages to PHP.

thank you everyone for your kind assistance.

sweet thanks

Andreea

1: ‘sticky’. This word means nothing to me. Please define it? You use it like it’s got context.

2: Make all the pages .php and stick <?php session_start(); ?> at the top of them. (You can also set apache to parse .html through the PHP engine, but that’s a non-ideal solution IMO.) PHP cant maintain a session if it isn’t run.

Hello Starlion

i am sorry. i thought that sticky forms was a standard expression.

what i mean by sticky is that the form retains the information that the user has clicked.

for example. if i have 5 options in a check box value and I click one of those options then those options will be retained and showed by the form if the form is returned to the user because he did not click all the questions.

the start of the thread is in the link below. you can see how and where i got the original function from.

i hope this helps. once again deepest apologies for making up a word

kind regards

Andreea

Ahhh okay. Returning the same data. Gotcha.

Well, lets take a look at your code. I’m going to condense the blank lines out of it for the sake of my eyes.


   if(isset($_POST['job_sought'])){  
     echo ' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"          '.selected($job);'    /> job' ; 
           }

(we’ll ignore the ELSE for now, though that’s got issues)
EDIT: Er. Blind me. Okay…

          if(empty($_POST['job'])){
            return;
          }

I see no field for job in your form. I see one for job_sought, but not job… so $_POST[‘job’] is not set, so it is returning.

hi.

thank you for your speedy responce.

i am a little bit confused at your answer though. ( sorry but i am still qyou

sorry but i am still quite new to PHP.

how are you able to return ;

if(empty($_POST[‘job’])){
return;
}

i wold have thought that the jobs was simply an element in an array of

 &lt;input type="checkbox"  name="job_sought[]" value= "'.$job.'"          '.selected($job);'    /&gt; job' 

i therefore dont understand how i am therefore able to return a single element of that post array .

kind regards

Andreea

Okay. Crash course on form handling.

<form action= " " method=“post” name=“form1” >
<input type=“checkbox” name=“job_sought[]” value= “‘.$job.’” ‘.selected($job);’ /> job’

Method=‘post’ says that the variables will be put into the $_POST variable on submission.
the NAME of each input defines the index of $_POST that will be used.

So in this case, your form will populate $_POST[‘job_sought’] (which will be an array, because of the additional 's). It’s not populating $_POST[‘job’], which is what the function is looking for.
Change all of the $_POST[‘job’] to $_POST[‘job_sought’] in that function declaration, and it should work.

hello again

thank you taking the time out to do a quick crash course. i understand the basic concept and have indeed scripted a large form. the only issue that i am stuck with is to return the values for these checkboxes.

i have followed your instructions but it is still not returning the values to the form. i have enclosed below the full script. i would be very grateful if you could tell me where i have gone wrong with it ( as far as i can make out, it looks correct and should return the values to the form).

 		


$jobs = ''; 
if(isset($_POST['submit']))


  {           require_once ('db_fns.php'); // Connect to the db.
 
$job_sought = $_POST['job_sought'];

foreach ($job_sought as $job) 

    {$source .= $jobs.", ";}

    $jobs_sought = substr($source, 0, -2);

        $q3 = "INSERT INTO typeworksought ( user_id, job_sought  ) 

        VALUES ( '$id','$jobs_sought' )";        

        $R3 = mysqli_query ($dbc, $q3); // Run the query.      

}

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

	 $job = 'job';  
		
		
	function selected($job){
		  
		  if(empty($_POST['job_sought'])){
		    return;
		  }
		  
		  if(!is_array($_POST['job_sought'])){
		    return;
		  }
		  
		  if(!in_array($job, $_POST['job_sought'])){
		    return;
		  }
		  
		  return 'selected="selected"';
		}

		
   
   

   if(isset($_POST['job_sought'])){  

          

     echo

          ' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"  

          '.selected($job);'    /> job' ; 

           }

          else

      

             { 

          

          echo '<input type="checkbox" name="job_sought[]" value="job"   /> job'; 


          } 

   


		

Try doing this:


    function selected($job){
          
          if(empty($_POST['job_sought'])){
            return '3';
          }
          
          if(!is_array($_POST['job_sought'])){
            return '2';
          }
          
          if(!in_array($job, $_POST['job_sought'])){
            return '1';
          }
          
          return 'selected="selected"';
        }

and see what number it puts into your form. At least then we’ll know which of the if conditions are failing.

HI

sorry to bother you again. i seem to be having an terrible time of it.

it did not return any values to the form. furthermore, if i don’t tick the box in the browser it return an error message. the error reads

Undefined index: job_sought

do you think my function for this whole thing is incorrect. is there a better way to make the form return these values