Jump to content

Dynamic JS form and $_POST array


jamina1

Recommended Posts

Hey guys.

 

I have a form that I am altering after page load with javascript. Basically I have a button that inserts another text field into the form which the user can fill out. Its for a calendar, and I'm allowing them to optionally add multiple days for an event.

Each box that is created has the same name with [] on the end to make it an array.

 

When I view the post array with print_r($_POST), only the first textbox's value shows up. This is the only textbox in the form when the page loads.

Simplified example:

<form name="calform" action="process.php" method="post">
<input name="startday[]" type="text" class="bordered" size="25" />
<input type="button" onclick="add_one_more()" value="Add another date" />

// if the user hits the add button it adds this input field after pageload

<input name="startday[]" type="text" class="bordered" size="25" />

</form>

And here's what I'd get from that form

 

Array
(
    [startday] => Array
        (
            [0] => test1
        )

)

 

A button inside the form basically adds another <input> tag between the <form> tags.

Why isn't PHP seeing the data?

Link to comment
Share on other sites

Where are you printing out the array? Before or AFTER the form is submitted?

 

I'm printing the array directly from the process.php page the form points to. Right now as debugging, I'm forgoing everything besides the print_r command so I can see what's passing to PHP.

Link to comment
Share on other sites

The javascript has to insert data into a table structure so its complicated. The command that actually prints out the <input> field is element.innerHTML like so:

 

	k.innerHTML = "<input name=\"starttime[]\" type=\"text\" class=\"bordered\" size=\"25\" />"

 

The javascript works perfectly, its the PHP I'm having issues with. Even if I assign the javascript inserted text boxes a predetermined value with value="something" that doesn't show up in the $_POST array.

Link to comment
Share on other sites

  • 11 months later...

Perhaps you're using two forms on your form page.  I was having a similar problem and just solved it.

 

The problem I was having: my dynamic dropdown, drawn from an external PHP page using AJAX, was populating fine, but my $_POST array on the resulting page was not listing the chosen value in my dynamic list.

 

Here's what I did to solve the problem.  In my PHP page called by my AJAX routing, I modified my <select> statement as follows:

 

<select name="veModel" id="veModel" onchange="partSearch.veModel.value=veModel.value; alert(partSearch.veModel.value)">

 

... where partSearch is the name of the veModel field's parent form on the search page.  The alert() is in there for debugging.  You will, of course, want to remove this after you verify the fix works for you.

 

Hope this helps!

 

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.