Jump to content

Recommended Posts

Hey.

 

Just wondering, if I loop through textboxes with PHP, and insert query for each - that if I added a new textbox with javascript, when submitted - would PHP read that one too? I'll write some code, maybe it'll explain with more ease.

 

PHP code (When form submitted)

$connection = mysql_connect('***', '***', '***');
$database = mysql_select_db('***');
for ($i = $_POST['txtboxNum']; $i >= 1; $i--) {
    echo $i;
}
$number = 0;
$query = mysql_query("SELECT * FROM field");
while ($array = mysql_fetch_array($query)) {  
  $number++;
  echo '<input type="text" name="txtbox'.$number.'" value="hello '.$number.'" />';
}
echo '<input type="hidden" name="txtboxNum" value="'.$number.'" />';

 

If I were to make a Javascript code to add another textbox into the code with a name of the next number, when submitted - would PHP be able to read that? I'm only asking because Javascript is client-side and not sure if PHP reads frfom browser or what. I'm just not 100%, so need reassurance.

 

PS: Ignore any logic errors with the PHP code, if there are any. I just wrote this up in the thread body field, my version of this works fine. Just need the question answered (:

Thanks alot.

Make sure you include the array brackets on the name when you name it using Javascript or it will not be part of the array:

 

<FORM action="" method="POST">
<INPUT type="text" name="txtData[]">
<INPUT type="text" name="txtData[]">
<INPUT type="text" name="txtData[]">
</FORM>

 

When POSTed this will produce an array in PHP:

foreach ($_POST['txtData'] as $field) {
  echo $field . PHP_EOL;
}

 

When you add the field in Javascript, you need to make sure you set the name to "txtData[]" so it will be part of the array.

 

Note: I've never actually done this, but my experience scanning through the form elements in Javascript seem to bear this out.

 

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.