Php if Else in form action - php
Im trying to make an online concert ticket system and this is my problem..
<?php
if($tickettype == 'VIP'){
$action = "seats.php";
}else if($tickettype == 'VVIP'){
$action ="seats1.php";
}
?>
<form action= "<?php $action; ?>" method="post">
And it doesn't work. Thank you in advance!
heres the full codes for the particular file that has a problem.
<html>
<head>
<title>Ariana Grande Concert</title>
</head>
<body>
<br>
<table>
<tr>
<td><img src="ariana2.jpg" height="300" width="260"></td>
<td></td><td></td><td></td>
<td>
<h2>ARIANA GRANDE THE HONEYMOON TOUR</h2><br>
<font face="Lucida Sans Unicode"> March 11, 2017<br>
Grand Ballroom, Solaire Resort & Casino<br>
Due to peoples demand, Ariana Grande is back in the Philippines<br>
Ariana Grande Live in the Philippines on <b> March 11, 2017!</b></font>
<br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
<br><br>
<hr>
<br>
<center>
<table cellspacing="10" cellpadding="10" bgcolor="gray">
<tr>
<td><font face=""><b>TICKET PRICES:</b></font></td>
<td><b>VVIP:</b> <u>Php 25,000.00</u></td>
<td><b>VIP:</b> <u>Php 20,000.00</u></td>
<td><b>Upper Box A:</b> <u>Php 15,000.00</u></td>
<td><b>Upper Box B:</b> <u>Php 15,000.00</u></td>
<td><b>Lower Box A:</b> <u>Php 10,000.00</u></td>
<td><b>Lower Box B:</b> <u>Php 10,000.00</u></td>
<td><b>General Ad:</b> <u>Php 5,000.00</u></td>
</tr>
</table>
</center>
<br><br>
<font face="Lucida Sans Unicode" size="2"><b>Ticket Type: </b></font>
<?php
$TicketType = array('VIP' =>'VIP', 'VVIP' =>'VVIP', 'Upper Box A'=>'Upper Box A', 'Upper Box B'=>'Upper Box B', 'Lower Box A'=>'Lower Box A', 'Lower Box B'=>'Lower Box B', 'General Admission'=>'General Admission') ;
echo ' <select name="Ticket_Type">';
foreach ($TicketType as $key => $value) {
echo "<option value=\"$key\">$value</option>";
}
echo '</select>';
if($TicketType == 'VIP'){
$action = "index.php";
}else if ($TicketType == 'VVIP') {
$action = "seats.php";
}
echo '<font face="Lucida Sans Unicode" size="2"><b> Quantity: </b></font>';
$Quantity = range (1, 30);
echo '<select name="Quantity">';
foreach ($Quantity as $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';
?>
<form action="<?php echo $action; ?>" method="post">
<br><br><br>
<button class="btnExample" type="submit" value="Submit"/>Next</button>
<style>
.btnExample {
color: #0000;
background: white;
font-weight: bold;
border: 1px solid #0000;
border-radius: 10px 10px 10px;
}
</style>
</form>
</body>
</html>
Heres the full codes of the webpage that has a problem.
You did not echo the variable to show the output:
<?php
if($tickettype == 'VIP'){
$action = "seats.php";
}else if($tickettype == 'VVIP'){
$action ="seats1.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Notice the <?php echo $action; ?>
Note: This is not the answer, and you should not accept this as answer, I am posting it here instead of posting in the comment section, because it's to long.
Note - You can solve your problem by your self, but for other it is impossible, as your code is just not making any sense at the moment, but I am pretty sure it will if you are able to read and understood what I am trying to say here in the solution.
First thing first,
Access Denied problem –
Note - you don’t have to fix this problem it will get fix by it self, but you should know why you have this problem, so in the future if you get the problem, you will know what to correct.
The reason it denying the access is this, that you are not allow to give special character for form action so after adding the echo in the form you are going to have this problem, because as soon as you add the echo in our form action you unknowingly giving special character to the action, because you never set the value for your variable $action so value for variable $action is equal to error message. So after adding the echo you are displaying that error message.
before doing any thing run your file in the browser and check the source code,
and see the value in the form action, it will be some thing like this
<br /><b>Notice</b>: Undefined variable: action in <b>C:\in here you are going to have file path\form.php</b> on line <b>1</b><br />
notice how many angle brackets colon you got in the here, you are not even allowed one angle brackets, so browser is thinking you are trying to do some thing malicious and that's why its blocking your access.
If you don’t know how to check source code, just type how to see source code and give your browser name (don’t ask me it just one click away)
so your code is basically generating error message and displaying it for the action after adding the echo and that's why browser is blocking the access
so why it was not doing the same thing before adding the echo, because in php, if you don’t use echo you are not displaying any thing, so before adding the echo, your action was empty, just remove the echo from the form action and check the source code again, you will understand what I am trying to say.
Again you don’t have to fix it, it will get fix by it self, remember this.
Now the solution start from here
First I have to be right about my assumption, that what you are trying to do,
So these are my assumption, what you want to do,
People should be able to buy different different types of ticket, and they can choose the number of ticket from 1 to 30,
Than you want to run different file according to the TicketType, in one case you want to run seat.php and in one case you want to run index.php,
And to achieve your goal, you created a form, use the if statement to set your action value in the form, as I can see you are using variable $action in the form for action, and you set this value through your if statement.
If I am wrong about any thing up until now, don’t read further it will be waste of time.
basically to achieve your goal, you are using form, array and some logic,
in your case you seems to know array and logic, but you lack pre basic knowledge about the form, and that's why your code is not working.
To be honest. In my opinion you know nothing about the form. That's why it is impossible to give you solution(at least for me). But I am quite sure, if you read this fully you will be able to solve your problem by your self,
so give a quick read to the points, and you will be surprised how easy it is to solve your problem for you, and you will also realized why it is impossible for other to solve it for you, at the moment.
Now Start from here
I am dividing the problem in Four part
Some important Notes about the form.
$Ticket_Type is not equal $TicketType - an you can not even use $TiccketType here
Select option only passes the value from the value tag-- Two part solution
Your logic is at the wrong place,
1. Some important point about the form.
When you use form, you have to tell php, that you are submitting the form, and if you want to do it only with php, you have to use submit button for it(I already mention this in my comment other day),
second you need to know, what happened when you click on the submit button in the form, form will run the action file and also will pass the data from. opening form tag <form> to closing form tag </form> to that file
but any data which is out of these opening and closing form tag, form will not pass that data, so point is it only passes the data which is inside the opening form and closing form tag.
Look at the code below to understand it better, and please also run this code, so you can understand it better,
Few points about the code
in the below code, we have two input field location and name, location input field is out of the form tag and name input field is inside the form tag.
and our form action is seat.php, and name for form file is form.php for this example.
as our action is seat.php, so if you click on the submit button, it will pass the data to seat.php, and will run that file.
in seat php, we are just using echo to display both value,
see the code below for form.php and for seat.php,
form.php
Location<input type="text" name="location"/><br><br>//out side of the form tag
<form action="seat.php" method="post">
Name <input type="text" name="name"/><br><br>
Location <input type="text" name="location"/><br><br>
<input type="submit" value="Hot Air">
</form>
seat.php
<?php
$name = $_POST['name'];
echo $name.'<br>';
$location = $_POST['location'];
echo $location;
Now go and run form.php and type some thing in the location and name box, and click on the submit button, and if you do that, it will only display you the value for name but for location value it will display you the undefined index location error.
And the reason is, because your form never passed that value for location to seat.php1, becauselocationis out ofform` opening and closing tag,
Now put this Location field inside the form tag
now put that location inside the opening and closing form tag,
after doing that our code should look like this
<form action="seat.php" method="post">
Name <input type="text" name="name"/><br><br>
Location <input type="text" name="location"/><br><br>//Now location is inside of the form
<input type="submit" value="Hot Air">
</form>
Now again run your form.php type some thing for name and for location and click on submit button, this time it will display you both the value, as this time both values are inside the form tag so both value get passed.
Note – If you can understand the difference between the first result and second result, you will be able to understand, why you need to have your select tag inside the opening and closing form tag, you can not have them out side of,
$Ticket_Type is not equal to $TicketType – and you can not even use $TicketType in here as it is equal to array you want value from the select option
For Second mistake, pay attention to this portion of your code
echo ' <select name="Ticket_Type">';
foreach ($TicketType as $key => $value) {
echo "<option value=\"$key\">$value</option>";
}
echo '</select>';
if($TicketType == 'VIP'){
$action = "index.php";
}else if ($TicketType == 'VVIP') {
$action = "seats.php";
}
in here if you read this line echo ' <select name="Ticket_Type">' you give name for select Ticket_Type and than in the if statement You use the variable $TicketType, I am assuming you are checking the value for $TicketType, This problem you can solve it by changing the variable name $TicketType to $Ticket_Type In the if statement.
you have to correct this as well at the same place
So even after apply the above fix, your code wont work, cause you have second problem in this code
Now comes to the second problem, which you made in your if statement , in your if statement in here you checking $TicketType value, but you don’t have value for variable $TicketType because when you use the form php don’t set the variable it set the index so first you have to set the value for $TicketType than only you can use the if statement,
After applying both fixes your code should look like this
Note – (I am changing the name for select to TicketType )
echo ' <select name="Ticket_Type">';
foreach ($TicketType as $key => $value) {
echo "<option value=\"$key\">$value</option>";
}
echo '</select>';
$Ticket_Type = $_POST['Ticket_Type']; // Now you set the value for variable
// as your value is set, now you can type your if statement below.
if($Ticket_Type == 'VIP'){
$action = "index.php";
}else if ($Ticket_Type == 'VVIP') {
$action = "seats.php";
}
3. Select option, pass value only from the value tag
Third mistake you make and this one you will not notice with this code, but if you don’t learn, you will likely to make that mistake in the future, so correct this mistake as well,
Just pay attention to this code
$TicketType = array('VIP' =>'VIP', 'VVIP' =>'VVIP', 'Upper Box A'=>'Upper Box A', 'Upper Box B'=>'Upper Box B', 'Lower Box A'=>'Lower Box A', 'Lower Box B'=>'Lower Box B', 'General Admission'=>'General Admission') ;
echo ' <select name="Ticket_Type">';
foreach ($TicketType as $key => $value) {
echo "<option value=\"$key\">$value</option>";
}
echo '</select>';
In here you have array, and than you are creating select option from your array which is fine, but you are making one fundamental mistakes in here, form will only passes the value form the value tag
so come to this line in your code
echo "<option value=\"$key\">$value</option>";
now change variable to $key to variable $value as well, because when you click on the submit button, again form only passes the value from value.
echo "<option value=\"$key\">$value</option>"; to echo "<option value=\"$value\">$value</option>";
it wont change the out come, for this code, as in your array key and values are same, but you should know what value you are passing for the future.
To Understand what I am trying to say here, run these two code from below, you will understand what I am trying to say.
form.php
<form action="seat.php" method="post">
<select name="Ticket_Type">
<option value="key1">First</option>
<option value="key2">Second</option>
</select>
<input type="submit" value="Hot Air">
</form>
seat.php
<?php
$value = $_POST['Ticket_Type'];
echo $value;
run this file in the browser and than select the option, and click on the submit button, it will display you key1 or key2 depending on the option you choose, not the first or second, as form only submit the value from value tag
now change value tag value to One and two, and run your code again,
<form action="seat.php" method="post">
<select name="Ticket_Type">;
<option value="One">First</option>
<option value="Two">Second</option>
</select>
<input type="submit" value="Hot Air">
</form>
Now again run the file in the browser, and chose the option and click on the submit, and see the outcome, and try to understand what is happening.
4 Your logic is at the wrong place
see this code
if($Ticket_Type == 'VIP'){
$action = "index.php";
}else if ($Ticket_Type == 'VVIP') {
$action = "seats.php";
}
What your are trying to do here, in your head it make sense, that you will run the different different file,
according the ticket type, but in reality it does not make any sense, because when you click on the submit button, than only form will pass the value, to the action file,
this one is hard to explain
ok listen you have this variable $TicketType in this code, it only get set after you click on the submit button, and the action file it never going get set in this files, so you can not have these lines in this file. (and that's why it was showing to access forbidden after adding the echo)
Ok let's say you move this line to the different file, but still what you are trying to do with these lines is still silly, just don’t use these silly lines, and don’t try to run different different file, run single file, means give single action, and lets say action in the form is book.php
so our file name is book.php,
so starting shell for your book.php should look like this
if(!empty($_POST['Ticket_Type'])){
// what you are basically saying that if TicketType is not empty run this code,
// if it is empty run the else statement so the user will know that he did not select any ticket,
//now we are going to add some more code In here
}else 'Hey you did not select any ticket';
After adding the code in the middle our code will look like this
if(!empty($_POST['Ticket_Type'])){
$Ticket_Type = $_POST['Ticket_Type']; // set the value
if($Ticket_Type == 'VIP'){ //
// Copy and paste the index.php file code here
}else if ($Ticket_Type == 'VVIP') {
//copy and paste the seat.php file code here
echo $action;
}// if you have another Ticket_Type just add another else if here,
//Note: you can add as many else if you like,
}else 'Hey you did not select any ticket';
so just run single file action
i am still alive, after typing all this, if you have any confusion let me know.
Your Proble is here:
$TicketType = array('VIP' =>'VIP', 'VVIP' =>'VVIP', 'Upper Box A'=>'Upper Box A', 'Upper Box B'=>'Upper Box B', 'Lower Box A'=>'Lower Box A', 'Lower Box B'=>'Lower Box B', 'General Admission'=>'General Admission') ;
And your condition for if statment is
if($TicketType == 'VIP'){
$action = "index.php";
}else if ($TicketType == 'VVIP') {
$action = "seats.php";
}
$TicketType is an array and you can't check it with a string, so your $action variable is not setting. and you have to provide index to check.
I had a similar problem where my error read "Object not found!
The requested URL was not found on this server. The link on the referr..."
Going off of aria_suhail_123's answer, you simply need to remove any php code surrounding your $action variable in your html like so:
<?php
if($tickettype == 'VIP'){
$action = "seats.php";
}else if($tickettype == 'VVIP'){
$action ="seats1.php";
}
?>
<form action= $action method="post">
The reason why is explained by aria_suhail_123, but since his/her response is very long... maybe this will help someone else!
Consider this :
<?php
if(!empty($_POST['tickettype'])){
if($tickettype == "VIP"){
?>
<script type="text/javascript">
window.location = "./seats.php";
</script>
<?php
}else if ($tickettype == "VVIP") {
?>
<script type="text/javascript">
window.location = "./seats1.php";
</script>
<?php
}
}else 'Hey you did not select any ticket';
?>
Related
How do you validate a html unicode in php
I am passing values from HTML form to a php file for processing $to_do = $_POST['action']; Then i can say <?php if( $to_do == "delete") { echo "i will delete for you"; } ?> Difficulty i am having is when the HTML value is unicode ✘ for a delete symbol. instead of the value "delete". In php i cannot tell how to test it. <?php if( $to_do == "✘") { echo "i will delete for you"; } ?> is not working. Any one to help me out?
How are you sending this value to begin with - via something like <input type="submit" value="✘">? In that case, I would recommend you switch to a button element - that can have a separate submission value and display text. So you can keep sending delete, and show ✘ to the user at the same time. <button type="submit" value="delete">✘</button> More details on the button element can be found in the MDN, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
Radio Input disappear on page reload (enum, foreach, default checked, remember answer on page reload)
I might be on the wrong path (tiredness..) for the moment but i have passed 4 hours or something similar to debug my code. I have a form that is auto submitted when I click on a radio button, once clicked the next form appear and let me input the customer information. But when the page reload for displaying the other form, my variable $CustomerType is set and correct and, when i complete the input form (the second one) the php check if everything in it seems correct and it does, but it says my variable $CustomerType is missing then reload the page and ask me again to set the type. I can't paste all my code out here (~300 lines) but here is the core : <?php $_POST['CustomerType']="SOMEONE"; ?> // Ok so this was the trick, it solved the main bug but it now fix my choice to SOMEONE only. Can't change to another type <form method="post" action="<?php echo escaped_html($_SERVER['PHP_SELF'],'URL');?>"> <?php $array=show_enum("customer", "CustomerType"); $checked=""; foreach($array as $CustomerType) { $checked=""; if(isset($_POST['CustomerType'])) { if($_POST['CustomerType']==$CustomerType) $checked="checked"; } echo "<input type='radio' name='CustomerType' value=".$CustomerType." ".$checked." onClick='this.form.submit()'>".$CustomerType."</input>"; } ?> </form> EDIT Ok there is some news : by modifying the top who was : <?php $_POST['CustomerType']="SOMEONE"; ?> TO if(!isset($_POST['CustomerType'])) $_POST['CustomerType']="SOMEONE"; It seems to solve the second problem of the form, which couldn't let me change the type (auto-rollback to SOMEONE). But now, on form submit my choices are always rolling back to [CustomerType] => SOMEONE instead of SOMEBODY (and i checked SOMEBODY). It means that i can't hold the value $_POST['CustomerType'] on page reload for submitting. For example : This one which seems identical except that it's submitted with "save" button instead of onsubmit is working fine. $array=show_enum("customer", "Language"); foreach($array as $Language) { $checked=""; if(isset($_POST['Language'])) { if($_POST['Language']==$Language) $checked="checked"; } else if($Language=="FR") $checked="checked"; echo "<input type='radio' name='Language' value=".$Language." ".$checked." />"; string2languagepic($Language); } Picture of the problem *OnSubmit = onClick='this.form.submit()
After looking at your code a little more I think I have spotted your problem, try the following and see if it works. echo "<input type='radio' name='CustomerType' id='CustomerType' value='$CustomerType' $checked onClick='this.form.submit();' >" If that fails you could always add a hidden field, and when clicking on the radio button it adds a value to it and then submits the form.
Reset dropbox automatically to first value after the users hit submit button PHP
After the user hits the submit button, how do I reset the drop down menu to the "blank" option of the the menu? I am using a MVC set up with php and HTML, and the concrete5 library. THANKS IN ADVANCE! Here is what I have so far: Controller code: public function authorize() { $selectHost = array('' => ''); foreach ($this->host->getHostInfo() as $row) { if (isset($row['HARDWARE_id'])) { $selectHost[$row['id']] = $row['host']; } } $this->set('selectHost',$selectHost); $postCheck=array(array('param' => 'host', 'check' => '^[0-9]{1,50}$', 'error_msg' => 'Invalid Host ID'), ); $post = scrub($_POST,$postCheck); if (isset($post['host'])) { $this->host->authorize($post['host']); $this->set('test', "<p> The host has successfully been authorized.</p>"); } else{ $this->set('failed', "<p>Invalid Host ID</p>"); } } view code: <form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>"> <?php $form = Loader::helper('form'); print $form->label('host', 'Host: '); print $form->select('host', $selectHost); ?> <?php print $form->submit('Submit','Submit'); echo $test; echo $failed; ?> </form>
I'm pretty positive that there's no way to override C5's desire to take the POSTed value and use that as the default. Even if, as TWR suggested, you specify a value. (This is typically a good thing, because if the page is POSTed to and there's an error, you don't want to show the value from the database; you want to show what was in the POST). You can override the form helper pretty easily. However, I'd suggest that you do a redirect after successful submission (don't redirect after an error -- then the POSTed value will be useful) to a page. You can redirect to another page, or the same one, ideally with a confirmation message. See https://github.com/concrete5/concrete5/blob/master/web/concrete/core/controllers/single_pages/dashboard/blocks/stacks.php#L23 for an example of using redirect. This is the best practice for your problem but also because, with your current code, if somebody hits refresh, it'll rePOST the data and reauthorize the host.
i think you could extend the form tag with a (javascript) onsubmit action which does the reset.
Since it's a form submit, you just want to change the value of the "drop box"/select in your view. After a submit, you'll have a fresh page load; so, in every case you'll want to display the default value, and not the current value of $selectHost In your view, change this print $form->select('host', $selectHost); to this print $form->select('host', $selectHost, null); According to http://www.concrete5.org/documentation/developers/forms/standard-widgets
If the problem is that the Concrete5 form helpers are not behaving as you want, then just don't use them -- instead just use regular HTML form inputs instead. <form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>"> <label for="host">Host: </label> <select id="host" name="host"> <?php foreach ($selectHost as $value => $text): ?> <option value="<?php echo htmlentities($value); ?>"><?php echo htmlentities($text); ?></option> <?php endforeach; ?> </select> <input type="submit" value="Submit" /> <?php echo $test; echo $failed; ?> </form>
check if checkbox is checked in php
I have a check box list which I fill it with data from my table.Here is the code: <?php mysql_connect("localhost","root",""); mysql_select_db("erp"); $a="Select * from magazine"; $b=mysql_query($a); $c=mysql_fetch_array($b); while($c=mysql_fetch_array($b)) { print '<input type="checkbox"/>'.$c['den_mag']; echo "</br>"; } if(isset($_POST['den_mag'])) { echo "aaaa"; } ?> It's a simple query and for each data just show it with a checkbox.Now what I want is when I press a checkbox the value of that checkbox to be shown in a table.So if I have check1 with value a , check2 with value b and I check check1 the value a to be outputted to a table row.How can I achieve that? how cand I get which checkbox is checked?
A few notes: Try to avoid using SELECT * queries. Select the fields you are going to use: $sql= ' SELECT id, den_mag FROM magazine '; Use better variable names. $a and $c make your code harder to follow for others, and for yourself when you come back at a later time. Use more descriptive variable names like $query_object and $row. Your code should read almost like an essay describing what you're doing. In your form, use an array of elements. By giving the input a name like selected_magazines[], you will end up with an array in your post data, which is what you want -- multiple selections Use the row ID as the value of the checkbox element. Your array in POST will then be a list of all the IDs that the user selected Separate your logic from your HTML generation. The top portion of your script should take care of all logic and decisions. At the bottom, output your HTML and avoid making logical decisions. It makes for a script that is easier to follow and maintain, as well as debug. Here is a sample script incorporating these ideas with the details you've given: <?php // FILE: myfile.php mysql_connect("localhost","root",""); mysql_select_db("erp"); if(isset($_POST['selected_magazine'])) { // $_POST['selected_magazine'] will contain selected IDs print 'You selected: '; print '<ul><li>'.implode($_POST['selected_magazine'], '</li><li>').'</li></ul>'; die(); } $sql= ' SELECT `id`, `den_mag` FROM `magazine` '; $query_object=mysql_query($sql); $checkboxes = array(); while($row = mysql_fetch_array($query_object)) { $checkboxes[] = '<input name="selected_magazine[]" value="'.$row['id'].'" type="checkbox" /> '.$row['den_mag']; } ?> <form action="myfile.php" method="post"> <?php print implode('<br>', $checkboxes); ?> <input type="submit" value="Submit" /> </form>
<input name="test" type="checkbox" /> <?php if(isset($_REQUEST['test'])){ // selected } ?> When you give input-type elements (input, textarea, select, button) a name attribute (like I did), the browser will submit the state/value of the element to the server (if the containing form has been submitted). In case of checkboxes, you don't really need to check the value, but just that it exists. If the checkbox is not selected, it won't be set. Also, you need to understand the client-server flow. PHP can't check for something if the client does not send it. And finally, someone mentioned jQuery. jQuery is plain javascript with perhaps some added sugar. But the point is, you could in theory change stuff with jQuery so that it gets (or doesn't get) submitted with the request. For example, you could get jQuery to destroy the checkbox before the form is submitted (the checkbox won't be sent in this case).
Here you go : <html> <input name="test" value="true" type="checkbox" /> </html> <?php $Checkbox1 = "{$_POST['test']}"; if($Checkbox1 == 'true'){ // yes, it is checked } ?>
header('Location: ) in php switch to execute url with onclick function
To put it simply I have this variable which carries a hyperlink: $test3 = 'Move to Quotes'; 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: Trash Selected 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 = 'Move to Quotes'; 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>
First of all, you can't execute JavaScript from within PHP like this. At this point, the control has already moved to the server and JavaScript is run on the client-side. Second of all Im assuming you dont want to just follow the link, you want to run the link's onClick event, since the href is just a hashtag. So you are trying to run a JavaScript function with PHP. You cant call a function in one language from a function in another language. Its hard to tell what exactly you are trying to do, but if you want to run a function when a user selects a certain dropdown, write a php function that does what archiveMove() does. If you want this to happen without a page refresh, you can stop the submit process and call your archiveMove() function with javaScript and Ajax. If elaborate on what exactly you are trying to do, maybe we can help more. Ok, so the only difference between your working code and the not working code is that you want to dictate the submitted URL based on what is selected in the dropdown? So you can use JavaScript to set the form action when the dropdown is selected. BUT, It might be a better idea to submit the form with the same action everytime, and then use PHP to decide what to do. It seems like this is where you were headed initially. Just get the folder id in the switch statement and call a function to make your edits: The PHP can be similar to the way you had it: switch ($_POST['dropdown']) { case "Two": // set folder id $folder_id = 2; break; } moveMessages($_POST['Messages'], $folder_id); function that moves the messages where they need to go. function moveMessages($messages, $folder_id){ // depending on your form setup foreach($data as $id => $value ){ if($value){ // code to move to folder } } return true; } If there are other factors involved, let me know.
You can write JavaScript code that request a url using window.location.href in click hadler. window.location.href="http://example.com";
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>