Jump to content

Hidden form fields.


rvdveen27

Recommended Posts

Hello all, 

 

For an application form I'm trying to put together a form with some hidden fields that become visible based on earlier selection choices. I've looked up some info about this on the internet and it seems to be done with a javascript. So I've added a script in my code, played around with it a bit but I seem not able to get it to work. The second box seems to visible with me anyways. 

 

I hope one of you can help me in getting this to work. 

 

Code:

	<script>
		$(document).ready(function() {
		  $.viewMap = {
		    '0' : $([]),
		    'YES' : $('#YES'),
		    'NO' : $('#NO')
		  };

		  $('#viewSelector').change(function() {
		    // hide all
		    $.each($.viewMap, function() { this.hide(); });
		    // show current
		    $.viewMap[$(this).val()].show();
		  });
		});
	</script>
				Have you been part of any other VTC's before this application?<br>
				<select id="viewSelector">
					<option value="0">---Select one---</option>
					<option value="YES">Yes</option>
					<option value="NO">No</option>
				<div id="view1">
					If so, which VTC's have you been in and what was your reason for quitting?<br>
					<input type="text" name="otherVTC" class="form-control" placeholder="Example: <vtc name>: reason, <vtc name>: reason, etc." required><br />
				</div><br>

Thanks in advance!

Link to comment
Share on other sites

I don't see any element with class='viewMap'

 

Too be honest, in my inexperience I have no idea where that element should be? Like I said, I looked up how to do it and found this. Then like usually I fiddle around with it a bit to see if I can et it to work, this time that seemed not possible for me.

Link to comment
Share on other sites

try

<html>
<head>
<meta name="generator" content="PhpED 14.0 (Build 14039, 64bit)">
<title>Sample</title>
<meta name="creation-date" content="05/27/2015">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    $(document).ready(function() {
      $(".view").hide(); // hide all  

      $('#viewSelector').change(function() {
        // hide all
        $(".view").hide();
        // show current
        $("#"+$(this).val()).show();
      });
    });
</script>
</head>
<body>
    Have you been part of any other VTC's before this application?<br>
    <select id="viewSelector">
        <option value="0">---Select one---</option>
        <option value="YES">Yes</option>
        <option value="NO">No</option>
    </select>
    <div id="YES" class='view'>
        If so, which VTC's have you been in and what was your reason for quitting?<br>
        <input type="text" name="otherVTC" class="form-control" placeholder="Example: <vtc name>: reason, <vtc name>: reason, etc." required><br />
    </div><br>
    <div id="NO" class='view'>
        <p>Visible if NO selected</p><br />
    </div><br>
</body>
</html>

  • Like 1
Link to comment
Share on other sites

Thank you, that worked perfectly. 

 

Now I'm running into the following problem: 

 

With the code below I tried filling in the form and see if it enters the data correctly into the database, but I get an error instead saying: 

Failed to run query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'start' cannot be null

Now it seems for some reason it's trying to enter the data from the form into the drive_routes table instead of the applications table. But I have no idea why since the code (according to me) doesn't say anywhere that it should put the data into the drive_routes table?

		$query = " 
				INSERT INTO applications ( 
					user
					,steamprofile
					,age
					,timezone
					,description
					,playtime
					,othervtc
					,vtcreason
					,teamspeak
					,tsreason
					,aboutedim
				) VALUES ( 
					:user
					,:steamprofile
					,:age
					,:timezone
					,:description
					,:playtime
					,:othervtc
					,:vtcreason
					,:teamspeak
					,:tsreason
					,:aboutedim
				) 
			"; 
			
			 
			$query_params = array( 
				':driver' => $_SESSION['userid']
				,':steamprofile' => $_POST['steamprofile']
				,':age' => $_POST['age']
				,':timezone' => $_POST['timezone']
				,':description' => $_POST['description']
				,':playtime' => $_POST['playtime']
				,':othervtc' => $_POST['othervtc']
				,':vtcreason' => $_POST['vtcreason']
				,':teamspeak' => $_POST['teamspeak']
				,':tsreason' => $_POST['tsreason']
				,':aboutedim' => $_POST['aboutedim']

			); 
			 
			try 
			{ 
				$stmt = $db->prepare($query); 
				$result = $stmt->execute($query_params); 
			} 
			catch(PDOException $ex) 
			{ 
				die("Failed to run query: " . $ex->getMessage()); 
			} 
				header("Location: index.php"); 
				exit;
		}
Link to comment
Share on other sites

You are not giving the "start" column a value in the INSERT statement so it will try to give it a null value.

 

You must either

  • Give a value in the insert query, or
  • define a default non-null value in the table definition

edit: Columns defined as NOT NULL should have a default value.

Edited by Barand
Link to comment
Share on other sites

 

You are not giving the "start" column a value in the INSERT statement so it will try to give it a null value.

 

You must either

  • Give a value in the insert query, or
  • define a default non-null value in the table definition

 

 

There is no "start" column in the "applications" table. But the "start" column is in the "drive_routes" table, that's why I know it's trying to input the data in the wrong table. Therefor it shouldn't even do anything at all with a "start" column and I don't see where it's getting that from.

 

 

EDIT: Found the issue it was in: 

			<form class="form-signin" role="form" action="submitjob.php" method="post">

forgot to change that to application.php

 

Now I'm getting this error, which is even more unclear to me:

Failed to run query: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Edited by rvdveen27
Link to comment
Share on other sites

You are inserting a record into the drive_routes table.

That table has a column called "start"

Therefore it is trying to put something into that column. As you haven't specified what to put in there it will try to put NULL. But you have said it is not allowed to be null, hence the error.

 

So, as I have said,

  • give a value when you insert, or
  • define a default value.

 

Sorry but I don't know how to say it any clearer than that.

Edited by Barand
Link to comment
Share on other sites

You are inserting a record into the drive_routes table.

That table has a column called "start"

Therefore it is trying to put something into that column. As you haven't specified what to put in there it will try to put NULL. But you have said it is not allowed to be null, hence the error.

 

So, as I have said,

  • give a value when you insert, or
  • define a default default value.

 

Sorry but I don't know how to say it any clearer than that.

 

 

Yeah I understood, the point was, I should not be inserting a record into the drive_routes table. But anyways, see my edit, that's fixed now.. Now I'm having the next issue.

 

EDIT: Got it fully working now. The problem was 

':driver' => $_SESSION['userid']

which was supposed to be 

':user' => $_SESSION['userid']

I guess I overlooked this since I was rushed... (I'm trying to reach a deadline which is in 1,5 hours from now).

Edited by rvdveen27
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.