PHP - How Can I Get The Value Of A Switch Case Coming From The Drop Down Form?
Hi I have a form that is popluated from the database now I am trying to get the switch case to be based on the option the user selects is this possible to make a case a variable popluated by the drop down if so can someone show me what am i missing?
the value I am trying to get from the form to the switch case is the ( $val->slug) but the value not been set help please here the form Code: [Select] <form method="post"> <select name="deal-locations" onChange="this.form.submit()"> <?php while(list($key,$val) = each($locations)) { ?><option value="<?php echo $val->slug; ?>"><?php echo $val->slug; ?></option><?php } ?> </select> <input type="submit" value="submit"> </form> Here the switch case Code: [Select] switch ($_POST['deal-locations']) { case " $val->slug": echo "<h3>deal-locations: ASC</h3>"; $args = array( 's' => $_GET['s'], 'post_type' => 'deals', 'deal-locations' => ' $val->slug', 'where' => ' $val->slug', 'paged' => 'paged' ); break; } Similar TutorialsHi, I have been using an old contact form for a while now, this is it: Code: [Select] <?php if (isset($_POST['submit'])) { switch($_POST['contact_type']) { case 'Customer': $to = '...@gmail.com'; break; case 'HQ'; $to = '...@gmail.com'; break; case 'Office 2': $to = '...@gmail.com'; break; case 'Office 3': $to ='...@gmail.com'; break; case 'Office 4': $to = '...@gmail.com'; break; case 'Office 5': $to = '...@gmail.com'; break; } $subject = "WEBSITE ENQUIRY"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; if (strlen(trim($message)) > 0) { $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if (mail($to, $subject, $body)) { echo "<h3>Thanks! Your email has been sent <br />We will answer your enquiry as soon as possible.</h3>"; } else { echo 'Cannot sent mail'; } } else { echo "<h3>Error! <br />Please ensure that all fields are filled out in order to email us!</h3>"; } } else { echo "blarg!"; } ?> I recently extended the form with HTML5 etc and got a new form script, which makes a lot of improvements with the PHP. I want to put this on a site I am working on. The current form has an contact type pull down which changes the destination of the submitted form. It looks like this: Code: [Select] <?php if( isset($_POST) ){ //form validation vars $formok = true; $errors = array(); //sumbission data $ipaddress = $_SERVER['REMOTE_ADDR']; $date = date('d/m/Y'); $time = date('H:i:s'); //form data $name = $_POST['name']; $email = $_POST['email']; $telephone = $_POST['telephone']; $company = $_POST['company']; $enquiry = $_POST['enquiry']; $product = $_POST['product']; $message = $_POST['message']; //validate form data //validate name is not empty if(empty($name)){ $formok = false; $errors[] = "You have not entered a name"; } //validate email address is not empty if(empty($email)){ $formok = false; $errors[] = "You have not entered an email address"; //validate email address is valid }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $formok = false; $errors[] = "You have not entered a valid email address"; } //validate company is not empty if(empty($company)){ $formok = false; $errors[] = "You have not entered a name"; } //validate message is not empty if(empty($message)){ $formok = false; $errors[] = "You have not entered a message"; } //validate message is greater than 20 charcters elseif(strlen($message) < 20){ $formok = false; $errors[] = "Your message must be greater than 20 characters"; } //send email if all is ok if($formok){ $headers = "From: user@mysite.com" . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p> <p><strong>Name: </strong> {$name} </p> <p><strong>Email Address: </strong> {$email} </p> <p><strong>Telephone: </strong> {$telephone} </p> <p><strong>Company: </strong> {$company} </p> <p><strong>Enquiry: </strong> {$enquiry} </p> <p><strong>Product of Interest: </strong> {$product} </p> <p><strong>Message: </strong> {$message} </p> <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>"; mail("....@gmail.com","New Enquiry",$emailbody,$headers); } //what we need to return back to our form $returndata = array( 'posted_form_data' => array( 'name' => $name, 'email' => $email, 'telephone' => $telephone, 'company' => $company, 'enquiry' => $enquiry, 'product' => $product, 'message' => $message ), 'form_ok' => $formok, 'errors' => $errors ); //if this is not an ajax request if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){ //set session variables session_start(); $_SESSION['cf_returndata'] = $returndata; //redirect back to form header('location: ' . $_SERVER['HTTP_REFERER']); } } As you can see the new php form is much better, validates each field etc... I am having issues getting the case and switch statement into my new form though, can anyone please advise where to put it / what I need to take out to make it work? Its an important part of the form I need to make. Any help would be much appreciated. can i do something like this? Code: [Select] <?php switch ($i) { case 0: echo "i equals 0";?> ////////////// html stuff //////// <?php break; case 1: echo "i equals 1"; ?> ///////////////////html stuff////////////// <?php break; case 2: echo "i equals 2"; ?> ////////////html stuff/////////////// <?php break; } ?> Hello, I am having a small issue with a case switch array that uses a range of data. I am using it to set the background of table cells depending on the data. I.E. 0-0.9, 1-1.9 etc with each one being a different color. Issue I am having is it doesn't change. It sets it based on the first array entry. What might I be missing? This is the first time I have used a case switch with a range.
-Thanks
$magScale = ''; switch($magnitude) { case myInterval >= 0 && myInterval <= 0.9: $magScale = 'rgb(195, 218, 236)'; break; case myInterval >= 1 && myInterval <= 1.9: $magScale = 'rgb(210, 238, 197)'; break; case myInterval >= 2 && myInterval <= 2.9: $magScale = 'rgb(244, 240, 202)'; break; case myInterval >= 3 && myInterval <= 3.9: $magScale = 'rgb(244, 223, 202)'; break; case myInterval >= 4 && myInterval <= 4.9: $magScale = 'rgb(240, 199, 205)'; break; case myInterval >= 5 && myInterval <= 10: $magScale = 'rgb(212, 195, 236)'; break; } Hi, I have the following code: Code: [Select] <?php $cmd = $_GET['cmd']; if($cmd=="") { $cmd = "adminlogin";} // This creates the header for each of the installation pages switch($cmd) { // This is the installation agreement page case "adminlogin": print <<<LOGIN <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Member Site Maker 1.0</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="head" align="center"> <h1 id="siteName">Member Site Maker </h1> <br /> <table align="center" border="0" bgcolor="#CCCCCC"> <tr> <td align="center"><span class=style1><b>ADMIN LOGIN</b></span></td> </tr> <tr> <td> <form action=admin.php?cmd=manage method=POST> Password: <input type=text name=password1> </td> </tr> <tr> <td> <input type=submit name=submit value=Submit> </td> </tr> </table> </form> LOGIN; break; // Managing Users case "manage": include_once("header.html"); include_once("data/password.php"); $password1 = $_POST['password1']; $password2 = base64_decode($password); if ($password1 != $password2) { print <<<BADLOGIN <table width=953 border=1 align=center bgcolor=#00CCFF> <tr> <td><span class=style1><b><center>Failed Login</center></b></span></td> </tr> <tr> <td><span class=style2>Your passwords do not match. Please go back and correct this error</td> </tr> </table> BADLOGIN; } else { echo <<<MANAGE <!--end navBar2 div --> <div id="navBar2"> <div id="sectionLinks"> <ul> <li><a href="admin.php?cmd=manage&password1=$password1">Manage</a></li> <li><a href="admin.php?cmd=dashboard&password1=$password1">Dashboard</a></li> <li><a href="admin.php?cmd=approval&password1=$password1">Approval</a></li> <li><a href="admin.php?cmd=msgcentre&password1=$password1">Message Center</a></li> <li><a href="admin.php?cmd=logins&password1=$password1">Logins</a></li> </ul> </div> </div> <!--end navBar2 div --> <div id="content"> <div class="story"> <table width="100%" border="0"> <tr> <td bgcolor="#99FF66"><div align="center"><span class="style3">Login</span></div></td> <td bgcolor="#99FF66"><div align="center"><span class="style3">Name</span></div></td> <td bgcolor="#99FF66"><div align="center"><span class="style3">Last Visited </span></div></td> <td bgcolor="#99FF66"><div align="center"><span class="style3">Registration Date </span></div></td> <td bgcolor="#99FF66"><div align="center"><span class="style3">Reset Password </span></div></td> <td bgcolor="#99FF66"><div align="center"><span class="style3">Delete</span></div></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> </table> <h3> </h3> </div> </div> <!--end content --> MANAGE; } break; case "dashboard": break; case "approval": break; This works fine for when viewing the admin.php, I am asked for a password and then it compares the password against the encoded password before displaying the manage page. However this does not stop someone typing http://www.mysite.com/folder/admin.php?cmd=dashboard If they do that, it skips the password form and password check, and they can then go ahead and do whatever in the admin.php file. How can I prevent this, so that a password check is automatically done before allowing somebody to view the page? I have tried adding the code I used in the manage section, but it doesnt work again. Any help will be greatly appreciated, I been trying to work it out all day and run out of ideas. Many Thanks I'm testing out small bits of code for myself, and I'm trying to understand why this isn't coming out how I would imagine it to... I have my function which stores colors in an array from a text file, then I return back a random color from my array like so... function getColor() { $ball_color = explode("\n", file_get_contents('colors.txt')); // Takes file contents and stores them into an array. return $ball_color[array_rand($ball_color)]; } And now in the main program, I simply have this: $color = getColor(); switch($color) { Case "blue": // echo stuff break; Case "red": // echo stuff break; etc... } For some reason though, my switch statement always reads the default case, making me assume that the words "blue" "red" "green" etc... aren't matching up to what I have set the cases at. When I output what color is actually being read, it displays exactly as it should, so I don't understand why the case "red" isn't opening up when that color is chosen, or same with any of the other colors. Is there some small conflict I'm unaware of in this tiny practice code? Any help is appreciated, thanks. Hello, i want to reproduce a content switcher based on drop-down menu (example: http://www.infocercetare.ro/index.php) anyone can help me with the logic of the function the rest i think i can handle. best regards I am using a captcha like image verification for my form fields. I want to make my check case insensitive: For example, if the security word is 1A3e then 1a3e or 1a3E should work.(Upper Case or lower case does not matter) Here is the check I am doin so far to see if my security word matches. Code: [Select] if(md5($_POST['security_word']).'a4xn' == $_COOKIE['tntcon']) Hi i am trying to change the query to sort by desc or asc I am using the switch method but the form is not calling the switch statment can someone help me out please? this is the swicth script Code: [Select] switch ($sortby) { case "ASC": $args = array( 's' => $_GET['s'], 'post_type' => 'deals', 'orderby' => 'title', 'order' => 'ASC', 'paged' => get_query_var('paged') ); break; case "DESC": $args = array( 's' => $_GET['s'], 'post_type' => 'deals', 'orderby' => 'title', 'order' => 'DESC', 'paged' => get_query_var('paged') ); break; }This is the form Code: [Select] <form name="myForm"> <select id="sortby" > <option value="ASC">ASC</option> <option value="DESC">DESC</option> </select> </form> I intend to use a onchange so that when depending on the option they select it will change the order? Morning, I'm working on a web site using a switch to separate different calls from a MySQL database. My style looks like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } What I am trying to do, is specify that if the page is news, there can be an additional variable for id. I attempted before and after the break; for the news case, to make a new switch, but it isn't taking. I've looked around online, but can't find any information about this. Can anyone offer a hand? My error: Code: [Select] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key FROM applications WHERE key = '0188f'' at line 1 Code: function randomNum() { $random = substr(md5(rand(5000,300000)), -5); $query = mysql_query("SELECT key FROM applications WHERE key = $random") or die(mysql_error()); //make sure it doesn't already exist if(mysql_num_rows($query) > 0) { randomNum(); } else { return $random; } } I simply cannot seem to figure out what is happening...I've tried many solutions, but all seem to fail. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <?php // Connect to database================================================== include("connect_db.php"); // sending query =========================================================== $result = mysql_query("SELECT * FROM members Order By last") or die(mysql_error()); if (!$result) { die("Query to show fields from table failed"); } echo "<br />"; echo "<p>MEMBERS LIST</p>"; echo "<table border='10' cellpadding='3' cellspacing='2'>"; echo "<tr><th>First Name</th><th>Last Name</th>><th>Phone</th><th>Email</th></tr>"; // keeps getting the next row until there are no more to get ================ while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table ========================== echo "<tr><td>"; echo $row['first']; echo "</td><td>"; echo $row['last']; echo "</td><td>"; echo $row['phone']; echo "</td><td>"; echo $row['email']; echo "</td></tr>"; } echo "</table>"; ?> </body> </html> This is the output. MEMBERS LIST > First Last Phone Email data data data data ect............................................................... I cannot find where the > (greater than) sign is coming from. There is a space after MEMBERS LIST but I could not get it to work in this display. Okay, so, I'm trying to pull the info from my `shop` table where the `users` table matches at x, y, z, and plane. Instead, I'm getting this: Resource id #22 Where is this coming from? I only have 1 row in my shop table, and it has: id = 7 name = admin shop x = 1 y = 0 z = admin plane = batai I'm standing in the shop right now (as my character, so my users table has the exact same x, y, z, plane)... Any help would be appreciated! $q1 = mysql_query("SELECT * FROM `shop` WHERE `x`='".$x["x"]."' AND `y`='".$x["y"]."' AND `z`='".$x["z"]."' AND `plane`='".$x["plane"]."'") or die("qry failed: ".mysql_error()); Variables: $x = $x = $player->username; (so it's pulling my username, which it is doing correctly ==> i echoed out $x and it came up with my username) Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\page\verify_security.php on line 170
earlier the code has been running fine and now it is showing error to me , can you tell something more about it ???
Hi, I just started learning PHP... Below is my first programme infact.. Out put is not coming when i type some thing in input boxes... Can somebody suggest where is the error... Index.php <html> <head> <title>Welcome to our site</title> </head> <body> <form method="POST" action="submit.php"> <label for ="business_name">Business name</label> <input type="text" id="business_name" \><br/> <label for ="contact_person">Contact person</label> <input type="text" id="contact_person" \><br/> <label for ="Telephone">Telephone</label> <input type="text" id="Telephone" \><br/> <label for ="FAX">FAX</label> <input type="text" id="FAX" \><br/> <label for ="Mobile">Mobile</label> <input type="text" id="Mobile" \><br/> <label for ="Addres_line_1">Addres_line_1</label> <input type="text" id="Addres_line_1" \><br/> <label for ="Addres_line_2">Addres_line_2</label> <input type="text" id="Addres_line_2" \><br/> <label for ="Addres_line_3">Addres_line_3</label> <input type="text" id="Addres_line_3" \><br/> <label for ="Addres_line_4">Addres_line_4</label> <input type="text" id="Addres_line_4" \><br/> <label for ="City">City</label> <input type="text" id="City" \><br/> <label for ="Pin_code">Pin_code</label> <input type="text" id="Pin_code" \><br/> <label for ="State">State</label> <input type="text" id="State" \><br/> <label for ="email">email</label> <input type="text" id="email" \><br/> <input type="submit" value="Please submit the details" name="submit"/> </form> </body> </html> submit.php <head><title>Your submission details</title></head> <body> <H3>YOU HAVE SUBMITTED THE FOLLOWING DETAILS</H3> <?php $business_name= $_POST["business_name"]; $contact_person = $_POST["contact_person"]; $Telephone = $_POST["Telephone"]; $fax = $_POST["FAX"]; $mobile = $_POST["Mobile"]; $address1 = $_POST["Addres_line_1"]; $address2 = $_POST["Addres_line_2"]; $address3 = $_POST["Addres_line_3"]; $address4 = $_POST["Addres_line_4"]; $city = $_POST["City"]; $pincode = $_POST["Pin_code"]; $state = $_POST["State"]; $email = $_POST["email"]; echo "Your business name".$business_name.'<br/>'; echo "Contact person".$contact_person.'<br/>'; echo "Telephone no.".$Telephone.'<br/>'; echo "Fax No.".$fax.'<br/>'; echo "Mobile No.".$mobile.'<br/>'; echo "Address line one".$address1.'<br/>'; echo "Address line two".$address2.'<br/>'; echo "Address line three".$address3.'<br/>'; echo "Address line four".$address4.'<br/>'; echo "Your City".$city.'<br/>'; echo "Your Pincode".$pincode.'<br/>'; echo "Your State".$state.'<br/>'; echo "Your email".$email.'<br/>'; ?> <a href=index.php>Home</a> </body> </html> Hi everyone reading this, I have used PHP before as a hobby programmer but recently my boss asked me to make a webpage to accept worker's hours and store them in a database. So I realize that in the last few years object oriented style has been added into PHP. I have learnt OOP (C++) about a decade ago and understand the fundamentals of it. If I am writing this website code, am I going to be better off spending the time to get familiar with the new OOP stuff in PHP and use that or would it be better to just use an old sequential style of programming? I know this may seem like apples and oranges and that it varies based on opinion, but maybe there can be reasons for me to use OOP over sequential. For instance, if I write this code to be flexible enough that I can do a basic "worker times" table first, then I might be able to add additional functionality later, like worker details and qualifications, all of which could be brought up with the click of a button. Will it generally be easier to add onto code using OOP? if any body copy and paste anything from word to editor some unwanted css also coming with that pasting. when we request the data same css also coming with that. so how to clean data when we request $desc =$_request['contents']; how to solve this issue. please help me. Hello all, I am trying to create a PHP CLi Script that will help me setup new config and server files / directories for every new web project i get instead of me having to manually create everything with each project. The script accepts the project name, and y/n for whether i want to create subdomains for css, jss and image files. After it has that, it must create a new http config file under sites-available (in Linux), by pasting the config file text. Problem : If I output regular text to the file, it displays exactly in the format that I write the string variable. But when I use a printf, or try to use variables directly in the config text, it starts formatting the text in the config file in a strange way. I just want the variables to just be replaced and the text to appear in the exact format below. The text displays perfectly as i typed below in the config file if I don't use any variables. $subDomainConfig .= '<VirtualHost *:80> ServerAdmin webmaster@%1$s ServerName %1$s.%2$s ServerAlias %1$s.%2$s # Indexes + Directory Root. DirectoryIndex index.html index.php DocumentRoot /var/www/%2$s/public_html/ # CGI Directory ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Location /cgi-bin> Options +ExecCGI </Location> # Logfiles ErrorLog /var/www/%2$s/logs/apache/error.log CustomLog /var/www/%2$s/logs/apache/access.log combined </VirtualHost> '.PHP_EOL; $httpConfigFileText = sprintf($subDomainConfig, $subDomain, $shortProjectName); The above code outputs text in this way in the file : <VirtualHost *:80> ServerAdmin webmaster@images ServerName images.mysite ServerAlias images.mysite # Indexes + Directory Root. DirectoryIndex index.html index.php DocumentRoot /var/www/mysite /public_html/ # CGI Directory ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Location /cgi-bin> Options +ExecCGI </Location> # Logfiles ErrorLog /var/www/mysite /logs/apache/error.log CustomLog /var/www/mysite /logs/apache/access.log combined </VirtualHost> Hi i need help iv create an event upload on the back-end of a website where a user can enter name, location and date of an event into sql database which works fine, the problem is that i want to display only current and up and coming events and not display the events that have passed if anyone can help that would be great hello, I have a search that matches users ISBN with two databases....code is below $query_search_exact_match = mysql_query("SELECT nvc_site.title, nvc_site.id, nvc_site.description, nvc_site.search_text, nvc_site.image, nvc_site.date, nvc_site.price, nvc_site.location_city, nvc_site_ads_extra.name, nvc_site_ads_extra.value, nvc_site_ads_extra.classified_id FROM nvc_site,nvc_site_ads_extra WHERE name = 'ISBN%3A' AND live=1") or die(mysql_error()); then i take the ISBN that the user entered and match that with the isbn's in the DB while ($fetch_extra = mysql_fetch_array($query_search_exact_match)) { $value = ereg_replace( "[^0-9]", "", $fetch_extra['value'] ); $to_find_isbn = mysql_real_escape_string(ereg_replace( "[^0-9]", "",$_POST['szs'])); if($value == $to_find_isbn) { echo "Match Found"; } else { echo "No Match Found"; } //ELSE doesn't work here.....it displays both the if and else at the same time } i need it to display no match found if there was no match found.....I am soo lost right now!! please help thank you 0
What is the best way to store this data coming from the api into a csv file to later put into the db. Output: rank, level, xp, rank, level, xp, etc. This api produces about 60 rows of data per name ran and x that by about roughly 300 names that equals a lot of data. Pretty much with my current code I am pretty much creating a endless loop almost that would take a long time to execute so updating said data would be a nightmare i would think. Is there a way to accomplish the same thing without the loop or I am not sure how to go about this. My current code is a mess that much I know I am gonna be told. This code works, just not efficiently. I am thinking there may be a better way to do this. $query = $conn->prepare("SELECT name FROM users LIMIT 1"); $query->execute(); while($row = $query->fetch(PDO::FETCH_ASSOC)){ $name = $row['name']; $url = 'https://secure.runescape.com/m=hiscore/index_lite.ws?player='. $name . '';//api that the csv data is coming from $highscores = file_get_contents($url); $fields = array("Name", "Rank", "Level", "Xp");//this is to add the headers // for the csv files $implode1 = implode($fields, ",");//turn into csv format, not sure this is //even needed $implode1 .= "\n";/*this is to add a line break so that the explode below will properly put it into its own element*/ //otherwise data starts joining togather $extra_data = array("$name");/*This is to add the name that the data pertains too*/ $implode2 = implode($extra_data, ",");//turn into csv format $highscores = $implode1 . $implode2 . $highscores;//join as one array $highscores = explode("\n", $highscores);//turn each csv into an element of //its own element a bunch of unsets to remove unwanted data. Omitted them to condense the code $i = 1; header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="name.csv"'); $data = $highscores; $fp = fopen('highscores/' . $name . '.csv', 'wb'); foreach ( $data as $line ) { $val = explode(",", $line); fputcsv($fp, $val); } fclose($fp); The pdo part I was gonna include but the way I have it setup now wouldn't work. I was thinking that I would use mysql's LOAD DATA INFILE to insert the csv data to database. The end goal here is to be able to search for player stats and to use said data to keep track of xp earned during a xp competition. I PRAY that i have included enough info... I would really appreciate any feedback if possible. |