I want to add new data to an array dynamically using HTML and PHP
Here is my code:
<form action="" method="">
<input type="text" name="name">
<input type="submit" name="send">
</form>
<?php
if(isset($_POST['send']))
{
$name = $_POST['name'];
for($=1;$i<2;$++)
{
$name = array($name);
$names = array_push($names,$name);
}
print_r($names);
}
?>`
Does anyone have a better way?
I prefer to create indexes after its declaration:
$test = array();
$test['a'] = 'value';
$test['b'] = 'value';
$test['c'] = 'value';
Related
I have a simple input field that takes a string "First Name Last Name" and when the user hits the submit button "Add Name", it should display what was just entered by the user in a textarea that is right below it, but also keep the previous entries until a reset button has been pressed.
I am trying to construct a loop of sorts to allow the user to keep entering names until the "Clear Names" button is pressed, but the request never finishes and times out. I feel like something like this is the solution, so is there something I am overlooking, or am I way off track?
function addName:
<?php
class AddNames {
function addName() {
$nameField = $_POST['nameField'];
if (isset($_POST['addName'])) {
do {
$arr = explode(" ", $nameField);
array_push($arr);
sort($arr);
}
while (!isset($_POST['clearNames']));
}
return "$nameField\n";
}
}
?>
code above HTML header in index.php:
<?php
$output = "";
if(count($_POST) > 0) {
require_once 'AddNames.php';
$addName = new AddNames();
$output = $addName->addName();
}
?>
HTML body:
<body>
<div class="container">
<form method="post" action="#">
<h1>Add Names</h1>
<input class="btn btn-primary" type="submit" name="addName" value="Add Name">
<input class="btn btn-primary" type="submit" name="clearNames" value="Clear Names">
<div class="mb-3">
<label for="nameField" class="form-label">Enter Name</label>
<input type="text" class="form-control" id="nameField" name="nameField" placeholder="First & Last Name">
</div>
<label for="listOfNames">List of Names</label>
<div class="form-floating">
<textarea class="form-control" id="listOfNames" name="listOfNames" style="height: 500px">
<?php echo $output; ?>
</textarea>
</div>
</form>
</div>
</body>
Consider the following changes to your Class function.
function addName($name) {
if (len($name) > 0) {
$arr = explode(" ", $name);
array_push($arr);
sort($arr);
$name = implode(" ", $arr);
}
return "$name\n";
}
It appears you are expecting a String and not an Array of Strings. This is why you chould not need a do, while loop.
In your code, it's not clear what you do with $arr, yet I suspect you mean to sort it and then combine it back into a String.
I ended up solving my problem. I decided to rename my variables to make it more clear as to what I was doing, and that really helped. This is my solution to the AddNames class
<?php
class AddNames {
function addName() {
$nameField = $_POST['nameField'];
if (isset($_POST['addName'])) {
$nameFieldArray = explode(" ", $nameField);
$firstName = $nameFieldArray[0];
$lastName = $nameFieldArray[1];
$name = "$lastName, $firstName";
$textArea = $_POST['listOfNames'];
$masterArrayOfNames = explode("\n", $textArea);
array_push($masterArrayOfNames, $name);
sort($masterArrayOfNames);
$listOfNames = implode("\n", $masterArrayOfNames);
}
if (isset($_POST['clearNames'])) {
$nameField = "";
return $textArea = "";
}
return "$listOfNames";
}
}
I created a simple form, to create a post, that has three inputs:
One for the title
Description
Image
So, when I submit my form (using post) I call a php file, that "echoes" the value from each input.
It works just fine, but when I try to call the php function $_FILES['my_input_name']['tmp_name'], on my file input, I get an error saying:
Undefined index: my_input_name
My form looks like this (shorter version):
<form action="processForm.php" method="post">
<input type="text" name="title" class="input" required>
<textarea id="description" name="description"required></textarea>
<input type="file" name="fileMedia">
</form>
My php file looks like this
$method = $_SERVER[ 'REQUEST_METHOD' ];
if ( $method=='POST') {
$_args = $_POST;
$_INPUT_METHOD = INPUT_POST;
}
elseif ( $method=='GET' ) {
$_args = $_GET;
$_INPUT_METHOD = INPUT_GET;
}
else {
exit(-1);
}
$title = $_args['title'];
$description = $_args['description'];
$mediaName = $_args['fileMedia'];
$mediatmpPath = $_FILES["fileMedia"]["tmp_name"];
echo $title."<br>";
echo $description."<br>";
echo $mediaName."<br>";
echo $mediatmpPath ."<br>";
I have no idea of what I'm doing wrong, so any helped would be really apreciated!
P.s: My form's is really reduced. In the original one I have row, cols, divs, etc, and some other inputs, which I did not find relevant for this question
You just need to add multipart = "form/data" in form tag
You need to add this below line in <form> tag
<form action="processForm.php" method="post" enctype='multipart/form-data'>
<input type="text" name="title" class="input" required>
<textarea id="description" name="description"required></textarea>
<input type="file" name="fileMedia">
<input type="submit" name="save" value="save">
</form>
And below post data code:
<?php $method = $_SERVER[ 'REQUEST_METHOD' ];
if ( $method=='POST') {
$_args = $_POST;
$_INPUT_METHOD = INPUT_POST;
}
elseif ( $method=='GET' ) {
$_args = $_GET;
$_INPUT_METHOD = INPUT_GET;
}
else {
exit(-1);
}
$title = $_args['title'];
$description = $_args['description'];
$mediaName = $_FILES["fileMedia"]["name"];
$mediatmpPath = $_FILES["fileMedia"]["tmp_name"];
echo $title."<br>";
echo $description."<br>";
echo $mediaName."<br>";
echo $mediatmpPath ."<br>";
?>
I think this help you.
I have PHP and HTML files on my IIS site and am trying to get the output of a form shown on the same page. The issue I am running into is when I load the HTML page I am seeing the array information show as plain text on that page. I have form action = "" defined. Alternatively, when I have form action = "file.php" defined, I get the desired results, but on a new page. I took a look at the the link here but didn't seem to provide what I am looking for. I tried adding the tags on each line, which helped a bit but am still seeing the array as plain text. Here is what I have:
<form action="" method = "POST">
MAC Address of phone: <input type="text" name="phonemac"><br><br>
<input type="submit" value="Check Phone Status">
</form>
<?php
$host = "server.com";
$username = "*****";
$password = "*****";
$context =
stream_context_create(array('ssl'=>array('allow_self_signed'=>true)));
$client = new SoapClient("C:\inetpub\wwwroot\PhoneSetup\AXLAPI.wsdl",
array('trace'=>true,
'exceptions'=>true,
'location'=>"https://".$host.":8443/axl",
'login'=>$username,
'password'=>$password,
'stream_context'=>$context
));
$response = $client->getPhone(array("name"=>"$_POST[phonemac]"));
$array = json_decode(json_encode($response), true);
echo $array['return']['phone']['description'];echo '<br><br>';
echo $array['return']['phone']['name']; echo;
?>
</body>
</html>
This
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
I will write a example with your code
$array = json_decode(json_encode($response), true);
echo $array[0]['phone']
echo $array[0]['description'];
echo '</br></br>';
echo $array[0]['phone'];
echo $array[0]['name'];
your code would be like
<form action="" method = "POST">
MAC Address of phone: <input type="text" name="phonemac"><br><br>
<input type="submit" value="Check Phone Status">
</form>
<?php
$host = "server.com";
$username = "*****";
$password = "*****";
$context =
stream_context_create(array('ssl'=>array('allow_self_signed'=>true)));
$client = new SoapClient("C:\inetpub\wwwroot\PhoneSetup\AXLAPI.wsdl",
array('trace'=>true,
'exceptions'=>true,
'location'=>"https://".$host.":8443/axl",
'login'=>$username,
'password'=>$password,
'stream_context'=>$context
));
$response = $client->getPhone(array("name"=>"$_POST[phonemac]"));
$array = json_decode(json_encode($response), true);
echo $array[0]['phone']
echo $array[0]['description'];
echo '</br></br>';
echo $array[0]['phone'];
echo $array[0]['name'];
?>
<body>
</html>
I have a html form which collects, name, place, email on submission it posts to csv file. But the php file creates a file without any data. Can any body help me in correcting the code. The full code is given below.
HTML form CODE:
<form method="post" name="excisedata" action="csv.php">
Enter Full Name: <br>
<input type="text" name="name"><br>
Enter Place /Location : <br>
<input type="text" name="place"><br>
Enter Valid Email :<br>
<input type="text" name="email"><br>
<input type="submit" name='submit' onclick="show_confirm()" value="SUBMIT">
</form>
PHP CODE
<?php
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a");
foreach ($list as $line) {
fputcsv($file,explode(',',$line)); }
fclose($file);
?>
first, i would add static values to your fputcsv function to make sure you have no file permissions problems.
fputcsv($file,'bird,dog,fish');
once you confirm that is ok, try this:
your form is posting data, your processor is looking for GET data.
change
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
to
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
<?php
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a+");
fputcsv($file,$list);
fclose($file);
?>
I never really used session so it could be some stupid mistake. When I use if(isset($_SESSION) it outputs false, I think it has something to do with the foreach. I get no errors whatsoever. Could anyody spare some time to help me?
<?php
session_start();
if(isset($_POST['register']))
{
require_once('../resources/library/register.class.php');
//require_once('../resources/library/sessions.class.php');
$obj_reg = new register();
$name = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];
$checking = $obj_reg->checking($name, $pass);
//An foreach for converting POST data inside SESSION variables
//isset checks if the array value contain post variables
$posts = array($name, $pass, $email);
foreach ($posts as $p)
{
if(isset($_POST['p'])){
$_SESSION['p'] = $_POST['p'];
}
}
}
?>
<form method="post" action="index.php?page=register.php">
<table>
<tr><td>username:</td><td> <input type="text" name="user"></td></tr>
<tr><td>password:</td><td> <input type="password" name="pass"/></td></tr>
<tr><td>email:</td><td> <input type="text" name="email"/></td></tr>
<?=( !empty( $checking ) ) ? $checking : '' ?>
</table>
<input type="hidden" name="token" value="<?=$token;?>"/>
<input type="submit" name="register" value="register"/>
</form>
<?php
session_start();
if(isset($_SESSION['p']))
{
echo "mama";
}
else
{
echo "why?";
}
?>
You need to call session_start on every page that needs $_SESSION.
I think you also mean to use $_SESSION[$p] = $_POST[$p] instead of the string 'p'.