Jump to content

Recommended Posts

I'm trying to create a dynamic form that is made up of dynamically created check boxes.

 

I can't figure out why I'm getting the following error message:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

Below is my code, if anyone has any idea or suggestions as to how I can get my code to work, please share them. Thanks.

$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType="."'".Domestic."'";
$domesticGoodsResult = mysql_query($domesticGoods, $connected);

$internationalGoods = "SELECT categoryName FROM categories WHERE categoryType=". "'".International."'";
$internationalGoodsResult = mysql_query($internationalGoods, $connected);

echo <<<END
<form name="categoryRegister" id="categoryRegister" method="post" onsubmit="return categoryRegistration()">
<table width="600" border="0" cellpadding="10" cellspacing="0">
<tr>
    <td>
        while($row = msql_fetch_assoc($domesticGoodsResult)){
            <center><input type="checkbox" name=$row['categoryName'] id=$row['categoryName'] value=$row['categoryName']/>$row['categoryName']</center><br/>
        }
    </td>
    <td>
        while($row = msql_fetch_assoc($internationalGoodsResult)){
            <center><input type="checkbox" name=$row['categoryName'] id=$row['categoryName'] value=$row['categoryName']/>$row['categoryName']</center><br/>
        }
    </td>
</tr>
<tr>
    <td colspan="2"><center><input type="submit" name="submit" value="Sign-Up"/></center>
    <input type="hidden" name="type" value="goodsInfo" />
    </td> 

Link to comment
https://forums.phpfreaks.com/topic/133129-trouble-with-form-creation/
Share on other sites

You are mixing PHP with HTML and php cannot parse that you need to exit in and out of PHP or echo the html out.

 

<?php
$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType='Domestic' OR categoryType='International' ";
$domesticGoodsResult = mysql_query($domesticGoods, $connected);

$internationalGoods = "SELECT categoryName FROM categories WHERE categoryType='International'";
$internationalGoodsResult = mysql_query($internationalGoods, $connected);

echo '<form name="categoryRegister" id="categoryRegister" method="post" onsubmit="return categoryRegistration()">
<table width="600" border="0" cellpadding="10" cellspacing="0">
<tr>
    <td>';
        while($row = msql_fetch_assoc($domesticGoodsResult)){
            echo '<center><input type="checkbox" name= ' . $row['categoryName'] . ' id=' . $row['categoryName']  . ' value=' . $row['categoryName'] . ' />' . $row['categoryName'] . '</center><br/>';
        }
    echo '</td>
    <td>';
        while($row = msql_fetch_assoc($internationalGoodsResult)){
             echo '<center><input type="checkbox" name= ' . $row['categoryName'] . ' id=' . $row['categoryName']  . ' value=' . $row['categoryName'] . ' />' . $row['categoryName'] . '</center><br/>';
        }
echo '
    </td>
</tr>
<tr>
    <td colspan="2"><center><input type="submit" name="submit" value="Sign-Up"/></center>
    <input type="hidden" name="type" value="goodsInfo" />
    </td>';
?>

 

Edit also assumed that you wanted it to be just "International" or "Domestic" and changed that portion as well.

$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType="."'".Domestic."'";

 

That needs to be either:

 

$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType='$Domestic'";

 

or

 

$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType='Domestic'";

 

depending on what you are trying to do.

 

And you need to end your heredoc with

END;

Here are some updates to your code.  However, like premiso said, you are mixing PHP and HTML and you cannot do that.

 

<?php
$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType='Domestic'";
$domesticGoodsResult = mysql_query($domesticGoods, $connected);

$internationalGoods = "SELECT categoryName FROM categories WHERE categoryType='International'";
$internationalGoodsResult = mysql_query($internationalGoods, $connected);

?>
<form name="categoryRegister" id="categoryRegister" method="post" onsubmit="return categoryRegistration()">
<table width="600" border="0" cellpadding="10" cellspacing="0">
<tr>
    <td>
		<?php
        while($row = msql_fetch_assoc($domesticGoodsResult)){
				?>
            <center>
						<input type="checkbox" name="<?php echo $row['categoryName'] ?>" id="<?php echo $row['categoryName']?>" value="<?php echo $row['categoryName'] ?>" />
						<?php echo $row['categoryName'] ?>
					</center><br/>
				<?php
			}
		?>
    </td>
    <td>
		<?php
        while($row = msql_fetch_assoc($internationalGoodsResult)){
				?>
            <center>
						<input type="checkbox" name="<?php echo $row['categoryName'] ?>" id="<?php echo $row['categoryName']?>" value="<?php echo $row['categoryName'] ?>" />
						<?php echo $row['categoryName'] ?>
					</center><br/>
				<?php
			}
		?>
    </td>
</tr>
<tr>
    <td colspan="2"><center><input type="submit" name="submit" value="Sign-Up"/></center>
    <input type="hidden" name="type" value="goodsInfo" />
    </td>

Thanks premiso and flyhoney for the responses. The problem, as you two correctly pointed out, was with the improper intermingling of php and html. I intended to use this form with my AJAX code, that is why I needed to echo/output the form.

 

My focus was on making sure this dynamic form correctly produced the checkboxes and so I overlooked the important rule that php can't parse a heredoc with combined php and html.

I'd like to check this dynamic form to see if it properly creates checkboxes and the selected checkboxes have their values assigned to an array. I've put together some code to perform that check, but it is unable to:

 

1. Determine which (if any) checkboxes are selected.

2. Assign the selected checkbox value to an array.

 

At this point I'm not sure if the form code is wrong or the code for checking the form is wrong.

Below is my code. If anyone could tell me what is wrong with the verifying portion of the code or the dynamic form it would be appreciated. Thanks.

 

<html>
<head>
<?php
$domesticGoods = "SELECT categoryName FROM categories WHERE categoryType='Domestic'";
$domesticGoodsResult = mysql_query($domesticGoods, $connected);

$internationalGoods = "SELECT categoryName FROM categories WHERE categoryType='International'";
$internationalGoodsResult = mysql_query($internationalGoods, $connected);

echo '<form name="categoryRegister" id="categoryRegister" method="post">
<table width="600" border="0" cellpadding="10" cellspacing="0">
<tr>
    <td>';
        while($domesticRow = msql_fetch_assoc($domesticGoodsResult)){
            echo '<center><input type="checkbox" name= "' . $domesticRow['categoryName'] . '" id="' . $domesticRow['categoryName']  . '" value="' . $domesticRow['categoryName'] . '" />' . $domesticRow['categoryName'] . '</center><br/>';
        }
    echo '</td>
    <td>';
        while($interRow = msql_fetch_assoc($internationalGoodsResult)){
             echo '<center><input type="checkbox" name= "' . $interRow['categoryName'] . '" id="' . $interRow['categoryName']  . '" value="' . $interRow['categoryName'] . '" />' . $interRow['categoryName'] . '</center><br/>';
        }
echo '
    </td>
</tr>
<tr>
    <td colspan="2"><center><input type="submit" name="save" value="save"/></center>';
</head>

<body>
if(isset($_POST['save'])){
while($domesticRow = mysql_fetch_assoc($domesticCategoriesResult)){
	if(isset($_POST[$domesticRow['categoryName']])){
		$domesticCategoryArray[] = $domesticRow['categoryName'];
	}
}
while($interRow = mysql_fetch_assoc($interCategoriesResult)){
	if(isset($_POST[$interRow['categoryName']])){
		$interCategoryArray[] = $interRow['categoryName'];
	}
}
$domesticCategoryString = implode("," , $domesticCategoryArray);
echo $dom = ($domesticCategoryString != "") ? 'NOT EMPTY' : 'Product String EMPTY';
$interCategoryString = implode("," , $interCategoryArray);
echo '<br/>';
echo $inter = ($interCategoryString != "") ? 'NOT EMPTY' : 'Service String EMPTY';
$categoryList = $domesticCategoryString . $interCategoryString;
?>
</body>
</html>    

You should probably use checkbox arrays.  That might be easier.  Here is an example of how that is accomplished

 

<?php
if ($_POST['action'] == 'Go') {
    $domestic = $_POST['domestic'];
    print_r($domestic);
    // the above will print an array, and the items in the array are all the values that were checked
}
?>
<form>
    <input type="checkbox" name="domestic[]" value="value 1" />Value 1
    <input type="checkbox" name="domestic[]" value="value 2" />Value 2
    <input type="submit" name="action" value="Go" />
</form>

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.