2

I want to put my select value as my form action + string(/index.php) but it seems that I can't get the value of the select until I click my submit button.

<tr>
    <td>Select Project: </td><td><select name="myproject" onchange="">
        <option>Exam1</option>
    </select></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="login" id="login" value="Submit" 
        formaction="<?php $myproject = $_POST['myproject']; echo $myproject."/index.php"; ?>"
        formmethod="POST"/>
    </td>
</tr>
mantal
  • 1,093
  • 1
  • 20
  • 38
Jaycee
  • 149
  • 1
  • 1
  • 10
  • 2
    Try using some javascript to edit the DOM of your form using onchange, since what you're asking is impossible due to PHP being server-sided. – Ben Fortune Sep 09 '13 at 12:59
  • First find a way to create a working and correctly filled form. Then, after submitting your form, do whatever you want with the submitted data ... e.g. redirecting to another page or showing computed content. Otherwise you will still have the chicken egg problem. – djot Sep 09 '13 at 13:00
  • Avoid Javascript ... first learn how to submit and process a form and its data. You don't need Javascript for that ... makes things only even more complicated. – djot Sep 09 '13 at 13:04

2 Answers2

2

That is correct. You are mixing server and client side.

After reading your comment I believe you want this (plain JS chosen)

<script>
window.onload=function() {
  document.getElementsByName("myproject")[0].onchange=function() {
    var path = this.value;
    if (path) this.form.action=path+'/index.php';
  }
}
</script>

with the HTML now this:

<form method="post" action="">
Select Project: <select name="myproject">
<option value="">Please select</option>
<option value="exam1">Exam1</option>
</select>
<input type="submit">
</form>

To do it on the server, have a look at Post to another page within a PHP script

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • hi sir. thanks for answering. this form is my main form for my projects. this is a login form. i want to select also a project to be opened. using select i want to get the value and i will add a string /index.php.. i wanted to get the value Exam1/index.php to be assigned in form action of my button. – Jaycee Sep 09 '13 at 13:05
  • hello sir. i understand the flow of your answer but, how can i put the value of //this.form.action=path+'/index.php'; to the form action of my input type = submit name ='login' .. because when i tried the codes. it didnt redirects me in Exam1/index.php – Jaycee Sep 09 '13 at 15:50
  • Where does IT go? Note i spelled exam with lowercase e – mplungjan Sep 09 '13 at 16:12
  • it just reload my form. i already retype the Exam1. should i put a value in the form acion of my submit button('login') – Jaycee Sep 09 '13 at 16:20
  • No need, but you must change the select – mplungjan Sep 09 '13 at 16:57
0
$myproject = !empty($_POST['myproject']) ? $_POST['myproject'] : "defaultaction.php";
Carlos
  • 228
  • 3
  • 10