PHP - Generating A Random Password Without Refreshing The Page.
Here is my code
if(isset($_POST['pass_btn'])) { $numchars = 8; $chars = explode(',','2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7,8,9'); $password=''; for($i=0; $i<$numchars;$i++) { $password.=$chars[rand(0,count($chars)-1)]; } } This is set up so when a person pressing the pass_btn, a password is provided. This works just fine. However, it refreshes the page and clears all other data that user may have entered. Suggestions? Similar TutorialsMaybe it's a Regex question, but I'm wondering how one would go about generating alphanumeric passwords that do not contain either the letters 'oh' or 'el', or the numbers 1 (one) or 0 (zero)? In other words, the p/w can contain any letters of the alphabet - apart from 'oh' and 'el' - and any digits from 2 to 9 inclusive. The p/w length is not critical - let's say 8 characters. Thanks in advance for your help. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=333172.0 Hi I am using php5 and Mysql I want to generate user id like phone format (Ex: xxx-xxx-xxxx). and check that user id exists in database or not. if yes, i have to generate new user id etc. If that user id not in database, i have to insert user id for new user. Means i have generate unique user id for login purpose. Generating user id is working fine. availability of user id also ok for me by using mysql_num_rows() function. But if user id i exists in database, how can i generate new user id. All this will be done in single instance (When user click on submit button from registration form). Here is my script. Code: [Select] <?php function UserID() { $starting_digit=5; $first_part1=rand(0,99); if(strlen($first_part1)==1) { $first_part="0".$first_part1; } else { $first_part=$first_part1; } $second_part1=rand(1,999); if(strlen($second_part1)==1) { $second_part="00".$second_part1; } elseif(strlen($second_part1)==2) { $second_part="0".$second_part1; } else { $second_part=$second_part1; } $third_part1=rand(1,9999); if(strlen($third_part1)==1) { $third_part="000".$third_part1; } elseif(strlen($third_part1)==2) { $third_part="00".$third_part1; } elseif(strlen($third_part1)==3) { $third_part="0".$third_part1; } else { $third_part=$third_part1; } $userid=$starting_digit.$first_part."-".$second_part."-".$third_part; return $userid; } $random_userid=UserID(); $sql=mysql_query("select * from users where user_id='".$random_userid."'"); if(mysql_num_rows($sql)==0) { $insert=mysql_query("insert into users (user_id) values ('".$random_userid."')"); } ?> If user id not exists in database, that user id inserting in database successfully. If not, empty value is inserting, not generating new user id. Can any one help me out please. Please i'm working on a project and i need to write a code that generates a unique random serial number.How can i go about it? Hi, I have a trouble figuring out how to properly convert a list of product data from XML into CSV format. My source is a XML file containing a list of products with attributes like color, size, material etc. with the following structu Code: [Select] <?xml version="1.0" encoding="utf-8" ?> <store> <products> <product> <name>T-Shirt</name> <price>19.00</price> <attributes> <attribute> <name>Color</name> <options> <option> <name>White</name> <price>0.00</price> </option> <option> <name>Black</name> <price>0.00</price> </option> <option> <name>Blue</name> <price>0.00</price> </option> </options> </attribute> <attribute> <name>Size</name> <options> <option> <name>XS</name> <price>-5.00</price> </option> <option> <name>S</name> <price>-5.00</price> </option> <option> <name>M</name> <price>0.00</price> </option> <option> <name>L</name> <price>0.00</price> </option> <option> <name>XL</name> <price>5.00</price> </option> </options> </attribute> </attributes> </product> <product> <name>Sweatshirt</name> <price>49.00</price> <attributes> <attribute> <name>Color</name> <options> <option> <name>White</name> <price>0.00</price> </option> <option> <name>Black</name> <price>0.00</price> </option> </options> </attribute> <attribute> <name>Size</name> <options> <option> <name>XS</name> <price>-10.00</price> </option> <option> <name>M</name> <price>0.00</price> </option> <option> <name>XL</name> <price>10.00</price> </option> </options> </attribute> <attribute> <name>Material</name> <options> <option> <name>Cotton</name> <price>10.00</price> </option> <option> <name>Polyester</name> <price>0.00</price> </option> </options> </attribute> </attributes> </product> <product> <name>Earrings</name> <price>29.00</price> </product> </products> </store> Each product has a number of elements like name, price etc. but also a random number of attributes (like color, size, material etc.) that also have a random number of options. Each option can affect the price of the product, so ordering a XS sized t-shirt can be cheaper than ordering a XL sized t-shirt. I would like to end up with a CSV representing one attribute combination on each line. In my example that would result in 3 colors x 5 sizes = 15 lines for the T-Shirt , 2 colors x 3 sizes x 2 materials = 12 lines for the Sweatshirt and 1 line for the Earrings without any attributes: Code: [Select] name,price,color,size,material T-Shirt,14.00,White,XS, T-Shirt,14.00,Black,XS, T-Shirt,14.00,Blue,XS, T-Shirt,14.00,White,S, T-Shirt,14.00,Black,S, T-Shirt,14.00,Blue,S, T-Shirt,19.00,White,M, T-Shirt,19.00,Black,M, T-Shirt,19.00,Blue,M, T-Shirt,19.00,White,L, T-Shirt,19.00,Black,L, T-Shirt,19.00,Blue,L, T-Shirt,24.00,White,XL, T-Shirt,24.00,Black,XL, T-Shirt,24.00,Blue,XL, Sweatshirt,49.00,White,XS,Cotton Sweatshirt,49.00,Black,XS,Cotton Sweatshirt,59.00,White,M,Cotton Sweatshirt,69.00,Black,M,Cotton Sweatshirt,69.00,White,XL,Cotton Sweatshirt,69.00,Black,XL,Cotton Sweatshirt,39.00,White,XS,Polyester Sweatshirt,39.00,Black,XS,Polyester Sweatshirt,49.00,White,M,Polyester Sweatshirt,49.00,Black,M,Polyester Sweatshirt,59.00,White,XL,Polyester Sweatshirt,59.00,Black,XL,Polyester Earrings,29.00,,, I already managed to generate the CSV Output for simple products like the Earrings and products with just one attribute, but am struggling to come up with a way to generate all possible product attribute combinations for products with more than one attribute. My miserable attempts at this so far have produced following code: Code: [Select] <?php mb_internal_encoding("UTF-8"); header('Content-Type: text/html; charset=utf-8'); $source = "example.xml"; $handle = fopen($source, "r"); $fp = fopen('export.csv', 'w'); $xml = simplexml_load_file($source); // Generate list of attributes (for csv header etc.) $header_attributes = array(); foreach ($xml->products->product as $product) { if(isset($product->attributes)) { foreach($product->attributes->attribute as $attribute) { array_push($header_attributes, $attribute->name); } } } $header_attributes = array_unique($header_attributes); $csvheader = array( 'name','price' // these exist for all products, could also include weight, image, description, special price etc... ); $static_csvheadercount = count($csvheader); foreach($header_attributes as $attribute) { array_push($csvheader, $attribute); // add variable number of attribute fields to csv header } fputcsv($fp, $csvheader); foreach ($xml->products->product as $product) { // loop through each product if(isset($product->attributes)) $simple = 0; else $simple = 1; if($simple == 1) { // if product is a simple product with no attributes $output=array(); array_push($output,(string)$product->name); array_push($output,(string)$product->price); for($i = $static_csvheadercount + $attribute_position; $i < count($csvheader); $i++) { array_push($output, ''); } fputcsv($fp, $output); } else { // is a configurable product with attributes $json = json_encode($product->attributes); $attributes = json_decode($json, TRUE); $attributes_number = count($product->attributes->attribute); if($attributes_number > 1) { // if product has more than 1 attributes so we have to generate each attribute combination // // I'm trying to figure out what should happen here // } else { // if product has only one attribute $attributename = (string)$product->attributes->attribute->name; $attribute_position = array_search($attributename, $header_attributes); $options_number = count($product->attributes->options->option); $pos = 1; foreach($attributes['attribute']['options']['option'] as $option) { $output=array(); array_push($output,(string)$product->name); array_push($output,(string)$product->price); for($i = $static_csvheadercount - 1; $i < ($static_csvheadercount + $attribute_position); $i++) { array_push($output, ''); } $output[$static_csvheadercount + $attribute_position] = $option['name']; for($i = $static_csvheadercount + $attribute_position; $i < count($csvheader) - 1 ; $i++) { array_push($output, ''); } fputcsv($fp, $output); $pos++; } $output=array(); array_push($output,(string)$product->name); array_push($output,(string)$product->price); for($i = $static_csvheadercount; $i < count($csvheader); $i++) { array_push($output, ''); } fputcsv($fp, $output); } } } ?> I've been stuck at this problem for hours unable to figure out a solution. Can someone give a few tips or pointer how to achieve the output for products with multiple attributes? I need a way to generate a random number and insert into a database, and I need the database to not contain any duplicates of that number. I basically need to generate a RMA # (Return Merchandise Authorization Number), so the numbers absolutely CAN NOT be duplicate. I have no ideas how to go about this. Should I generate a random number, search the database and see if there's a duplicate, and if there is re-run the script? Dear all, Can somebody help me please? Instead of ‘username’ and 'password', I need random values from CSV file. Can somebody show me how can I do this? My CSV file looks like this: user001,userpass001 user002,userpass002 user003,userpass003 My script looks like this: <?php $t= new post(); $t->username='username'; $t->password='password'; $res = $t->update('This is some text.'); ?> Thank you very much advance. Hi, In the following code I'm using a combo box to select a relay and activate it. The code works but if I refresh the page, the last selected relay is activated again. How can I reset the page to default to value '0' which has no relay control? TIA
<!DOCTYPE html> <html> <head> <title>Relay control</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <h2><center>Status y control de relés</center></h2> <?php $arrleds = array(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $selected_val = $_POST['sel']; if ($selected_val == '1'){ $cmd = exec("sudo ./trigger.py b 0"); } elseif ($selected_val == '2'){ $cmd = exec("sudo ./trigger.py b 1"); } elseif ($selected_val == '3'){ $cmd = exec("sudo ./trigger.py b 2"); } .... else {} } ?> <table class="center"> <tr> <td></td> <td><h1>Activación de relé</h1> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <select name='sel' onchange='submit();'> <option value='0'>Sin activar</option> <!-- no action --> <option value='1'>Secadora 1</option> <option value='2'>Secadora 2</option> <option value='3'>Lavadora S</option> .... </select> </form> </td> <td></td> </tr> <tr> <td></td> <td><a href="index.php">Home</a> <td></td> </tr> </table> </body> </html>
i have a form with two buttons, one is for submitting the form while button is for generating a random a code and placing it in a textfield. this works fine but the problem is anytime i click on the generate button, it refreshes the page thereby validating the form , which i don't want. How do i make it perform the function without refreshing the page.... I'm trying to generate a page with javascript in it with PHP. But the quotations are causing me problems, I need a third set of quotations. Here is the part I am having trouble with: echo"<div class='level'>Edit:</div><div class='box'><input type = 'text' size='50' name='cat' value='$cat'/> <b><input type='button' value='+ Details' onclick='document.getElementById('rev$clicker').style.display='block';'/></b> </div><br>"; When I view the html it comes out onclick='document.getElementById('rev$clicker').style.display='block';' I can't use "" quotations for 'block' and 'rev$clicker' because that would exit from the echo and I can't exit php because I need the $clicker variable. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=347117.0 Hi, I have a "user profile" page and it has a profile image upload form. I have it so that the old profile image is deleted, and the new image is uploaded and the new image is echoed out using the name of the new file from the database. The problem is, when I click submit to add the new image, the old image still stays there and the new image does not show up. Only when I manually click refresh on my browser does the new image show up. That is going to be bad for my users. Please if anyone can help, I'd greatly appreciate it. Below is the code I am using to delete the old image, and code to upload the image as well, when the photo upload form is used. Code: [Select] if(isset($_POST['photoUpload'])) { if(file_exists("images/".$currentUser.".jpg")){ unlink("images/".$currentUser.".jpg"); clearstatcache(); } //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } } //end if here //was else here //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name= $currentUser .'.'.$extension; //$image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname=$image_name; //"images/". // Connects to your Database mysql_connect("host", "user", "pass") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()) ; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], "images/".$newname); // update the photo name in the database $result = mysql_query("UPDATE users SET profilePhoto='$newname' WHERE username='$currentUser'") or die(mysql_error()); if (!$copied) { echo '<h1>Copy unsuccessful!</h1>'; $errors=1; } else{ $dir="images/"; echo "<p>Profile Picture Change Successful!</p>"; echo "<img src='{$dir}{$newname}' alt='{$newname}' height='200' width='200' />"; echo "<p></p>"; } } This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=330523.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=354853.0 Hey guys! I need your help with some issues. Firstly, I have an own website with a database filled with products, and also a template page(template.php) for each product. Problem is that I don't know how to generate each product from the database in the template.php file using their id from the database. How can I do this? Second problem, I got an own checkout and I don't know how to redirect the customer to a "thank you for buying" page after clicking "Finish Order". It sends the customer to a white page where it says: "Thanks for buying!". Third and last thing, do you guys know where can I find a php checkout template to follow? Thanks in advance for your answers! Cheers! Here is my add driver button I'm using paypal PDT, so when the user has made payment he gets directed back to my site, on that page it executes a special sql query. I noticed I can refresh the page to make duplicate sql queries. How can I avoid this?? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=349726.0 <!DOCTYPE html> <html> <head> <script> $(document).ready(function() { if($("#getPIN").click(function(){ <?php $char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890'; $generated_password = substr(str_shuffle($char), 0, 10); ?> var genNumber; $("#txtpin").val() = genNumber; return false; })); }); </script> </head> <body> <input type = "text" name = "txtpin" id = "txtpin" value = ""> <input type = "button" name = "getPIN" id = "getPIN" value = "getPIN"> </body> </html> Hey you guys need your help... Basically what im trying to do is when the user clicks the getPIN button, the value generated using php is assigned to the txtpin textfield without refreshing the page.... thanks alot guys.... Hi, I have a CMS, I am creating a forgotten password page, the page will require a user to enter an email address and the code will find it in the database and send them an email, in my database, i have multiple users accounts assigned to one email address. I want it so that if the user enters an email address and it was more than one accuont assigned to it, to error a message saying please contact your admin, but atm, it is not doing this. Any suggestions? |