I have a task to take input from user and push it in array.
How can I take input from input field submit it, push it in to array and then do this again?
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Input Stuff in this Table :
<input type="text" name="insert">
<input type="submit" name="submit">
</form>
<?php
$insert = ($_POST["insert"]);
array_push($data, $insert);
print_r($data);
?>
you will miss the content of your variable and will re-initialize it on every page load, the solution is to keep your $data variable in the session and use it on every form submission:
<?php
session_start();
$data = isset($_SESSION['data']) ? $_SESSION['data'] : [];
array_push($data, $_POST["insert"]);
$_SESSION['data'] = $data;
?>
Related
I'm a newbie on php. I try this form but, it won't store data from input to database. Page reloaded but insert data still not work.
here's the connection
<?php $connection = new mysqli("localhost","root","","geminpo"); ?>
here's the form
<form method="POST" enctype="multipart/form-data">
<input type="text" name="judul">
<button name="save">simpan</button>
</form>
here's the php
<?php
if (isset($_POST['save'])) {
$connection->query("INSERT INTO artikel(judul)VALUES('$_POST[judul]')");
}
?>
Try this, you were missing the ' when accessing the post array value:
<?php
if (isset($_POST['save'])) {
$connection->query("INSERT INTO artikel (judul) VALUES ('" . $_POST['judul'] . "')");
}
?>
Button name is not submitted with the form then $_POST['save'] is not set.
I suggest to use a hidden input :
<input type="hidden" name="save" />
I want to add my $_POST variable to my array every time I submit a name. With this code it empties the array every time I use the Form. How should I do this if I want to add to the array everytime I submit a name?
<?php
$array = array();
if (isset($_POST['name'])){
$new_name = $_POST['name'];
array_push($array, $new_name);
}
print_r($array);
?>
<form action="index.php" method="POST">
<input type="text" name="name">
</form>
See you need something that will remember what $array was, even after a refresh. So either you would need to save it in a database / cookie.
Here is an example using a session ($_SESSION).
<?php
session_start();
if(!isset($_SESSION['names'])){
$_SESSION['names'] = array();
}
if (isset($_POST['name'])){
$_SESSION['names'][] = $_POST['name'];
}
foreach($_SESSION['names'] as $name){
echo $name . '<br>';
}
?>
<form action="index.php" method="POST">
<input type="text" name="name">
</form>
If you don't understand anything, please do ask.
I want $id to increase by 1 every time the form is submitted. Then it should be appended to the array $users.
Why is this not working?
<?php
$users = array();
$id = 0;
if(isset($_POST["submit"])){
$id = $id + 1;
$users[] = $id;
}
echo "<pre>";
print_r($users);
echo "</pre>";
?>
<form action="random.php">
buy a ticket
<input type="submit" name="submit">
</form>
This is because once the PHP code stops executing the value of $id and $users is gone forever. HTTP and PHP is stateless. Once that page is processed it is gone and it is like it never existed. If you want to persist state you need to use a persistent data store like sessions or database.
<?php
session_start();
if(isset($_POST["submit"])){
if (!isset($_SESSION['users'])) { $_SESSION['users'] = 0 }
$_SESSION['users']++;
}
echo "<pre>";
print_r($_SESSION['users']);
echo "</pre>";
?>
<form action="random.php" method="post">
buy a ticket
<input type="submit" name="submit">
</form>
N.B.: Forms defaults to GET when a method isn't defined, therefore it needs the method="post" since you are working with POST variables.
I want to store the text value submitted by clicking the submit button of a form, in a variable, so that I can use that variable for further querying the DB.
My Code:
<?
if($submit)
{
mysql_connect("localhost:3036","root","root");//database connection
mysql_select_db("sync");
$order = "INSERT INTO country (id,country) VALUES ('44','$submit')";
$result = mysql_query($order);
if($result){
echo("<br>Input data is succeed");
} else{
echo("<br>Input data is fail");
}
}
?>
<html>
<title>form sumit</title>
<body>
<form method="post" action="">
<input type="text" name="id" value="<?=$submit;?>"/>
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>
//In real case, the form has elements with radio button containing values from a DB QUERY,
I wanted to use the selected item from the form to process another DB query in the same page...
Thanks in Advance
Try this -
<?php
$submit = $_POST['id'];
if($submit)
{
//your code is here
echo $submit;
}
?>
<html>
<title>form sumit</title>
<body>
<form method="post" action="">
<input type="text" name="id" value="<?php echo $submit; ?>"/>
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>
Submitted form data automatically gets allocated to a variable ($_POST, in your case). If you want longer-term storage, consider using the $_SESSION variable, otherwise the submitted data is discarded upon script termination.
Please clarify your question, as I'm not quite sure what you are trying to achieve here.
In a normal workflow, you would first check if your form has already been processed (see if $_POST has any data worth processing), then query the database for whatever data you need for your form, then render the actual form.
As promised, here's a hands-on sample:
<?php
if ($_POST['ajax']) {
// This is a very trivial way of detecting ajax, but we don't need anything more complex here.
$data = workYourSQLMagicHere(); //data should be filled with the new select's html code
print_r(json_encode($data));
die(); // Ajax done, stop here.
}
/* Your current form generation magic here. */
?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// This should probably go into a separate JS file.
$('#select1').change( function() {
var url = ''; //Here we're accessing the page which originates the script. If you have a separate script, use that url here. Local only, single-origin policy does not allow cross-domain calls.
var opts = { ajax: true };
$.post(url, opts, function(data) {
$('#select2').replaceWith( $.parseJSON(data) ); //Replace the second select box with return results
});
});
</script>
<select id="select1"><?=$stuff;?></select>
<select id="select2"><?=$more_stuff;?></select>
I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)