4 Replies - 314 Views - Last Post: 11 March 2014 - 02:48 AM Rate Topic: -----

#1 Mr_P  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-July 13

Coding help needed for Multiple input

Posted 10 March 2014 - 08:33 AM

I have code at the moment where is a user input multiple items into a text input separated by a comma, this gets submitted to php backend and using explode, I can retrieve the correct values and process them back through jquery.

Here is my problem. I need to find a way to validate input using several input boxes. The maximum would be 8 and I am struggling to find a way to validate the data, using my existing code. Firebug states the the POST is 2 separate box_add posts. I cannot use [] because that will interfere with validator. I would be grateful if someone could show me the correct way to code this. Many Thanks

HTML code

<a href="#" id="INTKAddMoreFileBox" class="btn btn-info">Add More Box Fields</a>
	<div id="INTKInputsWrapper">
	   <div><input name="box_add" type="text" id="box_add" />
              <a href="#" class="removeclass">&times;</a><a style="margin-left: 14px;" href="javascript:void(0)" title="Just an example" class="tooltip">Help</a>
           </div>
       </div<


PHP code

<?php
	session_start();
	
	$connect = mysql_connect("localhost", "root", "") or die ('{"opp":"error","box":"mysql_connect FAILED"}');
	
	$db = mysql_select_db("sample");


	// test vars from jquery form
	$status = mysql_real_escape_string($_REQUEST['status']);
	$company = mysql_real_escape_string($_REQUEST['company']);
	$requested = mysql_real_escape_string($_REQUEST['requested']);
	$activity = mysql_real_escape_string($_REQUEST['activity']);
	$address = mysql_real_escape_string($_REQUEST['address1']);
	$service = mysql_real_escape_string($_REQUEST['service']);
	$destroyDate = mysql_real_escape_string($_POST['datepicker']);
	$date = explode('/', $_POST['datepicker']);
	$newdate = $date[2].'-'.$date[1].'-'.$date[0];
	//$box = mysql_real_escape_string($_REQUEST['box_add']); // never used
	$authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
	$dept = mysql_real_escape_string($_REQUEST['dept']);
	$boxerrortext = 'Error';

	// Split the box if multiples
	$array = explode(',', $_REQUEST['box_add']);
	
	$outString = ''; 
	
	foreach ($array as $box) {
		$box = mysql_real_escape_string($box);
		$sql = "SELECT item FROM act WHERE item = '$box'";
		$result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}');

		// If there are dupe entries, send message to jquery
		if (mysql_num_rows($result) > 0) {
		
			$outString .= $box . ' ';		
	} 	
	}
	if ($outString) {
		$error = array('opp' => "error", 'box' => $outString); 
	
		$output = json_encode($error);
		echo $output;
		exit();
	}

	foreach ($array as $box) {
		$outString .= "<br />company:  $company <br /> address: $address <br />service: $service <br />Destroy Date: $destroyDate <br />box: $box <br />";

		$box = mysql_real_escape_string($box);
		$sql = "INSERT INTO `temp` (service, activity, department, company, address, user, destroydate, date, item, new)";
		$sql .= "VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', '$newdate', NOW(), '$box', 1)";
		$result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}');
	}

	$json = array('opp' => 'insert', 'box' => $outString);
	$result = json_encode($json);
	echo $result;
?>


Script to add inputs

<script type="text/javascript">
$(function() {

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#INTKInputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#INTKAddMoreFileBox"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(InputsWrapper).append('<div><input type="text" name="box_add" id="box_add" /><a href="#" class="removeclass">&times;</a></div>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
        if( x > 1 ) {
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
        }
return false;
}) 

});
</script>


Is This A Good Question/Topic? 0
  • +

Replies To: Coding help needed for Multiple input

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,505
  • Joined: 12-December 12

Re: Coding help needed for Multiple input

Posted 10 March 2014 - 11:13 AM

<input name="box_add" type="text" id="box_add" />

Element ids must be unique on the page. You could add a number to get distinct ids, "box_add1", "box_add2", etc., or, if you don't need the ids, then just remove them.

You post data to PHP as an array using square-brackets and the same name "box_add[]". The HTML validator probably won't like this, but it is necessary for PHP.

You can interate the array in PHP using:

$boxes = $_POST['box_add'];

foreach ( $boxes as $box ) {

It is preferable to use $_POST or $_GET as appropriate rather than $_REQUEST.

Note that the mysql extension library is deprecated - see my signature below.
Was This Post Helpful? 0
  • +
  • -

#3 Mr_P  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-July 13

Re: Coding help needed for Multiple input

Posted 10 March 2014 - 12:54 PM

Thank you Andrew for your comments. Is there a way round the validator issue. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,505
  • Joined: 12-December 12

Re: Coding help needed for Multiple input

Posted 10 March 2014 - 01:26 PM

If you mean the HTML Validator then I'll guess that you can tweak what it reports as errors or warnings, but this won't help if someone else validates the page.

So, no, you can't get around it. What is required by PHP isn't always valid HTML. The validator is useful but 99% of all existing web-pages won't get through it without at least one warning or error - my own estimate :)
Was This Post Helpful? 0
  • +
  • -

#5 Mr_P  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-July 13

Re: Coding help needed for Multiple input

Posted 11 March 2014 - 02:48 AM

The way I got round it andrew, was to do this. Works like a charm and takes care of the dynamic problem with validator. Thanks

 name="box_add[]'+FieldCount+'"

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1