1

I am attempting to make links act like a form POST as outlined here in a previous Stack Overflow example.

Here is the javascript code from that example that I am including in my header in my file named search.php:

<script>

function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.

// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {
    if(params.hasOwnProperty(key)) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
     }
}

document.body.appendChild(form);
form.submit();
}
</script>

Below, I am attempting to output a list of words from the $alternative multidimensional array so that they are listed in a clickable format, and act like a POST request. I am using php so I am attempting to combine the javascript and php together:

foreach ($alternative as $test)
{
  foreach ($test as $test2)
  {
  ?>    // end php code

  <script type="text/javascript">
  post_to_url('search2.php', {'<?php echo $test2;?>':'a'});  //php to echo $test2
  </script>

  <?php // start php code again
  }
}

It appears that it loads search.php and then loads search2.php almost instantly.

Two things that I am not sure of:

  1. Am I combining php and java script correctly?
  2. Am I implementing the java script function correctly?

This problem is a continuation of a problem about which I previous asked a question on Stack Overflow (click here). It was suggested to me to use a GET request - however my application does not act appropriately. (It does display the list of appropriate links but clicking those links results in inappropriate behavior). I believe that I must use a POST request.

Any suggestions on where I am going wrong or what I should do would be greatly appreciated.

Thanks guys.

Community
  • 1
  • 1
Tom
  • 531
  • 8
  • 16
  • in your javascript, the `form._submit_function_ = form.submit;` probably doesn't work, since form isn't defined yet. have you checked the console for js errors? – pichsenmeister Jul 20 '13 at 12:51
  • Apologies, there was an error in my code displayed. Edited now – Tom Jul 20 '13 at 13:39

0 Answers0