i need a php code that user click on send button in my form automatically add a different character like a - b - c -d to field input.
for example this is my form html code.
<form action="action.php" method="post">
<input type="text" id="phone" name="phone">
<input type="submit" value="send">
</form>
and in action.php like this
echo $_POST['phone'];
user enter 002255 in phone number field and click send button
print 002255 now .
but i want when user click send button for first time , print a002255
and when click send button for second time , print b002255
and next c002255 and next d002255
then make a loop and when click for fifth time , print a002255 again.
What will happened after reaching letter Z? In example below it will reset. I think you need to create an array and index to it. So when user click button index goes +1. Index will be on session because it has to store number. Am beginner too and trying to figure it out. Example:
session_start();
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
if(isset($_POST["submit"])) {
if($_SESSION["Number"] == 0) {
print_r($alphabet[0] . "002255");
$_SESSION["Number"] += 1;
} else {
print_r($alphabet[$_SESSION["Number"]] . "002255");
$_SESSION["Number"] += 1;
if($_SESSION["Number"] > 26) {
$_SESSION["Number"] = 0;
}
}
}
Related
I am working with multiple forms on a page, where pressing a button will edit that specific post (using the hidden input variable).
I am dealing with two issues here:
When I do press edit, it will grab the last hash from the list (I am using a foreach loop to iterate through the list).
When I do press edit and redirect to the next page, the $_POST variable is deemed null.
Page 1:
foreach ($result as $item) {
echo '
<form method="post" action="editPost">
<input type="hidden" id="messageID" value="' . $messageID . '">
# Print $item iterations here in the form of a html form
<div>
<input type="submit" value="Edit" name="editPost">
</div>
</form>
';
}
Page 2:
if ($_POST['messageID'] == null) {
echo '<script>alert("Key error")</script>';
} else {
# Do things if $_POST['messageID'] is not null
}
You should not add Form inside the loop and input or form with same id or name in loop will confuse html to what should send and may it will send last one,
instead of form in loop just add link of other page with edit record id or hash as in query string
example.com/pagetwo.php?editid=$messageid
and from page 2 you can get that variable useing
$_GET['editid'] or $_REQUEST['editid']
and based on id you need to get data from database and fill in inputs to update it
how to make something like a random lottery. Very simple, in the fact that it shouldn't be long code.
I just want a form that i user can click a radio button which equals 1 and enter their name. When they submit $min = 1(and stays at 1), $max = $max + 1.
Lets say 10 people select the radio button and hit the first submit button. $min = 1 meaning the lowest random number is 1. Then their name is put into an array along with a matching id corresponding to the number in the variable $max(if 5 people submitted, the $max would be 5, and their id would also be 5 as they submitted fifth.
So if 10 people submit , and then click another submit button below a random generated number will be made with a minimum of $min which is 1 and a maximum of $max (the last person to submit radio button).
if the random generated number was 7, then display the 7th person that submitted the radio button(because their id would match with the random number).
I have been learning php for 2 weeks so im not very good now, but my code goes like this...
<form action="POST" name="form">
<input type="text" name="name">
<input type="radio" name="1" value="1">
<input type="submit" name="submit" value="submit">
</form>
<?php
$min = "";
$max = "";
$person = array();
for($_SERVER["REQUEST_METHOD"] == "POST"){
$max = $max + 1;
$person[$max] = $_POST["name"] . ;
$min = 1;
}
?>
<form action="POST" name="random">
<input type="submit" value="submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["name"] == "random"){
$rand=------rand($min,$max);
if(//TODO) {
$person[$]
}
}
?>
can someone explain how to make this work please?
Assuming you store everything to a MySQL database a possible solution to retrieve a lottery winner would be:
$DB_winner = mysql_query("select name from participants_table_name");
while($win = mysql_fetch_array($DB_winner){
$winners[] = $win['name'];
}
// and the winner is $the_winer:
$the_winner = $winners[rand(0, sizeof($winners)-1)];
I was going to post a comment but it's busy with trains at the moment so...
You will need to learn and understanding the following for your task
Save the data using a file (json or csv up to you), or mysql if you want to further the project. See tutorials for any of this
Learn how $_POST, isset, for, foreach etc works and what they're used for
My advice is to ignore the above and start with absolute basics. Go through w3schools maybe. You're trying to build a palace when you're struggling with lego
I am trying to setup a website that converts a Steam user ID into an auth ID. It will ask for the visitor to input their regular Steam ID and then hit a button to convert it to auth ID. Steam provides us with the function for ID conversion from one type to the other.
Steam function for converting IDs:
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
Below is my attempt at setting up a basic HTML page that gets user input and then uses the function to convert that input to something else.
<INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">
<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>
Also, this is what the default Steam user ID looks like:
STEAM_0:1:36716545
Thank you for all the help!
If you can make it into two seperate files, then do so.
foo.html
<form method="POST" action="foo.php">
<input type="text" value="ENTER STEAM:ID" name="idform" />
<input type="submit" />
</form>
foo.php
<?php
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
$id = $_POST['idform'];
convert_steamid_to_accountid($id)
?>
if you don't have an option of making two seperate files, you can add the php code to 'foo.html' file and make the form to submit to the same file. However if you do this, check if the file is getting requested the first time, or it is requested because the form is submitted, BEFORE you call convert_steamid_to_accountid() function.
You can do this by:
if ($_SERVER['REQUEST_METHOD']=='POST'){
// your php code here that should be executed when the form is submitted.
}
So I have some data in MySQL being shown on my PHP page inside a table. I've set it up so that each page only displays 3 results each (temporary until it goes live, then it will be more). Everything works fine and it displays those 3 results on each page just fine.
What I want to do is be able to change the amount of results on each page right from the main PHP page. I can change the amount shown on that one page, but as soon as I go to page 2, it resets to 3 results. How can I remember the number stored in the variable, and display it on every page? I tried using SESSIONS, but I couldn't get it to work. I'm still a beginner btw.
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
}
else{
$limit=3;
}
//A few lines down -->
<form action="<?php $_SERVER['REQUEST_URI']?>" method="post">
Items: <input type="text" name="item">
<input type="submit" name"go" value="Go">
</form>
You could add SESSION use this way:
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
$_SESSION['item_limit'] = $limit;
}
else{
// If we find a limit set in the session use that
if (!empty($_SESSION['item_limit']) {
$limit = $_SESSION['item_limit'];
}
else {
$limit=3;
}
}
If you don't need to remember the value the next time the user visits the page you can simply output the $limit as a value attribute for the "item" input.
Items: <input type="text" name="item" value="<?php echo $limit; ?>">
i want to make a add cricket stats page (witch i have done) but when i fill in the form and press submit i made it say echo "$name stats have been added"; but when i add a new persons stats that dispersers and is replaced by a different the one i just made, how can i make it stay every time i add a new one so i can see who's stats i have added?
Create an array of names stored in $_SESSION and keep adding to it on each post. Then display them all each time.
session_start();
// Initialize the array
if (!isset($_SESSION['names'])) {
$_SESSION['names'] = array();
}
// Add the newest name to the array
$_SESSION['names'][] = $name;
// Display them all in a loop with linebreaks
foreach ($_SESSION['names'] as $cur_name) {
echo "$cur_name stats have been added<br />\n";
}
EDIT:
To reset them, pass ?action=reset in the URL querystring as www.example.com?action=reset
<form action='scriptname.php' method="get">
<input type="hidden" name="action" value="reset" />
<input type="submit" value="Reset list" />
</form>
// Remove the session array on reset.
if (isset($_GET['action']) && $_GET['action'] == "reset")
{
unset($_SESSION['names']);
}
why dont you just insert the new name you wanna add, into your DB ? and make a select when you wanna view some names