Search code examples
phplocationexechrefswitch-statement

header('Location: ) in php switch to execute url with onclick function


To put it simply I have this variable which carries a hyperlink:

$test3 = '<a href="#" onclick="archiveMove(\''.$mulmov.'\'); return false;">Move to Quotes</a>';

and what I need is to execute this variable inside a switch case like below:

switch ($_POST['dropdown'])  { 

    case "Select Folder":
    echo "Please select";
    break;

    case "One":
    exec($test3); <-- //here i want to run (if this is not execute, my misunderstanding) the link.


    break;

    case "Two":
    header('Location: http://www.facebook.com/'); <-- //this is just a test

    break;


default:
  echo "<br></br>";
  echo "Move multiple files:";
  echo "<br></br>";

  }


  ?>

  <form method="post" name="theform" action="">
    <select name="dropdown">
    <option value="Move to Folder">Select</option>
    <option value="One">One</option>
    <option value="Two">Two</option>
    </select>
    <input type="submit" value="Move"/>
</form>

I'd like know how to execute the ahref link without the user clicking it, but simply set this link as a case and when the user submits the form, the selected case actions the hyperlink.

Any help appreciated.


MORE DETAIL

I understand that javascript and php are both seperate languages and that a better option would be to use Ajax, but my understanding of Ajax is limited.

To explain it better, this is what's going on in its entirety:

1) I have a mailbox with a selection of messages.

2) You are able to check these messages and then click a link "Trash Selected" which deletes the selected messages. This the link:

<a href="#" onclick="deleteInbox(\''.$muldel.'\'); return false;">Trash Selected</a>

The javascript function actions the php function in $muldel for all selected messages and updates the database.

This is the javascript function in question:

function inboxDelete(url) {
    document.messages.action = url;
    document.messages.submit();
}

archiveMove() is exactly the same, just duplicated temporarily to make things clear.

3) I have now re-used the ahref code to do the same procedure, but this time, for moving the selected messages into folders.

4) These folders can be selected from a drop down box - this is where the form comes in.

5) So although I can get it to work by adding a link like such:

 $test3 = '<a href="#" onclick="archiveMove(\''.$mulmov.'\'); return false;">Move to Quotes</a>';

echo $test3;

6) I now need this to work the same way but the link being changed, depending on which folder is selected.

That's the full extent to my problem, I hope this is more clear.

I am aware you can send variables into javscript using GET or POST and then carry out the function entirely through javascript. I have tried something like below, but to no avail:

<form method=post name="myform" action="<?php echo $PHP_SELF;?>">
    <input type="hidden" name="formVar" value="">
    <input type="text" value="Enter Text Here" name="myText">
    <input type="text" value="Enter Text Here" name="myText2">
    <input type="submit" value="Send form!" onClick="readmove()">
</form>



 <?php
    // Retrieve the hidden form variable (using PHP).

    $myvar = $_POST['formVar'];

    if ($myvar == "$mulmov"){
        echo $mulmov; 
    }

    ?>
        <script language="JavaScript">
    <!--
    function setText(){
      document.myform.myText.value = document.myform.myText.value.toUpperCase();
    }


     function readmove(){
        document.myform.myText.value = "<?php echo $myvar; ?>" ; 
        readmove2();

    }


    function readmove2(){
        if (document.myform.myText.value == "$mulmov"){
            document.myform.myText2.value = "<?php echo $mulmov; ?>" ; 
            <?php exec ('archiveMove(\''.$mulmov.'\');  return false;'); ?>
    } else if (document.myform.myText.value == "$mulmov2"){
            document.myform.myText2.value = "<?php echo $mulmov2; ?>" ; 
    }
    }

</script>

Solution

  • Ok this was my solution but thank you also for your solution Jeff Ryan, this worked also.

    <script language="javascript">
    
    
    
     function buttons(str) 
    {
    document.getElementById("txtHint").innerHTML = str; 
    
    if (document.f1.users.options[1].selected){
        document.getElementById("txtHint").innerHTML ="<?php echo $mulmov; ?>";
        document.messages.action = document.getElementById("txtHint").innerHTML;
    
    
    }
    else if (document.f1.users.options[2].selected){
        document.getElementById("txtHint").innerHTML ="<?php echo $mulmov2; ?>";
        document.messages.action = document.getElementById("txtHint").innerHTML;
    
    
    }
    }
    
    function submit_mes(str)
    {
    
        document.messages.submit();
    }
    
        </script>
    
        <form name="f1">
    <select name="users" onChange="buttons(this.value)">
    <option value="">Select a folder:</option>
    <option value="Quotes">Quotes</option>
    <option value="Projects">Projects</option>
    
    <input type="button" value="Move" onClick="submit_mes(this.value)">
    </select>
    </form>
    
    <div id="txtHint"><b>Folder will be listed here.</b></div>