LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-23-2018, 06:37 PM   #1
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Rep: Reputation: 15
PHP form-handling ignores part of a print() argument.


I'm writing a set of HTML/PHP pages to receive user-data regarding changes to a large schedule of meetings. Users may submit data for (1) a new meeting, (2) changes to an existing meeting, or (3) deletion of an expired meeting. Furthermore, users may choose either to (a) print the completed form to paper and deliver it to the main office or (b) submit the data online.

The very first two data items requested of the user are the choice of function (add, change, or delete) and the effective date. A code snippet toward that end looks like this:
Code:
if ($_POST["purpose"] == 'new') {
$doWhatWhen = "<span id='cue'>Add</span> a new meeting, ";
} elseif ($_POST["purpose"] == 'chg') { 
$doWhatWhen = "<span id='cue'>Change</span> this meeting's listing, ";
} else {
$doWhatWhen = "<span id='cue'>Delete</span> the following meeting, ";
}

$doWhatWhen .= "as of " . $_POST["effdate"] . ".";

print ($doWhatWhen . "<br /><br />");
// ***** Here's where the trouble appears *****
print ("<input type='hidden' name='doWhatWhen' value='" . $doWhatWhen . "' />");
When I submit the form to the following page (for error-checking), the if-statements function properly, and the short print() statement (displaying the full content of the concatenated $doWhatWhen variable and the two line-breaks) displays correctly.

However, after that, the $doWhatWhen string is repeated followed by all the punctuation within the terminal pair of quotation marks. It looks like this:
Quote:
Change this meeting's listing, as of 4/1/2018.

Change this meeting's listing, as of 4/1/2018.' />
This is followed, without any space or line-break, by the next item of data in the form.

Everything beyond this point displays correctly, and many lines of code are written in the same format; I used copy-and-paste to write them, changing only the variable names.

Another consequence of whatever is happening is that the <input> element is not created, so the value of $doWhatWhen is not forwarded to the handling page.

I've tried changing the variable's name, to no effect.
I did break that troublesome line out of PHP for the first and last parts of the <input> element, calling PHP only to insert the value of the $doWhatWhen variable, like this:
Code:
?><input type='hidden' name='doWhatWhen' value='<?php $doWhatWhen ?>' /><?php
. That behaved properly in part. That is, it does not duplicate the latter part of the print() argument as a string, but neither does it pass the $doWhatWhen variable on to the next page.
 
Old 02-23-2018, 07:30 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Code:
print ("<input type='hidden' name='doWhatWhen' value='" . $doWhatWhen . "' />");
Not much of a programmer but if using double quotes variables will be expanded. I also use a backslash to specify a literal single quote.

Code:
print ("<input type=\'hidden\' name=\'doWhatWhen\' value=\'$doWhatWhen\' />");

Last edited by michaelk; 02-23-2018 at 07:43 PM.
 
Old 02-23-2018, 08:31 PM   #3
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
Thanks, michaelk, but I had already looked at the possibilities of quotation-mark problems. Thing is, I have another ten or so lines of code that are written identically (except for the name and $variable), and they all work perfectly.
 
Old 02-23-2018, 08:57 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Maybe it's the embedded single quote in the variable that's causing the problem. Try:
1. Escaping it...
Code:
$doWhatWhen = "<span id='cue'>Change</span> this meeting\'s listing, ";
or, if that causes the \ to print, use:
Code:
$doWhatWhen = "<span id='cue'>Change</span> the listing of this meeting, ";

Presuming here that the lines that work don't have an embedded single quote...

Last edited by scasey; 02-23-2018 at 09:01 PM.
 
Old 02-23-2018, 09:25 PM   #5
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
Another good thought, scasey. But I even tried it using
Quote:
...this meeting listing...
without any apostrophe anywhere in sight, and still got the same error.
 
Old 02-24-2018, 12:30 AM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Hmmm.
Am I interpreting correctly that the result of the print statement should look like this?
Code:
<input type='hidden' name='doWhatWhen' value='"<span id='cue'>Change</span> this meeting's listing, "' />
Is this the only variable with an an embedded span tag? That is, is the > and or < embedded in the variable that's to blame?
Yes. Putting the code above into a form yields
Code:
Change this meeting's listing, "' />
on the web page, duplicating what you're reporting...and View Source in Firefox indicates a syntax error in the html code:
Code:
<input type='hidden' name='doWhatWhen' value='"<span id='cue'>Change</span> this meeting's listing, "' />
What are you trying to accomplish with the span tag's id?
Since the input type is hidden, do you mean for nothing to print?

Last edited by scasey; 02-24-2018 at 12:41 AM.
 
1 members found this post helpful.
Old 02-24-2018, 07:46 AM   #7
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
Yes, scasey, that appears to be the cause! Thank you!

The <span> elements are used by a JavaScript function to change the page header if the user opts to print the form on paper. Instead of the generic "Meeting Change Form" and instructions that appear initially, the page (which will be submitted to the database manager) gets a specific heading that says "Add New Meeting" or "Change Meeting Data" or "Delete a Meeting." That, however, is secondary to actually getting the thing working, so I'll come up with some other way to cue the JS function to change the header.

Thanks again!
 
Old 02-24-2018, 11:58 AM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
My pleasure. I presume the span does its thing in the first print, yes?
Suggestion: Just create a separate variable for use in the <input tag that doesn't contain the span tag.
Code:
if ($_POST["purpose"] == 'new') {
$doWhatWhen = "<span id='cue'>Add</span> a new meeting, ";
$doWhatWhen1 = "Add a new meeting, ";
} elseif ($_POST["purpose"] == 'chg') { 
$doWhatWhen = "<span id='cue'>Change</span> this meeting's listing, ";
$doWhatWhen1 = "Change this meeting's listing, ";
} else {
$doWhatWhen = "<span id='cue'>Delete</span> the following meeting, ";
$doWhatWhen1 = "Delete the following meeting, ";
}

$doWhatWhen .= "as of " . $_POST["effdate"] . ".";
$doWhatWhen1 .= "as of " . $_POST["effdate"] . ".";

print ($doWhatWhen . "<br /><br />");
// 
print ("<input type='hidden' name='doWhatWhen' value='" . $doWhatWhen1 . "' />");
Glad I could help...

Last edited by scasey; 02-24-2018 at 04:40 PM.
 
Old 02-24-2018, 04:35 PM   #9
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
What I actually did was to write a PHP line to create an <input> element that is never used by the follow-up PHP page, but is found (via the id='cue' attribute) by the JavaScript function.
Code:
print ("<input type='hidden' id='cue' value='" . $_POST["purpose"] . "' />");
Then I changed the JavaScript function to read the document.getElementById('cue').value (rather than the innerHTML). Works a charm!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i make a diff between directories that ignores part of the filename pandersson61 Programming 4 12-16-2012 07:15 AM
KeePass 2 ignores commandline argument Al_ Linux - Software 2 07-04-2011 01:17 PM
how to show results of a php query from a form on the main form texmansru47 Programming 2 06-27-2008 01:26 PM
PHP: build query from form entry, then display results in the same form tonedeaf1969 Programming 4 06-22-2007 07:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration