I have this php file that allows a user to enter their favorite music artist. My program puts the entered artist in a variable called "art" and then adds it to an array called "artist". Then it displays the array in a bullet list. But the program as it is only allows one value to be in the array. When the user enters another artist, it overwrites the one that is already in the array. For example, the user writes "bob" as their favorite artist and presses submit. The program displays bob, but when you go to put in another artist, "tess", bob is gone and only tess is diplayed. How do i fix this?
Here is the code: (Don't mind the commented out echo, it was a test to see if the entered value was being assigned to the variable.)
<h1> My Favorite Artist </h1>
<form method='POST'>
<h3>Please enter your favorite artist</h3>
<input type="text" name="artist">
<input type="submit" value="Submit Artist">
</form>
<?php
$art = $_POST['artist'];
//echo "<h3> This $art </h3>";
$artist = array();
array_push($artist, $art);
foreach ($artist as $a)
{
echo "<li>$a</li>";
}
?>
You can try by creating a session of array and then print it like this.
<?php
session_start();
?>
<h1> My Favorite Artist </h1>
<form method='POST'>
<h3>Please enter your favorite artist</h3>
<input type="text" name="artist">
<input type="submit" value="Submit Artist">
</form>
<?php
if(isset($_POST['artist']))
{
$art = $_POST['artist'];
//echo "<h3> This $art </h3>";
if(empty($_SESSION['artist']))
{
$_SESSION['artist'] = array();
}
array_push($_SESSION['artist'], $art);
$arry=$_SESSION['artist'];
if(!empty($arry))
{
foreach ($arry as $a)
{
echo "<li>$a</li>";
}
}
}
Related
<form action="" method="post">
<textarea id="w3review" name="my_occu" rows="4" cols="50">
</textarea>
<input name="submit" type="submit" />
</form>
<?php
if (isset($_POST['submit'])) // Check for Button click or not
{
include('simple_html_dom.php');
$occ=$_POST['my_occu']; // Get textbox value
$occ2=explode("\n",$occ); // store occupation value in array by splitting it by new line
foreach ($occ2 as $value) {
echo $value.'<br>';
$dom = file_get_html($value);
$list=$dom->find('span[class="snapshot-data"]',0);
echo $list.'<br>';
}
}
?>
The URL I am trying to fetch data from is : https://joboutlook.gov.au/occupations/aboriginal-and-torres-strait-islander-education-workers?occupationCode=422111
https://joboutlook.gov.au/occupations/accommodation-and-hospitality-managers-not-covered-elsewhere?occupationCode=141999
I am trying to fetch "All Education Aides" data from the sidebar.
Hoping someone can steer me in the right direction. I have a form with an input field and a button. When the input field is populated and the button clicked the value of the field displays in the variable $capture_numbers.
Id like to be able to add to that value i.e person enters 1, $capture_numbers displays 1, person enters 2, $capture_numbers now displays 1, 2, person enters 3, $capture_numbers now displays 1, 2, 3 and so on. I'm thinking along the lines of storing the previous value and appending to it but cant figure out how it's done. Below is my script.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<fieldset>
<input type="text" name="mynumbers[]">
<button>add another numbers</button>
<input type="submit" value="Submit" title="submit">
</fieldset>
</form>
<?php
if(isset($_POST['mynumbers']) && is_array($_POST["mynumbers"])) {
$capture_numbers = ' ';
foreach($_POST["mynumbers"] as $key => $value) {
$capture_numbers .= $value .", ";
}
echo $capture_numbers;
}
?>
You will need some persistent storage to store previous submissions. Valid options are a database, local storage, cookies, session etc.
In this example, I used session. When the form is submitted, check the session for previous entries and then append the new one to the list. I've added comments in the code.
Edit: You don't need to add the text field as an array. Ie. name="mynumbers[]". That is usually used for when you have multiple inputs with the same name and want to read them as an array.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<fieldset>
<input type="text" name="number">
<button>add another numbers</button>
<input type="submit" value="Submit" title="submit">
</fieldset>
</form>
<?php
// Start the session
session_start();
// Check if form is submitted
if (!empty($_POST)) {
// Create a variable to store all numbers
$allNumbers = [];
// Does the session contain anything
if (isset($_SESSION['previous'])) {
// Grab it from the session as an array and store it
$allNumbers = explode(',', $_SESSION['previous']);
}
// Append the new number to the list
$allNumbers[] = $_POST['number'];
// Convert it to a comma separated string
$capture_numbers = implode(',', $allNumbers);
// Store the entire list in the session
$_SESSION['previous'] = $capture_numbers;
// Output the result
echo $capture_numbers;
}
The problem I'm working on is this: I have a form which ask user to type in a name. Then i search this name in my database, the database will return several addresses. Then i need to use those addresses to generate second radio button form. User will choose one address, and then they get redirected to that address.php
The entire process is like when you shop on amazon, after you log in with your username, it will ask you to select your shipping address and once shipping address is select, i want to redirect user to their address page.
what i current have is following
//error checking----------------------------------
if (empty($_POST['name'])===true) {
echo"filling in your name"
}
else{
//get value from database------------------------------
$query="get address";
$sql_result=mysql_query($query);
if(mysql_num_rows($sql_result)==0){
echo "no result found";
}
else {
//second form--------------------------------------------------------
while ($row = #mysql_fetch_assoc($sql_result)){
echo "<form name=\"select\" method=\"post\">";
echo '<input type="radio" name="name" value="'.$row['name'].'"> '.$row['name'];
echo '<br>';
}
echo "<input type=\"submit\" name=\"choice\" value=\"Submit\" />";
}
}
}
//first form-----------------------------------------------
<form action="" method="post">
<ul>
<li>name: <br> <input type="text" name="name">
</li>
<li><input type="submit" value="Find it">
</li>
</ul>
</form>
I have no idea how to proceed from here. Where shall i put my first form so that only first form is displayed when the page is loaded. Only second form is displayed when first form is submitted. I tried to use $_GET like
if(isset($_GET['success'])&& empty($_GET['success'])){
form2;
redirect(location:address.php);
}
else{
get name and search address;
get address;
redirect(location:samepage?success)
form1;}
But this didn't work since address is generated in else statement, i can not access it from if statement. Thanks for the help.
You have to redirect the first form to a new page then look at the $_POST variable.
You can do it this way :
firstpage
<form action="secondpage.php" method="post">
<ul>
<li>name: <br> <input type="text" name="name">
</li>
<li><input type="submit" value="Find it">
</li>
</ul>
</form>
secondpage.php
if(!isset($_POST['name']))
{
redirect(location:firstpage.php);
}
else
{
// MySql request to check username
$resultArray // you result as an array
echo '<select>';
foreach ($resultArray as $value){
echo '<option value="'+$value+'">'+$value+'</option>
}
echo '</select>';
}
I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)
Code:
<?php
echo "<h1>Testing your Trivia</h1>";
$ages['1943'] = "Casablanca";
$ages['1956'] = "Around The World in 80 Days";
$ages['1970'] = "Patton";
$ages['1977'] = "Annie Hall";
$ages['1981'] = "Chariots of Fire";
$ages['1990'] = "Dances With Wolves";
$ages['2005'] = "Crash";
$ages['2006'] = "The Departed";
echo "Give the year below won academy award<br>";
echo "<Strong>Movie: </strong> <input type='text' name='' id='' readonly='readonly' /><br>";
echo "<Strong>Year it Won the Oscar: </Strong> <form method='get'><input type='text' name='year' /></form><input type='submit' /> ";
echo '<pre>';
foreach( $ages as $key => $value){
print_r("Year: $key, Title: $value <br />");
}
echo '</pre>';
if(isset($_GET['year']))
{
if(array_key_exists($_GET['year'], $ages))
{
echo "<h2>" . $ages[$_GET['year']] . "</h2>";
}
else
{
echo 'Cannot find data';
}
}
?>
Basically trying to have it setup where I can get the movie input to choose a random title and display it in the input field for "movie", then a user has to guess the year it was made. If their too high, it shows a page saying that, if too low it shows an error as well.
I feel like I need to add in another If/Else for if high or too low. Anyone?
Thanks!!
Is there a reason your array keys are strings? It seems like it would make sense in this case for them to be integers.
<?php
$ages['1977'] = "Annie Hall";
$ages['1956'] = "Around The World in 80 Days";
$ages['1990'] = "Dances With Wolves";
$ages['2006'] = "The Departed";
$ages['2005'] = "Crash";
$ages['1943'] = "Casablanca";
$ages['1981'] = "Chariots of Fire";
$ages['1970'] = "Patton";
if(isset($_GET['year']))
{
if(array_key_exists($_GET['year'], $ages))
{
echo $ages[$_GET['year']];
}
else
{
echo 'Cannot find data';
}
}
?>
<form method="GET">
<input type="text" name="year" value="1984" />
<input type="submit" />
</form>
If the user is submitting, say, a value from a dropdown menu/list of radio buttons, you could check to see if the $ages array has that year set, and if not, display a default message:
$year = $_GET['year'];
echo isset($ages[$year]) ? $ages[$year] : 'DOES NOT COMPUTE';
Expanded Edit
MVC men will likely put out a hit on me for saying this (if they haven't already), but I like to keep this kind of thing self-contained. That means a page (say, index.php) that looks generally like this:
<?php
// All of your PHP goes here.
?>
<!DOCTYPE html>
<!-- All of your HTML goes here. -->
I'll start with HTML. You'll want a form, like so:
<form action="" method="get">
<div>
<label>Movie Year: <input type="text" name="year" />
<input type="submit" value="Look Up Movie" />
</div>
</form>
Note that the form's action is left blank. That means that the form will be submitted to the current page. That's where the PHP at the top of your document comes into play.
You'll first want to initialize your array of years and titles:
$ages = array(
'1977' => 'Annie Hall',
// ...
'1970' => 'Patton'
);
Then you check to see if the user submitted the form:
if (isset($_GET['year'])) {
$year = $_GET['year'];
$message = isset($ages[$year]) ? 'The film for that year is called '.$ages[$year].'.' : 'There is no film for that year.';
}
This sets the variable $message to some text that you want to display to the user.
Now we make one last jump back to the HTML part of your document, just above the form:
<?php
if (!empty($message)) {
echo '<p>', $message, '</p><p>Want to go again?</p>';
}
?>
And there you have it, a searchable array of movie titles, organized by year.