Jump to content

Breaking Up Forms For Query


noj75

Recommended Posts

Hi guys,

 

I have a form that has some jquery added to it so that my user can add new rows of date by adding more form fields. Here is the script:

 


<?php
ob_start();
include('connect.php');

$hID = $_REQUEST['ID'];
$hname = $_REQUEST['name'];

?>
<head>
<script language="Javascript" src="jquery.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var current = 1;

function addEntry() {
current++;
var strToAdd = '<tr><td><p><input id="eracedated'+current+'" name="eracedated'+current+'" size="5" /> <input id="eracedatem'+current+'" name="eracedatem'+current+'" size="5" /> <input id="eracetime'+current+'" name="eracetime'+current+'" size="5" /> <input id="eracetype'+current+'" name="eracetype'+current+'" size="15" /> <input id="eraceage'+current+'" name="eraceage'+current+'" size="5" /> <input id="eentdated'+current+'" name="eentdated'+current+'" size="5" /> <input id="eentdatem'+current+'" name="eentdatem'+current+'" size="5" /> <input id="eracetrack'+current+'" name="eracetrack'+current+'" size="15" /> <input id="eracedistance'+current+'" name="eracedistance'+current+'" size="5" /> <input type="hidden" id="ehid'+current+'" name="ehid'+current+'" value="<?php echo $hID; ?>" /><input type="hidden" id="ehorse'+current+'" name="ehorse'+current+'" value="<?php echo $hname; ?>" /></p></td></tr>'
$('#mainField').append(strToAdd)
}

$(document).ready(function(){
$('#addEntry').click(addEntry)
});
</script>
</head>
<style type="text/css">
body { background-color:#4a0303; }
</style>



<div style="background-color:#734e4e; border:2px solid #000000; color:#FFFFFF; padding:10px; width:770px;">

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td>
   <fieldset style="border:none;" id="mainField">
	  <p>
	  <input id="eracedated1" name="eracedated1" size="5" /> 
	  <input id="eracedatem1" name="eracedatem1" size="5" /> 
	  <input id="eracetime1" name="eracetime1" size="8" /> 
	  <input id="eracetype1" name="eracetype1" size="15" /> 
	  <input id="eraceage1" name="eraceage1" size="5" /> 
	  <input id="eentdated1" name="eentdated1" size="8" /> 
	  <input id="eentdatem1" name="eentdatem1" size="8" /> 
	  <input id="eracetrack1" name="eracetrack1" size="15" /> 
	  <input id="eracedistance1" name="eracedistance1" size="5" />
	  
	  <input type="hidden" id="ehid1" name="ehid1" value="<?php echo $hID; ?>" />
	  <input type="hidden" id="ehorse1" name="ehorse1" value="<?php echo $hname; ?>" /> 
	  </p>
   </fieldset>
   	</td>
  </tr>
  <tr>
  	<td>
   <p>
   <input type="button" id="addEntry" value="Add Another Entry">
   </p>
   	</td>
   </tr>
   <tr>
   	<td>
   <input type="submit" value="Upload Entries"> <input type="submit" name="cancel" onclick="window.parent.location.href=window.parent.location.href;" value="Cancel" />
   </td>
  </tr>
</table>   
</form>
</div>

 

If my user clicks the addentry button a new set of fields will appear so that they can add another row. Now comes the tricky part, how the hell do I process this with PHP to add each line as a record into my database?

 

It really is stumping me on how to do it. Any help, as always, is very much appreciated.

 

Kind regards

Link to comment
Share on other sites

Okay, change all those names in format of:

 

somename# to somename[]

 

So eracedated1 should be eracedated[] instead. Do that for all your inputs (both in your jQuery and in the pre-set form. Then you can remove the JavaScript reference to current.

 

In PHP, just use $_POST['eracedate'] to get all of them. It will be an array and you can use a foreach loop to get all the data.

 

When in doubt, use

var_dump($_POST['eracedate']);

to see what's in the array.

 

Good luck!

Link to comment
Share on other sites

>:( That is so annoying! I tried that and it wasnt working because I didnt set the array [] to the javascript names!

 

LOL Thanks very much mate.

 

Now, how would I store each form set into the database? i.e so they insert into seperate rows?

 

Sorry to be a pain!

 

 

Link to comment
Share on other sites

Just loop through the array and run mysql_query on each one. Since you have so many, I'll just show a preview.

 

// do all your validation up here i.e.: the form is submitted and the fields aren't empty, etc.
$arr1 = $_POST['eracedate'];
// get the rest of the $_POST data...

// since they are in blocks, I can assume they're all of the same length, so looping through one count is fine.
$arrlen = count($arr1);
foreach ($e=0; $e < $arrlen; $e++) {
     $arr1_val1 = $arr1[$e];
     // same for the rest of the arrays
     mysql_query(...) or trigger_error(mysql_error());
}

Link to comment
Share on other sites

Sorry Ken, I didnt explain myself enough.

 

$rdated = $_REQUEST['eracedated'];
$rdatem = $_REQUEST['eracedatem'];
$rtime = $_REQUEST['eracetime'];
$rtype = $_REQUEST['eracetype'];
$rage = $_REQUEST['eraceage'];
$redted = $_REQUEST['eentdated'];
$redtem = $_REQUEST['eentdatem'];
$rtrack = $_REQUEST['eracetrack'];
$rdist = $_REQUEST['eracedistance'];
$rhid = $_REQUEST['ehid'];
$rhorse = $_REQUEST['ehorse'];

 

That is what I want in one row on my database, not individually. Does that make sense?

Link to comment
Share on other sites

Hi Ken,

 

Yep, I got it now. The problem I was having was that when I used FOREACH nothing was happening. I changed it to FOR and it works a treat:

 

$rdated = $_REQUEST['eracedated'];
$rdatem = $_REQUEST['eracedatem'];
$rtime = $_REQUEST['eracetime'];
$rtype = $_REQUEST['eracetype'];
$rage = $_REQUEST['eraceage'];
$redted = $_REQUEST['eentdated'];
$redtem = $_REQUEST['eentdatem'];
$rtrack = $_REQUEST['eracetrack'];
$rdist = $_REQUEST['eracedistance'];
$rhid = $_REQUEST['ehid'];
$rhorse = $_REQUEST['ehorse'];

$arrlen = count($rdated);
for($e=0; $e < $arrlen; $e++) {
     $rdated1 = $rdated[$e];
     $rdatem1 = $rdatem[$e];
     // same for the rest of the arrays
    print $rdated1.' '.$rdatem1.'<br>';
}

 

All I need to do now is simply change the print to an sql query and roberts your fathers brother!

 

Thanks for the help Ken, you are a star!

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.