Passing input value into name of input - php

I'm trying to get the value from two input fields and pass them as the name in a form.
In my code, I am hardcoding the value of the price range in for testing purposes.
echo 'PRICE RANGE:';
echo 'Low: <input type="text" name="t[pr_100000]" value="" maxlength="25" /> High: <input type="text" name="t[ph_10000000]" value="" maxlength="25" />';
echo 'STATUS:';
$termsStatus = get_terms( 'Status', array(
'hide_empty' => 0
) );
echo '<ul>';
foreach ($termsStatus as $term_st) {
$termsStatus = $term_st->name . 'PropertyFilter';
echo '<li><label><input type="checkbox" name="t[st_' . $term_st->name . ']" value="st_">' .$term_st->name. '</label></li>';
}
echo '</ul>';
Here is the code on another page that the search parameters are sent to:
// GETS THE VARIABLE FROM THE SEARCH WIDGET
$array_terms_test = array_keys( $_GET['t'] );
Any suggetions? Thanks in advance!

Judging by your comment, it seems like you are having trouble passing the input fields to the second snippit of code (via a submit button or javascript). You need to surround the input fields with a form tag and submit the form. If you want to use $_GET simply assign action='get' on the form. If for some reason this doesn't work for you, you can use javascript to pull the values from the fields and create a url var which you use to redirect (window.location = url).
Sorry if I didn't understand your question.

Related

Getting the null vaue in the textfield (fetching with the help of json decoding)

I am implementing a functionality in which users can update their profile. When user clicks on "update profile", a form and text fields are displayed with all their values returned to the user.
But I am unable to get the "Interest field", in which I encode the values to JSON and store it in array string. Now while retrieving it in the text field I use json_decode, which helps me to write the values in text field. But once user clicks on "save" and then clicks on "update", all values come null.
For more than one interest, text fields are dynamically added by the user. Here is the code that I wrote:
<div class="form-group" id="area_interest">
<h4>Area of Interest</h4>
<div style="display:inline-block">
<? $interest = json_decode($model->profile->interest,true) ? : [];
foreach($interest as $area_int) { ?>
<label>Enter Interest:</label>
<input name="$area_int" type="text" value="<?= $area_int ?>"/>
<? }
<div style="display:inline-block">
<input type="button" value="Add More Interest" onClick="addTextArea();">
</div>
</div>
</div>
Function for adding more textbox dynamically:
function addTextArea(){
var div = document.getElementById('interests');
div.innerHTML += "<label>Enter Interest</label>"
div.innerHTML += '<?php echo $model->form->textBoxFor("interests[]"); ?> ';
div.innerHTML += "\n<br />";
}
The code in the function is:
$interests = isset($_POST['interests']) ? $_POST['interests']: [];
$_POST['interests'] = json_encode($interests);
What should I write in name ?
I don't know where I missed some important point. Help would be greatly appreciated. TIA.
<input name="$area_int" type="text" value="<?= $area_int ?>"/>
I think the name is the issue, if in the function u are accessing the $_POST['$area_int'] value it should work, but if u are accesing $_POST['area_int'] , that value will not be found.
Hope my answer helps u.
-----All this after the comments-----
I see the method $model->form->textBoxFor("interests[]") im gessing that method allows u you to create a new input, so i guess it echo a javascript function that allows that, the name if the input is interests[]
In the function on your controller you sould get it as $_POST['interests'], that will contain all the interests u added in the form, so how to get those values?
When the name of an input is nameOfTheInput[] php accept those inputs as an array, so when u are accessing $_POST['interests'], u are getting
array (
0 => 'this is my first NEW interest',
1 => 'this is my second NEW inetrest',
....
n => 'this is my n NEW interest'
);
After that u should process the values.
Hope my answer helps u.
But try to be especifics on how your code is working.

PHP Form variable and data to & from text file?

I have a form with quite a lot of input fields and checkboxes.
When I submit the form I want to write the form field name and value to a text file.
<input type="text" name="mailHost" value=""/>
<input type="text" name="mailUser" value=""/>
<input type="text" name="mailPass" value=""/>
So with this as an example it would be written to a file as :
mailHost = VALUE
mailUser = VALUE
mailPass = VALUE
For a few form fields it fine doing each on by hand, but is there a function or way to do this for numerous fields ?
And then the same for reading it back ?
Again using the same example above I'd end up with the following when read back :
$mailHost = Value, $mailUser = value etc where the variable name is dynamically created and the value assigned ?
Thanks
You can iterate through your $_POST array:
foreach ($_POST as $key => $value) {
file_put_contents('file.txt', $key . " = " . $value . "\n", FILE_APPEND);
}
NOTE
Unchecked checkboxes will not in the $_POST array.

get checkbox value in to a variable

This is my checkbox
<input name="interests2" type="checkbox" value="double-deep-racks" />
This is how I am trying to get that value in to a variable
$int = $_POST['interests2'];
Can you please tell me what i am doing wrong. I cant get the values I just get blank.
Try
$int = $_POST['interests2'];
If you are trying to set multiple checkboxes you can do something like,
// Your html
<input type="checkbox" name="interests[]" value="This is i">
<input type="checkbox" name="interests[]" value="Another i value">
// php
$email = "Further Information In: \n";
foreach($_POST['interests'] as $i)
$email .= $i . "\n";
The name of your checkbox is interests2. You must get the value by that name like this:
$int = $_POST['interests2'];
The name element must match what you are looking for. In your input field the name is interests2 but you are looking for interests (missing "2").
Also, you may possibly need to look in $_GET instead of $_POST, depending on the form or the AJAX method you are using (you didn't post that portion of your code).

How to display a value being passed?

How can I display this value in the loop ?
echo '<input type="hidden" name="eg_payamt_'.$i.'" id="amount_post_'.$i.'" value="">';
this code is being passed into next code by POST
On the next page,
Could it be like this ?
foreach($_POST["eg_payamt_"] as $key => $payamt)
{
echo "eg_payamt_$key => $payamt\n <br>";
}
I don't see any results,
Do you guys have any ideas ??
You have to change the name of the input element -- all input elements like this will have the same name:
<input type="hidden" name="eg_payamt[]" value="whatever" />
And then when you access $_POST['eg_payamt'] it will already be an array, so your code will work almost as-is (you need to lose a couple of underscores).

What is wrong with this foreach loop?

I am trying to allow my users to update quantities in the database, by providing them some input fields, I know that the sql update query is perfect, because if i DONT use the foreach loop, it will submit and update the last textfield only.
But I need the foreach loop so it will loop through all the textfields and update them all in the database. Can somebody please help me figure out why it's not updating with this foreach loop? Lots of thanks in advance :)
foreach($_POST['items'] as $p=>$q)
{
// working sql code is in here.
}
And the fields are dynamically generated like so:
$ct->data[$key][0]='<input type="text" value="'.$ct->data[$key][0].'" name="product" />';
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[' . $ct->data[$key][1] . ']" />';
$ct->data[$key][2]='<input type="submit" value="Update Item">';
$ct->data[$key][3]='<p name="price">'.$ct->data[$key][3].'</p>';
You should ALWAYS check if a variable exists. That's no more that normal and should have been lession 1.
You can't iterate over $_POST['items'] if $_POST['items'] doesn't exist (obviously). So you have to check if the form has been submitted.
One way is:
if ( isset($_POST['items']) ) {
// foreach ( $_POST['items'] ....
}
I always like to be on the safe side and check for type as well:
if ( isset($_POST['items']) ) {
// form submitted at least
if ( is_array($_POST['items']) ) {
// and it's an array as it should be
foreach ( ....
}
}
POST variables are never set in a GET request, so doing the foreach first time on a page is useless. That's what the check is for: 'is the form submitted and do the necessary post vars exist?'
What values are you seeing?
foreach($_POST['items'] as $p=>$q) {
print "$p: $q\n";
}
The name of the items input field should just be items[].
So:
# Good
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[]" />';
# Bad
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[' . $ct->data[$key][1] . ']" />';

Categories