PHP - If/else Redirects After Form Submit...
I am fairly new to PHP forms so I am trying to adapt an example that I found online. Right now it IS functioning... showing a error message OR showing a success message and sending all the form information to my email account.
I would like to edit it to redirect to a specific page if it has an error (http://www.mosaleen.com/contact_error.html) and to another if it sends successfully (http://www.mosaleen.com/thankyou_email.html) I am a bit confused by how the script is looped at the end. However, if I delete out that last few lines of coding, the form stops working. I am attaching the code below. Keep in mind that I am a newbie! Code: [Select] <?php // Turn on error reporting. Handy for debugging. error_reporting(E_ALL ^ E_NOTICE); // The following parameters are pretty much all that you // need to change except for the format of the email message // below. Note that your mail server may require the mailTo // address be in the host domain. $mailTo = "****"; // The address that will receive form submissions $mailSubject = "Comments from Mosaleen"; // Whatever you want $mailHost = "****"; // Usually looks like mail.yourhost.com $mailPort = "25"; // Usually 25 $mailAuth = true; // "true" if your mail server requires authentication, "false" if not $mailPassword = "****"; // The mail password associated with $mailTo // If you want to get cookies values... // $myCookie = $_COOKIE["cookiename"]; // Get the form fields. $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $message = $_POST['message']; // If there are errors, display them and a back button. if($errorMessages) { ?> <p>Errors were found on the form.</p> <ul> <?php echo $errorMessages; ?> </ul> <p><input type="button" value="Back" onClick="history.back()"></p> <?php } // No errors, send the message and print out success message. else { // These can sometimes be useful, although you should not // violate your site's privacy policy. $browser = $HTTP_USER_AGENT; $ip = $_SERVER['REMOTE_ADDR']; // Build the email.<br /> $body = " Name: $name Email: $email Phone: $phone Message: $message -------------------- Browser: $browser User IP: $ip"; include("Mail.php"); $headers["From"] = "contact@mosaleen.com"; $headers["To"] = $mailTo; $headers["Subject"] = $mailSubject; $params["host"] = $mailHost; $params["port"] = $mailPort; $params["auth"] = $mailAuth; $params["username"] = $mailTo; $params["password"] = $mailPassword; $mail_object =& Mail::factory("smtp", $params); $mail_object->send($mailTo, $headers, $body); ?> <h1>Thank You</h1> <p>Thank you for contacting us. We will be in touch with you shortly.</p> <?php } ?> Similar TutorialsHello everyone. I am writing this order/feedback form using a php script.
Every time i hit submit, the html page (that has the form inside) just reloads and does not perform the script
could anyone give me some pointers in my code the form/php?
I have made sure they are linked verbatim
I am a nube so please forgive my inexperience
Here is the form in the html , down below it is the corresponding php code for the form
Say I have an "Entries" table. I want to submit same multiple entries using a form submission. And If I have other queries submitted in the same form, I want those quarries to be submitted only once. Is that possible to do? Here's my code. if(isset($_POST['submit'])) { $entries = 10; $id = 55; $name = 'Smith'; $insert = $db->prepare("INSERT INTO entries(id, name) VALUES(:id, :name)"); $insert->bindParam(':id', $id); $insert->bindParam(':name', $name); $result_insert = $insert->execute(); if($result_insert == false) { echo 'Fail'; } else { echo 'Success'; } } ?> <form action="" method="post"> <input type="submit" name="submit" value="SUBMIT" /> </form> Edited January 13, 2019 by imgrooot Hi. Pretty straight forward I guess but as the name suggests am a newbie. I have a form that requires the user to enter certain parameters. If the values are blank it submits to itself and loads the error messages. What I want to do is create PHP code that submits the form to a different url. What I thought was create two forms (the second with hidden fields replicating the first form), each form having a different url in the action"" code. What I cant work out is the PHP IF ELSE code to submit form 2 if Form1 is is validated correctly. This is the PHP code relevant to the form validation. Help? <?php //If form was submitted if ($_POST['submitted']==1) { $errormsg = ""; //Initialize errors if ($_POST[width]){ $title = $_POST[width]; //If title was entered } else{ $errormsg = "Please enter width"; } if ($_POST[drop]){ $textentry = $_POST[drop]; //If comment was entered } else{ if ($errormsg){ //If there is already an error, add next error $errormsg = $errormsg . " & content"; }else{ $errormsg = "Please enter drop"; } } } if ($errormsg){ //If any errors display them echo "<div class=\"box red\">$errormsg</div>"; } //If all fields present if ($title && $textentry){ //Do something echo 'THIS IS WHERE I WANT THE CODE TO SUBMIT FORM 2 or SUBMIT FORM 1 TO A DIFFERENT URL'; } ?> Hello, first time poster.. I've looked the web over for a long time and can't figure this one out. - Below is basic code that successfully checks MySQL for a match and displays result. I was debugging and forced the "height" and "width" to be 24 and 36 to make sure that wasn't the problem. That's good.. - I'd like to give the user ability to select width and height from a form.. and have it do an onchange this.form.submit so the form can be changing as fields are altered (thus the onchange interaction) - In a normal coding environment I've done this numerous times with no "Page cannot be displayed" problems. It would simply change one select-option value at a time til they get down the form and click submit... but in WordPress I'm having trouble making even ONE single onchange work! - I've implemented the plugins they offer which allows you to "copy+paste" your php code directly into their wysiwyg editor. That works with basic tests like my first bullet point above. - I've copied and pasted the wordpress url (including the little ?page_id=123) into the form "action" url... that didn't work... tried forcing it into an <option value=""> tag.. didn't work. I'm just not sure. I've obviously put xx's in place of private info.. Why does this form give me Page Cannot Be Displayed in WordPress every time? It won't do anything no matter how simple.. using onchange.. Code.. $con = mysql_connect("xxxx.xxxxxxx.com","xxxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxx", $con); $myprodwidth=24; $myprodheight=36; $result = mysql_query("SELECT * FROM product_sizes WHERE prodwidth='$myprodwidth' and prodheight='$myprodheight'"); while($row = mysql_fetch_array($result)) { echo $row['prodprice']; } mysql_close($con); <form method="post" action=""> <select name="myheight" onchange="this.form.submit();"> <option selected="selected" value="">select height</option> <option value="xxxxxxxxx.com/wordpress/?page_id=199&height=36">36</option> <option value="xxxxxxxxx.com/wordpress/?page_id=199&height=36">48</option> </select> Hi- the code below lets me upload a CSV file to my database if I have 1 field in my database and 1 column in my CSV. I need to add to my db "player_id" from the CVS file and "event_name" and "event_type" from the form... any ideas??? here's the code: Code: [Select] <?php $hoststring =""; $database = ""; $username = ""; $password = ""; $makeconnection = mysql_pconnect($hoststring, $username, $password); ?> <?php ob_start(); mysql_select_db($database, $makeconnection); $sql_get_players=" SELECT * FROM tabel ORDER BY player_id ASC"; // $get_players = mysql_query($sql_get_players, $makeconnection) or die(mysql_error()); $row_get_players = mysql_fetch_assoc($get_players); // $message = null; $allowed_extensions = array('csv'); $upload_path = '.'; //same directory if (!empty($_FILES['file'])) { if ($_FILES['file']['error'] == 0) { // check extension $file = explode(".", $_FILES['file']['name']); $extension = array_pop($file); if (in_array($extension, $allowed_extensions)) { if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) { if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) { $keys = array(); $out = array(); $insert = array(); $line = 1; while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) { foreach($row as $key => $value) { if ($line === 1) { $keys[$key] = $value; } else { $out[$line][$key] = $value; } } $line++; } fclose($handle); if (!empty($keys) && !empty($out)) { $db = new PDO( 'mysql:host=host;dbname=db', 'user', 'pw'); $db->exec("SET CHARACTER SET utf8"); foreach($out as $key => $value) { $sql = "INSERT INTO `table` (`"; $sql .= implode("`player_id`", $keys); $sql .= "`) VALUES ("; $sql .= implode(", ", array_fill(0, count($keys), "?")); $sql .= ")"; $statement = $db->prepare($sql); $statement->execute($value); } $message = '<span>File has been uploaded successfully</span>'; } } } } else { $message = '<span>Only .csv file format is allowed</span>'; } } else { $message = '<span>There was a problem with your file</span>'; } } ob_flush();?> <!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=UTF-8" /> <title>CSV File Upload</title> </head> <body> <form class="form" action="" method="post" enctype="multipart/form-data"> <h3>Select Your File</h3> <p><?php echo $message; ?></p> <input type="file" name="file" id="file" size="30" /> <br/> <label>Event Name:</label><input name="event_name" type="text" value="" /> <br/> <label>Event Type:</label><input name="event_type" type="text" value="" /> <br/> <input type="submit" id="btn" class="button" value="Submit" /> </form> <br/> <h3>Results:</h3> <?php do { ?> <p><?php echo $row_get_players['player_id'];?></p> <?php } while ($row_get_players = mysql_fetch_assoc($get_players)); ?> </body> </html> Hi all, What I am trying to achieve is, I thought quite simple! Basically, a user signs up and chooses a package, form is submitted, details added to the database, email sent to customer, then I want to direct them to a paypal payment screen, this is where I am having issues! Is their any way in php to submit a form without user interaction? Here is my code for the form process page Code: [Select] <?php include('config.php'); require('scripts/class.phpmailer.php'); $package = $_POST['select1']; $name = $_POST['name']; $email = $_POST['email']; $password = md5($_POST['password']); $domain = $_POST['domain']; $a_username = $_POST['a_username']; $a_password = $_POST['a_password']; $query=mysql_query("INSERT INTO orders (package, name, email, password, domain, a_username, a_password) VALUES ('$package', '$name', '$email', '$password', '$domain', '$a_username', '$a_password')"); if (!$query) { echo "fail<br>"; echo mysql_error(); } else { $id = mysql_insert_id(); $query1=mysql_query("INSERT INTO customers (id, name, email, password) values ('$id', '$name', '$email', '$password')"); if (!$query1) { echo "fail<br>"; echo mysql_error(); } if($package=="Reseller Hosting") { //email stuff here - all works - just cutting it to keep the code short if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } ?> <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick-subscriptions"> <input type="hidden" name="business" value="subscription@jollyhosting.com"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="item_name" value="Jolly Hosting Reseller Packages"> <input type="hidden" name="no_shipping" value="1"> <!--1st month --> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="a3" value="3.00"> <input type="hidden" name="p3" value="1"> <input type="hidden" name="t3" value="M"> <input type="hidden" name="src" value="1"> <input type="hidden" name="sra" value="1"> </form>'; <?php } //last } //end ?> Hi all I created a site a while ago now. Using PHP and MySQL. I set it up using a windows server NOT linux. I've come to start to do some SEO work on the site, but Google is finding two different urls. the www version and the non www version. As I'm on a windows server I cant create a htaccess file and add some script to re-direct. For some reason I dont have IIS installed. I believe you can create a re-direct in PHP. I have done this for the index.php file. It works fine when you go to the homepage but when you click on another link to a different part of the site in Google results, it then redirects me to the homepage. This is obviously messing up my rankings. The code i used for the re-driect was as follows: <?php if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.'.$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']); } ?> My site works with includes to the index.php file. I thought this would work but obviously not. Anyone any ideas. Adi I have the following 1. php page that stores sessions. press submit and... 2. ...goes to perl/cgi page to interface with an api only available in perl, then auto redirects via HTTP headers to... 3. ... a php page where depending on the result a field is added to database then emailed, etc... then... 4.... redirects to main page so really all the user sees is page 1 -> hits submit -> ends up on page 4 via redirects Is there a problem with using redirects? Could the user intervene and prevent these redirects? Thanks Hi All, Am having a "can't see the wood for the trees" moment I have a php redirect file which contains my URLs, like so: return [ '/shop/' => '/products/', ]; I then have a functions file which contains: function 301_redirects(){ $redirects = include __DIR__ . '/redirects.php'; if (isset($redirects[$_SERVER['REQUEST_URI']])) { header("HTTP/1.1 301 Moved Permanently"); header('location: ' . $redirects[$_SERVER['REQUEST_URI']]); exit; } if (isset($redirects[rtrim($_SERVER['REQUEST_URI'], '/')])) { header("HTTP/1.1 301 Moved Permanently"); header('location: ' . $redirects[rtrim($_SERVER['REQUEST_URI'], '/')]); exit; } } If the URL is https://www.mydomain.com/shop then the redirect won't work If the URL is https://www.mydomain.com/shop/ then the redirect works What am I missing? Thanks I am trying to use curl to login to a website and get the login cookie. After I try to post, however, I get a 302. Does anyone know how to log into a page like this? Maybe there's a tutorial or something. This what firefox sends: http://oron.com/ POST / HTTP/1.1 Host: oron.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Referer: http://oron.com/login.html Cookie: login=username; xfss=; per_page_files=50 Content-Type: application/x-www-form-urlencoded Content-Length: 56 login=username&password=password&op=login&redirect=&rand= HTTP/1.1 302 Moved Server: nginx/0.8.50 Date: Tue, 22 Mar 2011 00:56:34 GMT Content-Type: text/plain; charset=UTF-8 Connection: keep-alive Set-Cookie: login=username; domain=.oron.com; path=/; expires=Mon, 20-Jun-2011 00:56:34 GMT Set-Cookie: xfss=sijw0iyu3evs3f13; domain=.oron.com; path=/; expires=Mon, 20-Jun-2011 00:56:34 GMT Location: http://oron.com/?op=my_account Content-Length: 0 ---------------------------------------------------------- http://oron.com/?op=my_account GET /?op=my_account HTTP/1.1 Host: oron.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Referer: http://oron.com/login.html Cookie: login=username; xfss=sijw0iyu3evs3f13; per_page_files=50 HTTP/1.1 200 OK Server: nginx/0.8.50 Date: Tue, 22 Mar 2011 00:56:35 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Expires: Mon, 21 Mar 2011 00:56:35 GMT Content-Encoding: gzip Hi,
Is it possible to create a PHP file that 301 redirects specific pages if the file is included on those pages?
I am wanting to create a file similar to a htaccess file and then include that file on the pages I want to redirect.
For example
"if page is page1.shtml redirect to page2.shtml"
"if page is page5.shtml redirect to page10.shtml" etc
and on page1.shtml and page5.shtml, I will include the above PHP include file
Hope that makes sense
Thanks!
hello everyone, I have a php form, everything works, I do not carry any header() at the end of the form because I have to stay on the same page and because I feel it wipes out the message you sent the form successfully, but for this though if reloading the page shows me the popup asking me to resend the form. how can i solve? I found a function in js with replacestate but I saw that it doesn't work with mobile Safari. if ( window.history.replaceState ) { window.history.replaceState( null, null, window.location.href ); }
Hi, I have setup a basic enquiry form with a Captcha - once the code has been inserted and is correct I want to action the form (submit) in PHP. <?php session_start(); if($_SERVER['REQUEST_METHOD'] == 'POST'){ $vResult = ''; if(strtolower($_SESSION['security_code']) != strtolower($_POST['security_code'])){ $vResult = 'Invalid code!'; } else{ "/enquiry.php" } } ?> I want to submit the form to /enquiry.php - I tried header(Location.. and realised that just redirects, and doesn't submit the form. Any suggestions or tips would be great. Cheers, Paul Code: [Select] <?php if (isset($_POST['checking'])) { echo $_POST['checking']; } if (isset($_POST['test'])) { echo $_POST['test']; } ?> <form id="testform2" action="testform.php" method="post"> <select name="checking" onchange="this.form.submit()"><option value="5">5</option><option value="6">6</option></select> <input type="text" name"test" id="test"> <a href="#" onclick="this.form.submit()">submit it!</a> </form> It works when I try to submit by selecting a new value from the dropdown box but when I try to click the link it won't display the text field value. That is a test case for a problem I m having in one of my codes. I can't submit php form using IE 8. This works fine on Firefox very well. I tried following code but none of them is working. 1. <button type="submit">Submit</button> 2. <input type="submit" value="Submit" name="submit"> I would appreciate if any one help me in this. Can anybody see as to why, if any, this form takes forever to execute? Code: [Select] <?php if(isset($_POST['submitted'])){ $z = $_POST['zipcode']; $r = $_POST['radius']; $sql = mysql_query("SELECT DISTINCT m.LocAddZip, m.MktName,m.LocAddSt,m.LocAddCity,m.LocAddState,m.x1,m.y1,z1.lat,z2.long FROM mrk m, zip z1, zip z2 WHERE m.LocAddZip = z1.zipcode AND z2.zipcode = $z AND ( 3963 * acos( truncate( sin( z2.lat / 57.2958 ) * sin( z1.lat / 57.2958 ) + cos( z2.lat / 57.2958 ) * cos( z1.lat / 57.2958 ) * cos( z1.long / 57.2958 - z2.long / 57.2958 ) , 8 ) ) ) <= $r ") or die(mysql_error()); while($row = mysql_fetch_array( $sql )) { $store = $row['MktName']."<br />"; $store .= $row['LocAddSt']."<br />"; $store .= $row['LocAddCity'].", ".$row['LocAddState']." ".$row['LocAddZip']; $lat1 = $row['lat']; $lon1 = $row['long']; $lat2 = $row['y1']; $lon2 = $row['x1']; $dis = distance($lat1, $lon1, $lat2, $lon2); echo "<p>".$store."</p>"; echo ceil($dis) . " mile(s) away"; echo "<hr/>"; } } ?> I get a timeout error sometimes, and sometimes I don't. Is it the form, or would it be on the server? Thanks in advance Hi, I want to submit a form using ajax ans jquery with two fields input text and file. Code: [Select] <script> $(document).ready( function() { $('#basicinfofrm').ajaxForm(function() { url: ''+PN+'.php', type:'POST', data:'action=yes&'+frmFeilds, success: function(html) { alert(html); } }).submit(); </script> <form action='' method='post'> <label>name</label> <input type='text' name='txtname' /> <input type='file' name = 'txtfile' /> <input type='submit' value='save' /> </form> but is is not working... can anyone tell me how to? i don't want to refresh my page Thanks Hello!
I am trying to display content that will only be displayed for 30 seconds to each visitor. I want to have a unique ID given to each user that visits my page and also add that assigned ID to the end of the URL. If the page is loaded again after 30 seconds, I want the unique user ID to redirect using header();
So When a suer visits my page, I want the end of the url to have “userid=“ like this:
http://example.com/page.php?userid=0000000001If that user comes back to the same url after 30 seconds, I want to redirect using header(); How do I do this? what is the best way? Edited by supertooper, 01 September 2014 - 10:41 PM. I have a Feedback form that I use to get peoples input. So far it works But I have 2 Problems Here is the Code: Code: [Select] <?php include "includes/config.php"; $date = date('jS \of F h:i A'); $sql="INSERT INTO news (name, email, comments, date) VALUES ('$_POST[Name]', '$_POST[email]', '$_POST[comments]', '$date')"; header( 'Location: thanks.php' ); mysql_close($con) ?> I'm making a VB.net Feedback form and I want to imput like this http://mysite.com/feedback.php?name=Mike&email=test@test.net&comments=comment&date=20th of April 12:30 AM But so far from my testing it doesn't register the values and says that there is a Duplicate value for email ". I'd like it to register values somehow and if there is a duplicate email entered then have it update it or write over it. Any help appreciated, Thanks! Is it possible to link action in the form to a function thats included from a functions file? Like so: Code: [Select] <form action="<?php add($text); ?>" > <input type="text" name="text" /> <input type="submit" /> </form> What I want to do is add a block of text to the database. The add function does that. So ideas? I have like 9 of these forms on the same page they all do different things though but all involve modify the database. |