Jump to content

Reading dynamically created fields. $_POST does not work


mweinberger

Recommended Posts

Hi All,

 

I am using Ajax (JavaScript calling PHP) to create part of a form. The form displays and operates perfectly, so there is no problem on this front. My issue is how to read back the data once the user presses the submit button on the form. The standard PHP Super Global Variable, $_POST, works for all static fields (controls) hard coded into the HTML file, but $_POST does not show any of the dynamic fields.

 

I asked a colleague of mine and he said that dynamic fields must be manually enumerated, but that does not help me. I went through the list of $_POST variables and those fields are simply not there. According to PHP.net $_POST is a super global variable encompassing super global space. I have no problem with that. That is good, however $_POST does not seem to be super global enough. Is there an "Ultra Super Duper Global" PHP variable, such as $_ULTRAPOST? LOL

 

I am open to suggestions on how to solve this problem,

 

Thanks in advance,

 

Marci Weinberger

ButterflyVista

Link to comment
Share on other sites

Hi,

 

I do thank you for the quick reply. There is nothing magical from my perspective. Is there more than one way?

 

I create the main form using Ajax too, but the main form displays upon load, not via a button click. There is one part of the form that displays through a button click, which is where I have a problem. Think of a user clicking on a check box and if the button is checked, then the user sees the details of the form, else then see nothing more.

 

The user clicks on a button and that calls a JavaScript function. JavaScript calls to a PHP function using the standard HTML Get (in this case I use Sajax, but I use various means). The PHP Ajax code that generates the form is something like:

 

This PHP function dumps the main form onto the display, which works for read, if I might add. There is a similar function which creates the full body if the user checks the check box.

 

function ShowAddDonation()

{

// Get the main body.

if (1 == $this->DataAddDonateUses)

$strBody = $this->ShowAddDonationBody();

else

$strBody = "";

 

// Form: Header

// $strHTML .= "<h3 style=\"margin-left:10; margin-top:10; margin-bottom:0;\">$this->FormTextAddDonateHeader</h2>\r\n";

$strHTML .= "<p align=\"left\" style=\"margin-bottom:5; font-size: 14pt; color: #FF0000;\">$this->FormTextAddDonateHeader</p>\r\n";

$strHTML .= "<table class=\"styleClientContestAdd\" align=\"center\" cellpadding=\"8\" cellspacing=\"0\">\r\n";

 

// Form: Body

$strHTML .= "\t<p style=\"margin-top:15; margin-left:15;\" align=\"left\"><input type=\"checkbox\" id=\"IsDonate\" name=\"IsDonate\" onclick=\"javascript:ShowFormDonation();\" value=1 $DataCheckDonation>$this->FormTextAddDonateCheck</p>\r\n";

$strHTML .= "\t<p style=\"margin-top:0;  margin-left:15; margin-right:15; font-size:8.0pt\" align=\"left\">$this->FormTextAddDonateDisclaimer</p>\r\n";

$strHTML .= "\t<div id=\"BodyFormDonation\" name=\"BodyFormDonation\">\r\n";

// $strHTML .= $strBody;

$strHTML .= "\t\t<script type=\"text/javascript\">\r\n";

$strHTML .= "\t\t<!--\r\n";

$strHTML .= "\t\t\t// Show the donation form, if checked.\r\n";

$strHTML .= "\t\t\tShowFormDonation();\r\n";

$strHTML .= "\t\t<-->\r\n";

$strHTML .= "\t\t</script>\r\n";

$strHTML .= "\t</div>\r\n";

 

// Form: Footer

$strHTML .= "</table>\r\n";

 

return $strHTML;

}

 

If the user checks the check box, the other similar function to the one above dumps the PHP string to the already dynamically laid out DIV statement that showed on load using the JavaScript call:

 

function ToAreaDonate( strOutput )

{

var objBodyDonate;

 

 

// Get the donation area.

objBodyDonate = document.getElementById( "BodyFormDonation" );

 

// Set the new text.

objBodyDonate.innerHTML = strOutput;

}

 

The JavaScript variable, strOutput, contains the PHP string variable, $strHTML. I push the form onto the main form using the  JavaScript line above. Baiscally, strOutput sets replaces the content of a DIV statement. That is something like:

 

As I stated, the static stuff posts just fine, but the Ajax part of the form does not show up in the "Super Global" $_POST variable. Maybe the PHP committee should rethink their description of "Super Global" lest Supergirl and Superman have a new enemy that is even more powerful than kryptonite, Ajax! LOL

 

Marci

Link to comment
Share on other sites

Maybe the PHP committee should rethink their description of "Super Global" lest Supergirl and Superman have a new enemy that is even more powerful than kryptonite, Ajax!

 

Trust me... if the value is sent via the post method, it will be in the $_POST array.

 

Its pretty unclear as to how your code is dynamically generating a form. Any chance of seeing a snippet of the final forms html?

Link to comment
Share on other sites

Hi Again,

 

I just modified my last response a bit. I do not understand what you mean by sent via the POST. I am replacing a dynamically laid out DIV statement using the JavaScript call:

 

  // Get the donation area.

  objBodyDonate = document.getElementById( "BodyFormDonation" );

 

  // Set the new text.

  objBodyDonate.innerHTML = strOutput;

 

Is that a sent by post? I am really just replacing the innerHTML contents of a DIV statement to show the new form. I tried last week as an exercise simplifying the sample laying out a real DIV statement hard coded in HTML and then trying to read the value of a field back that was laid out by Ajax and I have the same problem. The field shows, but $_POST does not see the field.

 

As to seeing code fragment, I did show the code fragment. I did not post the entire two files here, as that is huge and not appropriate for display. I can create a sample, which would be a bit of work. I could give you the actual URL, but you would have to create an account and go through a lot of gyrations, which does not seem worthwhile.

 

Would creating a sample code be helpful, although I would prefer to not do that? It would show the same stuff that I showed above.

 

Let me know. Also, what do you mean by "sent via the post method"?

 

Marci

Link to comment
Share on other sites

I need to see the form. A form has an method attribute. eg;

 

<form action="page.php" method="post">

 

If this is set to post, your values will show up in the $_POST array when the form is submitted, otherwsie they will be within the $_GET array.

Link to comment
Share on other sites

Hi,

 

I found the problem, but not the solution, so at least that is a partial victory. I created a demonstration page for you and I discovered that it worked. I then started to ask what the difference is between the test code that I wrote and the real code. The difference turned out to be that the DIV statement containing the dynamic form is embedded inside a TABLE cell. Is there an alternative as a DIV statement works best, but it has to go inside a multi-tiered table?

 

To answer your question, which is kind of not appropriate anymore. It is:

 

<form method="post" action="#" onSubmit="return validateform();" enctype="multipart/form-data"><td width="665" align="justify" height="23" valign="top" bgcolor="#7F0B0B" colspan="5" style="margin-left: 5; margin-right: 5; margin-top: 0">

 

The sample code is as follows:

 

<body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" bgcolor="#ffffff"><center>

<?PHP if ("" != $UserEntered) echo( "<p style=\"margin-top:5; margin-bottom:10;\">You entered dynamically read text [$UserEntered]<br /></p>\r\n" ); ?>

<?PHP if ("" != $UserNonDynamic) echo( "<p style=\"margin-top:5; margin-bottom:10;\">Non-dynamically entered text [$UserNonDynamic]<br /></p>\r\n" ); ?>

 

<!--

<table width="585" border="0" align="center" cellpadding="0" cellspacing="0">

<tr valign="top" bgcolor="#FFFFFF">

<td>

-->

<form method="post" action="#" onSubmit="return validateform();" enctype="multipart/form-data"><td width="665" align="justify" height="23" valign="top" bgcolor="#7F0B0B" colspan="5" style="margin-left: 5; margin-right: 5; margin-top: 0">

<?PHP

// Data: Determine if there should be a checkmark or not on the field.

if (1 == $this->DataAddDonateUses)

$DataCheckUser = "checked=\"Checked\"";

else

$DataCheckUser = "";

 

// Form: Body

$strHTML .= "\t<p style=\"margin-top:0; margin-bottom:15;\">Non-Dynamic Text: <br /><input id=\"FldNotDynamic\" name=\"FldNotDynamic\" type=\"text\" size=\"40\"></p>\r\n";

$strHTML .= "\t<p style=\"margin-top:15; margin-left:15;\" align=\"left\"><input type=\"checkbox\" id=\"IsShowUser\" name=\"IsShowUser\" onclick=\"javascript:ShowUserForm();\" value=1 $DataCheckUser> Place a check on this box to see the dynamically created use form.</p>\r\n";

$strHTML .= "\t<div id=\"BodyFormUser\" name=\"BodyFormUser\">\r\n";

$strHTML .= "\t\t<script type=\"text/javascript\">\r\n";

$strHTML .= "\t\t<!--\r\n";

$strHTML .= "\t\t\t// Show the user form, if checked.\r\n";

$strHTML .= "\t\t\tShowUserForm();\r\n";

$strHTML .= "\t\t<-->\r\n";

$strHTML .= "\t\t</script>\r\n";

$strHTML .= "\t</div>\r\n";

 

// Form: Header

$strHTML .= "<table align=\"center\" cellpadding=\"8\" cellspacing=\"0\">\r\n";

 

// Form: Body (Command Buttons)

$strHTML .= "\t<tr valign=\"top\" bgcolor=\"#FFFFFF\">\r\n";

$strHTML .= "\t\t<td width=\"20\"> </td>\r\n";

$strHTML .= "\t\t<td width=\"545\" align=\"center\">\r\n";

$strHTML .= "\t\t\t<table class=\"styleClientContestAdd\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\r\n";

$strHTML .= "\t\t\t<tr>\r\n";

$strHTML .= "\t\t\t<td><input type=\"submit\" name=\"Submit\" value=\"Submit Dynamic Text\"><input type=\"hidden\" id=\"ContestID\" name=\"ContestID\" value=\"$this->IDContest\"><input type=\"hidden\" name=\"validateme\" value=\"doit\"></td>\r\n";

$strHTML .= "\t\t\t</tr>\r\n";

$strHTML .= "\t\t\t</table>\r\n";

$strHTML .= "\t\t</td>\r\n";

$strHTML .= "\t\t<td width=\"20\"> </td>\r\n";

$strHTML .= "\t</tr>\r\n";

 

// Form: Footer

$strHTML .= "</table>\r\n";

 

// Show the form.

echo( $strHTML );

?>

</form>

<!--

</td>

</tr>

</table>

-->

</center></body>

</html>

 

If I uncomment the table lines, then the form fails. If I leave the comments in, then the form fails. I can give the code above the BODY tag if you want. I just did not want this reply to get too long.

 

Thanks for your help!

 

Marci

Link to comment
Share on other sites

Done, Whew!

 

I can add yet another sub table and fill in the innerHTML of a table cell and that works! The following is the new code fragment:

 

$strHTML .= "\t\t\t\t\t<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\r\n";

$strHTML .= "\t\t\t\t\t\t<tr>\r\n";

$strHTML .= "\t\t\t\t\t\t\t<td ID=\"BodyFormUser\" NAME=\"BodyFormUser\">\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t<script type=\"text/javascript\">\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t<!--\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t\t// Show the user form, if checked.\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t\tShowUserForm();\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t<-->\r\n";

$strHTML .= "\t\t\t\t\t\t\t\t</script>\r\n";

$strHTML .= "\t\t\t\t\t\t\t</td>\r\n";

$strHTML .= "\t\t\t\t\t\t</tr>\r\n";

$strHTML .= "\t\t\t\t\t</table>\r\n";

 

ButterflyVista! (Yahoo!, different company)

 

Marci

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.