Jump to content

I can't figure out why this script doesn't work.


Alternamaton

Recommended Posts

This isn't the actual script I'm using, it's just an example of the functionality that I'm going for.

 

<HTML>
<BODY>
<?php

$counter = 2;

echo "<FORM method = 'post'>
Enter a number: <input type = 'text' name = 'number[1]' size = 5>
<br>";

while (isset($_POST['add_number']))
{
    echo "<br>Enter another number: <input type = 'text' name = 'number[" . $counter . "]' size = 5>";
    $counter++;
    unset($_POST['add_number']);
}
?>

<br>
<input type = 'submit' name = 'add_number' value = 'Another number!'>
</FORM>
</BODY>
</HTML>

 

If you click the 'Another number!' button once, the script echoes another input field. However, if you continue clicking the button after that, nothing happens. It should ( "should"... ::) ) echo another input field every time you click the button, because every time you click it the variable $_POST['add_number'] is first set and then unset (and therefore ready to be set again, at least in theory...).

 

I tried it with "if" instead of "while" (though in this case I don't really see the difference), but that didn't work either.

 

What gives?

Link to comment
Share on other sites

<?php session_start(); ?>
<HTML>
<BODY>
<?php

$_SESSION['counter'] ? isset($_SESSION['counter']) : $_SESSION['counter'] = 2;

echo "<FORM method = 'post'>
Enter a number: <input type = 'text' name = 'number[1]' size = 5>
<br>";

while (isset($_POST['add_number']))
{
   echo "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>";
   $_SESSION['counter']++;
   unset($_POST['add_number']);
}
?>

<br>
<input type = 'submit' name = 'add_number' value = 'Another number!'>
</FORM>
</BODY>
</HTML>

Link to comment
Share on other sites

Edit: Updated to give the additional form inputs the correct names ("name = number[" . $_SESSION['counter'] . "...", not "name = " . $counter . "..."). Still doesn't work.

 

That's good to know, but even if every form input after the fist has the same name ('number[2]'), they should still show up every time you hit the submit button, shouldn't they?

 

In other words, thank you for pointing that out, but that's not the main problem with the script. The main problem is that after you click the submit button the first time, nothing happens on subsequent clicks.

 

Here's the updated version of the code, with a bit at the end to see if clicking the submit button adds to the $_SESSION['counter'] variable.

 

<?php
session_start();

$_SESSION['counter'] = 2;
?>

<HTML>
<BODY>
<?php


echo "<FORM method = 'post'>
Enter a number: <input type = 'text' name = 'number[1]' size = 5>
<br>";

while (isset($_POST['add_number']))
{
    echo "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter']. "]' size = 5>";
    $_SESSION['counter']++;
    unset($_POST['add_number']);
}
?>

<br>
<input type = 'submit' name = 'add_number' value = 'Another number!'>
</FORM>
<?php echo $_SESSION['counter']; ?>
</BODY>
</HTML>

 

It still doesn't work, and no matter how many times you click the submit button $_SESSION['counter'] is echoed as 3.

Link to comment
Share on other sites

Sorry, theres actually quite a bit more to it then that. I didn't really read your code. The while has got to go.... variables do not persist. You also left out a chunk of my code (which id'e foobared anywho).

 

<?php
session_start();
isset($_SESSION['counter'] ? $_SESSION['counter'] : $_SESSION['counter'] = 2;
?>

<HTML>
<BODY>
<?php
echo "<FORM method = 'post'>
Enter a number: <input type = 'text' name = 'number[1]' size = 5>
<br>";
$_SESSION['form'] = '';
if (isset($_POST['add_number']))
{
    $_SESSION['form'] .= "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>";
    $_SESSION['counter']++;
}
?>
echo $_SESSION['form'];
<br>
<input type = 'submit' name = 'add_number' value = 'Another number!'>
</FORM>
<?php echo $_SESSION['counter']; ?>
</BODY>
</HTML>

 

You know this type of thing is much more efficiently done in Javascript?

Link to comment
Share on other sites

$_SESSION['form'] = '';
if (isset($_POST['add_number']))
{
    $_SESSION['form'] .= "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>";
    $_SESSION['counter']++;
}
?>
echo $_SESSION['form'];

 

So you store the HTML code for the form as a $_SESSION variable, and then when the user clicks "Another number!" the $_SESSION variable is simply appended, and then the whole form is echoed. I take it that this is because every time the user clicks a submit button the page is refereshed, and the script doesn't remember that the LAST time the submit button was clicked, the form had ALREADY been added to.

 

D'oh. I think I understand now. Thank you! :)

 

Oh, and I don't know Javascript, but I will look into learning it. I am going to be taking the input variables and feeding them to MySQL, though. Would JavaScript be able to handle that?

Link to comment
Share on other sites

it would be MUCH easier and more practical to do this with a javascript thing...  and the fact that you dont have to store a session variables or the values of the temporary fields is a plus.

if you werent thinking about storing temporary data...  it would be an inconvenience for the user to have to fill in every number again.

 

<script language="javascript">
function add_input(id) {
  box = document.getElementById("newForm_" + id);
  box.innerHTML = 'insert your form data here';
  box.innerHTML += '<div id="newForm_'+(id + 1)+'"></div>';

  //i dont exactly remember how to set this property, so the following line is pseudo-pseudocode
  document.getElementById("add_number").onmouseup = "add_input(" + (id+1) + ")";
}
</script>

 

of course, that requires that you add a div at the end of your form, and also that you add an onmouseup event to trigger that function.  its just the way i would do it.

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.