Jump to content

Frontend Form Using Multiselect Chekboxes . How to save it to db?


Recommended Posts

Here is my frontend form selecting multiple items.  OFFERED ITEMS AND REQUESTED ITEMS

image.thumb.png.e22fe3c60ea62b3009ea6d4cf333bedc.png

here is my admin tree

image.png.7e712f8a303b104183391565da0896aa.png

 

 

inside of the trade template admin panel i am using asm select.

image.thumb.png.f7750437bc289b6341eb6b0f6f949174.png

 

What i trying to do is when user select items from frontend form. I want to save that into trade page items_offered and items_requested asmselect as shown in the screen shot.

i can do it for robloxusername with this code

if ($input->post('robloxusername')) {

 $p = new Page();
$p->template = 'trade'; //or whatever your template is called
$p->parent = wire('pages')->get('/trades/'); //or whatever the parent page is called 
$p->title = 'Form submission from ' . date('Y-m-d H:i');
$p->robloxusername = $input->post->text('robloxusername');


  $p->save();

how can i do it to checkboxes ?

Here is my form html

 <form method="POST" action="<?php echo $page->url; ?>" id="id01" class=" w3-container  w3-black w3-text-light-grey w3-margin">
	
	
	
 <span onclick="document.getElementById('id01').style.display='none'" 
        class="w3-button w3-text-white w3-xlarge w3-display-topright">&times;</span>
  
  
  
  
			<div class="w3-row-padding w3-left w3-card w3-dark-grey w3-round w3-margin "  >
            	<h2 class="w3-center">WHAT DO YOU OFFER? SELECT MAX 4</h2>
				<?php $x=1?>
				<?php foreach  ($pages->get('/en/items')->children as $r) {?>
					<div  class="w3-center w3-col m3 l2 s4  w3-round  " >
						<div class="cont-checkbox">
							<input class="w3-check" type="checkbox" id="x<?php echo $x;?>" />
							<label for="x<?php echo $x;?>">
								<img class="responsive" src="<?php echo $r->image->url;?>">
									<span class="cover-checkbox">
									  <svg viewBox="0 0 12 10">
										<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
									  </svg>
									</span>
									<div class="info">	<?php echo $r->title;?></div>
							</label>
						</div>
					</div>
				<?php $x=$x+1;}	?>

			</div>




  
			<div class="w3-row-padding w3-left w3-card w3-dark-grey w3-round w3-margin "  >
            	<h2 class="w3-center">WHAT DO YOU REQUEST? SELECT MAX 4</h2>
				<?php $a=1?>
				<?php foreach  ($pages->get('/en/items')->children as $m) {?>
					<div  class="w3-center w3-col m3 l2 s4  w3-round  " >
						<div class="cont-checkbox">
							<input class="w3-checkreq" type="checkbox" id="a<?php echo $a;?>" />
							<label for="a<?php echo $a;?>">
								<img class="responsive" src="<?php echo $m->image->url;?>">
									<span class="cover-checkbox">
									  <svg viewBox="0 0 12 10">
										<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
									  </svg>
									</span>
									<div class="info">	<?php echo $m->title;?></div>
							</label>
						</div>
					</div>
				<?php $a=$a+1;}	?>

			</div>

 

	<div class="w3-col m12" >
	    <div class="w3-container "  >
			<div class="w3-row-padding w3-card w3-dark-grey w3-round w3-margin "  >
			 <input class="w3-input w3-border w3-xlarge " name="robloxusername" type="text" placeholder="ENTER ROBLOX USERNAME">
			</div>
		</div>
	</div>


	<div class=" w3-center">
	<input type="submit" value="Send" class="w3-button w3-xlarge w3-center w3-section w3-blue w3-ripple w3-padding"/>
		
	</div>
</form>

 

Link to comment
Share on other sites

You'd need to add name to your checkboxes like (example only with offers):

<input name="offers[]" value="<?=$m->name?>" class="w3-checkreq" type="checkbox" id="a<?php echo $a;?>" />

Then on form processing you'd need to do sth like this:

// sanitize array input
$offers = $input->post->array('offers', 'pageName')
foreach($offers as $offer){
	// do your saving or whatever;
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...