Jump to content

Getting "parse error" Any ideas?


sonnieboy

Recommended Posts

Greetings experts,

 

I am trying to give our users the ability opportunity to preview their data before submitting to the database.

 

So, we have contacts.php with the following:

                       <div class="bs-example">
						<form class="form-inline" action="<?php echo get_option('siteurl'); ?>/form/preview.php" id="contactForm" role="form" method="post">
                         <div class="form-group">
                             <label  for="employeename">Employee Name</label><br>
								<input type="text" name="employeename" id="employeename" style="width:375px;"  placeholder="your name..." class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
				            </div>
		                    <div class="form-group">
                             <label for="ttitle">Title</label><br>
								<input type="text" name="ttitle" id="ttitle" style="width:375px;"  placeholder="Your title..." class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
							</div><br><br>

    <script id="row-template" type="text/x-handlebars-template">
	<div>
		  <!--reseed attribute IDs in case of gap resulting from deletions -->
	     <input type="hidden" name="rowIDs" value="{{rowNumber}}" />
	    <div class="form-group">

								<input type="text" name="sourcename1{{rowNumber}}" id="sourcename1{{rowNumber}}" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
						    </div>
		                    <div class="form-group">
								<input type="text" name="sourceaddress1" id="sourceaddress1{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
							</div>
	                        <div class="form-group">
								<input type="text" name="income1{{rowNumber}}" id="income1{{rowNumber}}" style="width:250px;"  class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
	    </div>
	    <input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
	</div>
	</script>
	<div id="addrow">
	    <div>
	    <!--reseed attribute IDs in case of gap resulting from deletions -->
	       <input type="hidden" name="rowIDs" value="{{rowNumber}}" />
	    <div class="form-group">
	        <label for="sourcename1">Name</label><br>
								<input type="text" name="sourcename1" id="sourcename1" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
						    </div>
		                    <div class="form-group">
		                	    <label for="sourceaddress1">Address</label><br>
								<input type="text" name="sourceaddress1" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
							</div>
	                        <div class="form-group">
	                          <label for="income1">Income</label><br>
								<input type="text" name="income1{{rowNumber}}" id="income1" style="width:250px;"  class="form-control" value="" class="required requiredField" />
								<?php if($nameError != '') { ?>
									<span class="error"><?=$nameError;?></span>
								<?php } ?>
	                <input type="button" value="Add More" rel="add-row" />
	        </div>
	    </div>
	</div><br><br>

There is a whole lot more code than this. I am just trying to simplify.

 

When the user clicks submit, s/he is taken to the preview.php code with following:

<?php
echo "<p>Employee Name: <b>" .$_POST["employeename"]. "</b></p>";
echo "<p>Title: <b>" .$_POST["ttitle"]. "</b></p>";
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
 echo "Source Name: <b>" $sourcename1 = $_POST['sourcename1' . $id] "</b></p>";

 echo "Source Address: <b> <b>" $sourceaddress1 = $_POST['sourceaddress1' . $id] "</b></p>";
 echo "Income Source:" $income1 = $_POST['income1' . $id] "</b></p>";
}
echo "<input type='hidden' name='employeename[]' value='".$sourcename1."' />";
echo "<input type='hidden' name='ttitle[]' value='".$sourceaddress1."' />";
echo "<input type='hidden' name='sourcename1[]' value='".$income1."' />";

Right now, I am getting an error on the preview.php that says:

 

Parse error: in C:\forms\preview.php on line 2

 

​which is this line:

echo "<p>Employee Name: <b>" .$_POST["employeename"]. "</b></p>";

​It might be obvious but I can't see it.

 

Can you please help?

 

Thanks in advance
 

Edited by sonnieboy
Link to comment
Share on other sites

What is the exact error? Please copy/paste it here.

 

EDIT: FYI, are you aware that you are using the exact same variable for all form fields ($nameError) for determining field level errors?

<?php if($nameError != '') { ?>
    <span class="error"><?=$nameError;?></span>
<?php } ?>

Also, Instead of littering the markup (HTML) with PHP logic, create PHP variables at the top for the error messages, then just output those variables in the markup. E.g.

 

//PHP code at top of script (hint, you could create a function to pass the error message to and have it return back the formatted HTML).

$nameErrorOutput = ($nameError != '') ? "<span class='error'>{$nameError}</span>" : '';
$otherfield1ErrorOutput = ($otherfield1Error != '') ? "<span class='error'>{$otherfield1Error}</span>" : '';
$otherfield2ErrorOutput = ($otherfield2Error != '') ? "<span class='error'>{$otherfield1Error}</span>" : '';

//HTML markup at bottom of script

<div class="form-group">
    <label  for="employeename">Employee Name</label><br>
    <input type="text" name="employeename" id="employeename" style="width:375px;"
           placeholder="your name..." class="form-control" value="" class="required requiredField" />
    <?=$nameErrorOutput?>
</div>
Edited by Psycho
Link to comment
Share on other sites

Thank you sir for your prompt response.

 

I did - > Parse error: in C:\forms\preview.php on line 2

 

However, I have made some changes since I posted the issue here and that error seemed to have been resolved but not completely.

 

Now, I am getting the following error:

 

Invalid argument supplied for foreach() in C:\forms\preview.php on line 5

 

Here is the latest code after some changes:

<?php
if(isset($_POST['employeename'])) echo $_POST['employeename'];
if(isset($_POST['ttitle'])) echo $_POST['ttitle'];
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
 echo $sourcename1 = $_POST['sourcename1' . $id];
 echo $sourceaddress1 = $_POST['sourceaddress1' . $id];
 echo $income1 = $_POST['income1' . $id];
}
echo "<input type='hidden' name='employeename[]' value='"$sourcename1"' />";
echo "<input type='hidden' name='ttitle[]' value='".$sourceaddress1."' />";
echo "<input type='hidden' name='sourcename1[]' value='".$income1."' />";
?>

The ROWIDs declaration is based on the assumption that some users may click a button to add additional textbox rows and we need to make sure all their data is captured.

 

Again, thanks a lot.

Link to comment
Share on other sites

Thank you sir for your prompt response.

 

I did - > Parse error: in C:\forms\preview.php on line 2

 

A parse error will typically provide more information than that, such as 

 

 

Parse error: syntax error, unexpected '$importFolder' (T_VARIABLE) in C:\xampp\htdocs\escalations\index.php on line 8

 

 

Now, I am getting the following error:

 

Invalid argument supplied for foreach() in C:\forms\preview.php on line 5

. . . 

The ROWIDs declaration is based on the assumption that some users may click a button to add additional textbox rows and we need to make sure all their data is captured.

 

You are trying to use the "rowIDs" POST value as an array on the foreach line. You have defined two fields as "rowID" on your form - but they are not an array. They are instead two separate single entity fields. What will happen in the post data is that the second field's value will overwrite the first field's value. If you want an array to be returned you need to define the fields with array names (note the "[]" in the field name).

 

<!--reseed attribute IDs in case of gap resulting from deletions -->
     <input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />

 

Since you are having problems with the POST data names/indexes and the logic to use them, add this on the action page (for debugging purposes) so you can SEE what is being passed.

 

echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
Link to comment
Share on other sites

That makes perfect sense.

 

Thanks a lot sir.

 

Now, the errors are gone. I don't know yet what the output will look like but at least the errors there are gone.

 

Now, I getting it on line 10 which is starts from here:

 

 

These are hidden fields that will be passed to submit.php to submit to the database:


echo "<input type='hidden' name='employeename[]' value='".$sourcename1."' />";
echo "<input type='hidden' name='ttitle[]' value='".$sourceaddress1."' />";
echo "<input type='hidden' name='sourcename1[]' value='".$income1."' />";

Parse error: in C:\xampp\htdocs\disclosures\forms\preview.php on line 10

 

Do I add the DEBUG... at top (of preview) page?

 

Yes, I added it to the top and it seems to be showing me what I am doing wrong on the markup.

 

Let me review that and post back.

 

Many thanks for your help.

 

 

 

 

 

 

Edited by sonnieboy
Link to comment
Share on other sites

Ok, I resolved that already. All I needed to do was refresh and start all over.

 

But I am getting way too many errors:

 

Notice: Array to string conversion in C:\xampp\htdocs\disclosures\forms\preview.php on line 3
Array
Notice: Array to string conversion in C:\xampp\htdocs\disclosures\forms\preview.php on line 4
Array
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\disclosures\forms\preview.php on line 6

Link to comment
Share on other sites

OK, the Invalid argument supplied error is gone.

 

It is probably due to empty string being passed.

 

I was able to do this:

if (is_array($rowIDs) || is_object($rowIDs))
{
for each(...)
...
...
)

Now, I got to figure our thw Array to string conversion error and the other error.

Link to comment
Share on other sites

You need to slow down. All of these drive-by posts are going to make people (i.e. me) decide not to help. If you get an error - read it. What do you think the error means? Then look at the code in question and determine what may be going on. An "array to string conversion" is pretty self explanatory. You are referencing a variable that is an array and you are performing some function on it that is intended for a string type value.

 

Note: Many times when you get a parsing error, the error is actually before the line on which the error actually exists. PHP will report the line oin which it reached a place where it cannot understand what to do. For example, if you forget to close a string variable with double quotes, the PHP parser will think the lines of code following that line is part of the string you are defining. It isn't until it sees a closing quote mark that it will identify the error.

 

FYI

 

echo "<input type='hidden' name='employeename[]' value='".$sourcename1."' />";
echo "<input type='hidden' name='ttitle[]' value='".$sourceaddress1."' />";

echo "<input type='hidden' name='sourcename1[]' value='".$income1."' />";

 

When defining string with double quotes, there is no need to exit the quotes to append a variable. You can do this

 

echo "<input type='hidden' name='employeename[]' value='{$sourcename1}' />";
echo "<input type='hidden' name='ttitle[]' value='{$sourceaddress1}' />";
echo "<input type='hidden' name='sourcename1[]' value='{$income1}' />";
Link to comment
Share on other sites

Sorry sir.

 

I think you mis understood my intentions.

 

When I post a question, I don't sit around waiting for someone to solve it for me. I keep researching.

 

So, what i was doing with those posts was to come and update you that i have resolved it so you don't waste your time on it any longer.

 

Your help is truly appreciated.

 

I was able to resolve thos string conversion error. The reason I had those errors was I was treating some form fields as arrays when infact they are not.

 

All I have left now is to figure out a way to display those array results to the user.

 

echo is not getting it done.

Link to comment
Share on other sites

The issue, is that you post a question and then a few minutes later you post that you've resolved it. That's great. You will learn much more by solving your problem yourself than someone just telling you what the problem is.

 

But, there are two reasons why seeing such behavior will make me not want to post a response: 1) It would seem that the OP did not take the time to at least attempt to solve their problem before posting. Providing help to such people would only encourage them to not try to solve their own problems. 2) If I take the time to research the issue and post a response, the OP may already find the solution before I get a chance to reply. Nobody gets paid to provide help here. We do this because we like helping people. So, the fact that someone was not really "stuck" and was just posting a "drive-by" question while they complete their due diligence makes me feel like they do not value my time.

 

I'm happy to help, really I am. But, if I find that my time is being wasted, I will be less apt to reply. You are free to post however you like (as long as it meets the forum rules), I am just providing some constructive criticism that I believe will help you get more and better responses.

Link to comment
Share on other sites

I do value your time very much and I do sincerely appreciate your help.

 

I do spend a lot of time researching the problems before posting, sometimes, I spend more than a day believe it or not as is the case with this issue but sometimes, it so happens that as I keep researching, I see something that leads me to the solution.

 

I will handle things differently next time.

 

Your point is well taken and thank you very much.

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.