[SOLVED] PHP form-handling ignores part of a print() argument.
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
. 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.
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.
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?
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.
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 . "' />");
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.