This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 2 years ago.
I got a challenge in which I have this code in a PHP file:
<?php
include('secretfile.php');
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if (!empty($a) && !empty($b))
{
if($a===$b)
{
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if($a!==$b)
{
echo $secretcode;
}
}
}
}
}
I have to print secretcode in webpage with OUT changing the PHP file.
How can I do it?
I tried by giving parameters through URL like this:
http://127.0.0.1:8080/?a=1&b=1&a=22&b=33
But it didn't work. The file is taking the last values directly and no matter what, I couldn't go past the 13th line. I went through a lot of answers but I go no solution.
Is it possible to do it? If yes, how?
To pass multiple values for a or b, you need to use the [] notation like below:
http://127.0.0.1:8080/?a[]=1&b[]=1&a[]=22&b[]=33
That being said, the PHP code in your file seems to be poorly written.
To pass an array on html form you can use [] like
<form method="GET">
<input type="text" name="a[]" />
<input type="text" name="b[]" />
<form/>
Related
This question already has answers here:
PHP How to send multiple values within the same name
(2 answers)
I am trying to insert multiple input value with same name
(2 answers)
How to get multiple input values with PHP when name attributes are the same?
(1 answer)
store multiple value with same input name in sql
(1 answer)
PHP Get multiple values from GET with same name
(2 answers)
Closed 4 months ago.
simple php code that works well in terminal:
<?php
$a = array();
for($i=0; $i<3; $i++){
$b = readline('time: ');
$c = readline('money: ');
$d = array('time'=>$b, 'money'=>$c);
array_push($a, $d);
}
print_r($a);
this pushes the values of multiple entries into an array, creating an array of arrays. however, readline() does not work in the browser. i know i can use javascript easily enough but am trying to replicate this simple action using only php and html. and i really like the way readline() works. i have tried variations on the following but am left scratching my head:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time'>
<input name = 'money'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
was hoping print_r($_POST['input name']) would return an array, but instead only gives the last input entry. is there a straight forward way to do this with php, or do i HAVE to use a client-side script like javascript?
Just add [] to input names:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time[]'>
<input name = 'money[]'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
This question already has answers here:
Fatal error: [] operator not supported for strings
(9 answers)
Closed 6 years ago.
I have the following code where the user selects the value from a drop down. Everytime they submit, the ID should be pushed in the array.
<?php
$_SESSION["test"] = array();
$link = mysql_connect("localhost","root","");
mysql_select_db("dbsikkim",$link);
?>
<form name="form1" method="post" action="">
<?php
$sql=mysql_query("SELECT ID,DOC_DESC FROM gstn_document_type_master",$link);
echo "<select name='doc_type_code2'>";
if(mysql_num_rows($sql)){
while($row=mysql_fetch_array($sql)){
echo "<option value=".$row['ID'].">".$row['DOC_DESC']."</option>";
}
}
echo "</select>";
?>
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$_SESSION["test"][] = $_POST['doc_type_code2']; //ERROR IN THIS LINE
for ($i=0; $i < sizeof($_SESSION["test"]); $i++) {
echo $_SESSION["test"][$i]."<br>";
}
}
?>
Only the current value is pushed into the array and replaces the previous one. In simple words, it is working as a variable and not an array.
You most likely need to get your session variable initialized.
$_SESSION["test"] is not an array by default. You could try to do:
// If the session variable is not yet an array, initialize it
if (!is_array($_SESSION["test"])) {
$_SESSION["test"] = [];
}
$_SESSION["test"][] = $_POST['doc_type_code2'];
The first line assigns an array to your field.
The second one assigns a value to the array.
Please note, there are several things to improve in your code. In example, you could use PDO these days. Also you should not use "global". Also mixing PHP and HTML code like that might lead to software which is hard to maintain. I don't want to sound arrogant, but I highly recommend you the book "PHP The right way".
This question already has answers here:
Generate Array from a comma-separated list - PHP [duplicate]
(3 answers)
Closed 8 years ago.
I have a form that asks for comma separated phone numbers, for example, if user enters following in "Phone" field:
9999999999,8888800000,7777788888
Then want to store them as an array just like:
$contacts = array ("9999999999","8888800000","7777788888");
How can I do that?
I tried:
$contacts = array();
if (is_array(#$_POST['phone']))
{
foreach($_POST['phone'] as $one)
{
$contacts[] = basename($one);
}
}
$myArray = explode(',', '9999999999,8888800000,7777788888');
You will have to fetch the form input with the global variable called $_POST, $_POST listens to the name of the html element.
Let's say we got
<form method="POST">
<input type="text" name="phoneNumber1" />
<input type="text" name="phoneNumber2" />
<input type="text" name="phoneNumber3" />
</form>
Then we are able to fetch the data in it like this.
$contacts = array($_POST['phoneNumber1'], $_POST['phoneNumber2'], $_POST['phoneNumber3']);
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 months ago.
If I have a form:
<form action="/page.php" method="POST">
<input name="length[<?=$ID;?>]['00:12:00']">
</form>
So, on the BACK end, I clearly have an array, but I need to reference the ID number above, i.e.
foreach($_POST['length'] AS $p) {
echo($p['ID']);
echo($p['ID']['length_number']);
}
Is there a way to structure the front end form input data differently so it is easier to comb through on the back end?
You could reference it by using a hidden input field to grab the ID.
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Try using something like this:
foreach($_POST['length'] AS $id=>$values) {
echo $id;
print_r($values);
}
The the above $values will be an array such as array('00:12:00'=>'{value of the input field}')
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 9 years ago.
In a html form I have a variable $var = "some value";.
I want to call this variable after the form posted. The form is posted on the same page.
I want to call here
if (isset($_POST['save_exit']))
{
echo $var;
}
But the variable is not printing. Where I have to use the code GLOBAL ??
EDIT: After your comments, I understand that you want to pass variable through your form.
You can do this using hidden field:
<input type='hidden' name='var' value='<?php echo "$var";?>'/>
In PHP action File:
<?php
if(isset($_POST['var'])) $var=$_POST['var'];
?>
Or using sessions:
In your first page:
$_SESSION['var']=$var;
start_session(); should be placed at the beginning of your php page.
In PHP action File:
if(isset($_SESSION['var'])) $var=$_SESSION['var'];
First Answer:
You can also use $GLOBALS :
if (isset($_POST['save_exit']))
{
echo $GLOBALS['var'];
}
Check this documentation for more informations.
Try that
First place
global $var;
$var = 'value';
Second place
global $var;
if (isset($_POST['save_exit']))
{
echo $var;
}
Or if you want to be more explicit you can use the globals array:
$GLOBALS['var'] = 'test';
// after that
echo $GLOBALS['var'];
And here is third options which has nothing to do with PHP global that is due to the lack of clarity and information in the question. So if you have form in HTML and you want to pass "variable"/value to another PHP script you have to do the following:
HTML form
<form action="script.php" method="post">
<input type="text" value="<?php echo $var?>" name="var" />
<input type="submit" value="Send" />
</form>
PHP script ("script.php")
<?php
$var = $_POST['var'];
echo $var;
?>