0

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?

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks heaps man. This was not quite what I wanted but put me on the right track!
1

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']));

2 Comments

This just makes the $term2 var into the already posted variable from the form? Essentially it creates another array?!?
You could make use of the second way i suggested as it pushes $_POST['term2'] directly to your $myUrls array
1

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'];

3 Comments

Don't do this $myUrls = array(); because OP already has some values on $myUrls array, doing this will overwrite the existing values.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.