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';
?>
I have researched many places to find an answer to this question, but they never quite answer my real question: What is the best/approved way to move to a new page within the same website? I have read that it is bad to use window.location because search engines will think you are hiding something. But, when I don't want to open a new window (window.open), then I don't know how else to do it. I use href anchors in links and form actions, where appropriate. But when I have menus or buttons with onclick, then I need something else.
Here's an snippet of my code:
my javascript: (with one option commented)
function gotoCat() {
var catcdF = document.catSelect.catcd.value;
<?php
echo "window.location.href='http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF; ";
/*
echo "window.open('http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF,'','resizable=1,scrollbars=1,toolbar=1,top=50,left=300,width=950,height=800,location=0'); ";
*/
?>
}
My dynamic SELECT list in a form (within PHP):
echo " <select name='catcd' id='catcd' size='8' onclick=gotoCat() > \n";
// display list of categories
if ($numcats == 0) { // print message text only
echo "<option value='0' >".$catMsg."</option> \n";
}
else {
for ($i=1; $i<=$numcats; $i++) {
$catcd_db = $catAry[$i][1];
$catName_db = $catAry[$i][2];
echo "<option value='".$catcd_db."'> ".$catName_db." </option> \n";
}
}
echo "</select>";
So, as you can see, I just want a method to allow the user a choice and then automatically go to the correct web page once selected. This is not always in a select list. Often it's when they want to exit or get an error:
if (mysqli_connect_errno()) {
echo "<br/> <p style='text-align:center;'> <button type='button'
class='buttonStyle' style='padding: 4px 20px;' value='Exit' ";
echo "onClick=\"window.location.href='http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev'\" > ";
echo "Exit </button></p> ";
}
I cannot use "go back" because they need to go to a prior page, not the form they came from.
So, unless my navigation methods are really off-the-mark, I guess I need to know the acceptable method for using javascript onClick to move to the next page in the same website. Is window.location okay, or should I use something else?
Any opinions or suggestions are welcome!
To navigate to another page using Javascript, use:
window.location.href = "url";
That's how it's done and there's nothing wrong about it.
For the sake of argument, you could create a hidden link and simulate a click on it, but as I said, there's really no need.
You can use php header('location') instead:
<form action="submit.php">
<input type="hidden" value="test" name="hidden1" />
<input type="submit" Value="Exit" ... />
submit.php
<?php
if (isset($_POST['hidden1'])
{
header('Location: http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev');
exit;
}
?>
More info about header('Location ...');:
http://php.net/manual/en/function.header.php
Instead of a hidden, you use your select's value and get it via the $_POST variable.
I have a huge form and in part of the form I want to insert some value to database using js. I might not be clear about how to present the question but here my needs are:
suppose, I have two tables in database table1, table2. In a html form:
<select name="tab1" id="tab1">
<?php while($row = fetch from table 1){ ?>
<option value"<?=$row['name']?>" name="option1"><?=$row['name']?></option>
<?php } ?>
</select>
<input type="file" name="file">
<input type="button" name="button" onclick="submit_form(true,'');">
Now, I want to pass the $row['name'] value to submit_form() function in javascript. The javascript code will check the value and return it to the form to submit it. My question is since the $row['name'] from table1 is inside the while loop, I cannot pass the value to javascript. If the form was small I could have done using submit button and check $_POST('submit') type. I want to insert the $row['name'] in this form to table2 as file name associated with the name.
As i understand you want to pass selected value from form to submit_form() function?
function submit_form(param1, param2){
var passedValue = document.getElementById('tab1').value;
// here is your old submit_form() function. passedValue contains
// your selected $row['name']
}
#Jhilke Dai, First of all your php code is little buggy, '=' sign must be in html not in php the correct code is
<select name="tab1" id="tab1">
<?php while($row = fetch from table 1) { ?>
<option value="<? echo $row['name'] ?>" name="option1"><? echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="file" name="file"> <input type="button" name="button" onclick="submit_form(true,'')">
You can use generic functions or even jQuery itenerations, to fetch form values
See the similar question answer : Get selected value/text from Select on change
function getDomValueByID( id ) {
return document.getElementById(id).value;
}
function submit_form( a, b ) {
var formValue = getDomValueByID( 'tab1' );
//OR
var jQueryFormValue = jQuery( "#tab1" ).val();
//Do what u want here.
}
In fact several consider it a very bad idea to pass the option data over via javaScript, if its already generated on page for the following reasons
Duplicate data, wasted bandwith.
Less portable code, non-OOP.
Harder to maintain, changes in your php code, requires changes in your javaScript code.
Also if you are really interested (this practice is sometimes frowned on). You can use the following as PHP code somewhere in the header. To pass PHP variables to JavaScript. However there are lots of better ways to do this, from JSONS to XML.
<?php optList = ['one', 'two', 'three']; ?>
<script type="text/javascript">
//Window represents the global variable space, and doing this is really bad practice as listed above.
window.optionList = [ <?php echo( implode(' , ', optList) );?> ];
</script>
I have two drop down lists.
Second one is populated based on value chosen in the first one. I'm using Double Combo Script Credit By JavaScript Kit to do that (I am very bad with javascript).
I use this to filter results from my Mysql database.
The problem is that when user applies filter i want him to see what he applied (when page refreshes or user goes to other page) - those values should be seen as selected in both drop down lists. I can't figure out where i should place an event or something else.
I'm holding subcategory values from the second drop down list in php session :
if (isset($_SESSION['subcat']) && !isset($_GET['subcat'])){
$color= $_SESSION['subcat'];
}
elseif (!isset($_SESSION['subcat']) && isset($_GET['subcat']))
{
$_SESSION['subcat'] = mysql_real_escape_string($_GET['subcat']);
$color= $_SESSION['subcat'];
}
elseif (isset($_SESSION['subcat']) && isset($_GET['subcat'])){
unset($_SESSION['subcat']);
$_SESSION['subcat'] = mysql_real_escape_string($_GET['subcat']);
$color= $_SESSION['subcat'];
}
else {
$color= "";
};
I can echo selected in first drop down list, based on session value and that works, but a second one drop down list is not generated when page refreshes and i don't know where should i echo 'selected = "selected"' or maybe everything can be done only with javascript? Please help.
The code:
<div class="filter">
<form method="get" name="doublecombo" action="" id="filterform" >
<select name="example" id="exampl" size="1" onChange="redirect(this.options.selectedIndex)">
<option>All kinds</option>
<option>Women</option>
<option>Men</option>
</select>
<select name="subcat" size="1" id="subcategory">
<option value="lists.php">All colors</option>
</select>
<input type="button" name="test" value="Filter" onClick="go()">
</p>
<script>
<!--
/*
Double Combo Script Credit
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free JavaScripts here!
*/
var groups=document.doublecombo.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()
group[0][0]=new Option("All colors","list.php")
group[1][0]=new Option("Pink","list.php?subcat=1 ")
group[1][1]=new Option("White","list.php?subcat=2")
group[1][2]=new Option("Green","list.php?subcat=3")
group[2][0]=new Option("Black","list.php?subcat=12")
group[2][1]=new Option("Blue","list.php?subcat=13")
group[2][2]=new Option("Grey","list.php?subcat=14")
group[2][3]=new Option("Brown","list.php?subcat=15")
var temp=document.doublecombo.subcat
function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
}
function go(){
location=temp.options[temp.selectedIndex].value
}
//-->
</script>
</form></div>
you could set a cookie to hold the selected value, so if the user selects there choice and refreshes, you would then check if the cookie exists and then populate the menus accordingly.
Update:
This will store the selected values and repopulate the select menus if the user refreshes the page.
First select added onkeup:
<select name="example" id="exampl" size="1" onchange="redirect(this.options.selectedIndex)" onkeyup="redirect(this.options.selectedIndex)">
for the second select and as follows to check for changes
<select name="subcat" size="1" id="subcategory" onchange="checks(this)" onkeyup="checks(this)">
Now find the Line temp.options[0].selected=true and add this directaly below
createCookie("selected_option_1", x, 0);
if(x==0){
eraseCookie("selected_option_2");
}
then add these two new function say at the bottom of your script block
// checks if the Second Select has changed
function checks(oWhich){
createCookie("selected_option_2", oWhich.selectedIndex, 0);
}
// repopulate the options base on selection thats saved in the cookies
onload = function(){
if(readCookie("selected_option_1") != null) {
redirect(document.doublecombo.example.options.selectedIndex = readCookie("selected_option_1"));
if(readCookie("selected_option_2") != null) {
document.doublecombo.subcat.options.selectedIndex = readCookie("selected_option_2");
}
}
}
Finaly for these functions/scrip to work you will need
// The cookie script im using for the functions is located below include this and you chould ok. http://www.quirksmode.org/js/cookies.html#script
Now once the form has been submitted you GET the selected values as usual, and the REPOPULATE the menu, once you done with the cookie you could remove them.
If it's jQuery you are using you can try a short PHP tag on the page like this:
jQuery('#MyDropDown').val('<?php echo $_SESSION['MyStoredValue']; ?>');
If you are not using jQuery but straight JavaScript this would have the same effect:
document.getElementById("MyDropDown").value = '<?php echo $_SESSION['MyStoredValue']; ?>'