Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using the following form to POST to the same page upon clicking submit:

<form action="" method="post"><a class="buttonnohover">
Enter URL: </a><input type="text" name="term2" class="buttonnohover"/>
<input type="submit" class="button" name="submit" value="Add URL" />
</form>

Top of the code then needs to call the form POST and append to existing array of URLs ($myUrls) upon refresh. At the moment it just replaces the existing array value.

    <?php 

    try 
    {
        //URLs Array
        $myUrls = array($_POST['term2']);

        //Array Push for $myUrls
        array_push($myUrls,$term2);

...

Not sure what is wrong?

share|improve this question
add comment

3 Answers

At the moment it just replaces the existing array value.

Because you are directly assigning it to $myUrls variable like this $myUrls = array($_POST['term2']);

Do like this...

$term2 = array($_POST['term2']); // assign it to the $term2 variable instead of $myUrls
array_push($myUrls,$term2);

(or)

array_push($myUrls,array($_POST['term2']));
share|improve this answer
 
This just makes the $term2 var into the already posted variable from the form? Essentially it creates another array?!? –  Sam Whitehead Dec 24 at 4:37
 
You could make use of the second way i suggested as it pushes $_POST['term2'] directly to your $myUrls array –  Shankar Damodaran Dec 24 at 4:53
add comment

If you're just trying to add the value for term2 to your array of URLs you can just append it to the end:

$myUrls = array(); // This will normally be populated with your values
$myUrls[] = $_POST['term2'];
share|improve this answer
1  
Don't do this $myUrls = array(); because OP already has some values on $myUrls array, doing this will overwrite the existing values. –  Shankar Damodaran Dec 24 at 4:01
 
I wasn't telling them to make a new array. That was just to demonstrate in my code that it was an array so the next line would make sense. That's why I added the comment to hopefully make that clear. –  John Conde Dec 24 at 4:03
 
This does nothing. term2 adds to existing array entry, but upon adding another value, the new added term2 value replaces the newly added array value. –  Sam Whitehead Dec 24 at 4:51
add comment

Please try this,

  <?php

    if(isset($_POST['term2'])){

          @session_start();

          $myUrl = array($_POST['term2']);

          // DEFINE A SESSION ARRAY [CHECK BEFORE EXISTED OR NOT}]
          $_SESSION['myUrls'] =count($_SESSION['myUrls'])>0?$_SESSION['myUrls']:array();

          //ADDED TO SESSION ARRAY
          array_push($_SESSION['myUrls'],$myUrl);

          //GET ARRAY FROM SESSION
          $myUrls = $_SESSION['myUrls'];

          //PRINT THE RESULTS
          print_r($myUrls);

   }



  ?>

  <form action="" method="post"><a class="buttonnohover">
  Enter URL: </a><input type="text" name="term2" class="buttonnohover"/>
  <input type="submit" class="button" name="submit" value="Add URL" />
  </form>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.