How do I access dropdown in SPAN within PHP POST form? - php

I have a modification of the dynamic code here embedded within a PHP form, with span elements such as
<span level='0'></span>
<span level='1'></span>
<span level='2'></span>
in the form.
Each span will contain a <select> dropdown that needs to be POSTed
Each span is filled using a JS function exactly like the one in the link, which calls the shown ajax.php file to fill the dropdowns.
These values do not POST with my form, presumably because SPANs may not be processed as part of the POST array. Is there a way I can modify this to work?

I haven't checked the details of the link, but it seems you could use a form element such as instead of <span> to post the data that you need, and just use JS to set the value of this element accordingly.
Edit: An example. I may be misinterpreting your question. What is the data that is important to you - The level at which the user selected? So, if I make a selection from the <span level='1'></span> <select>, you need to know that I chose level 1? In this case, since you're pulling that from the DB through AJAX anyway, I would check it on the server side using PHP to determine the chosen level. Otherwise, you can do something similar to the following.
Note: This will only set the hidden element to the level of the last changed <select>.
<form>
<span level="0">
<select onchange="javascript: $('#input_span_level').val('0');"></select>
</span>
<span level="1">
<select onchange="javascript: $('#input_span_level').val('1');"></select>
</span>
<input id="input_span_level" type="hidden" name="span_level" value="-1" />
</form>

Related

PHP. How to get $_POST value after table disappears from HTML

I am sorry for the title. That was my best shot to explain the situation with the least words.
I am trying to make a php program that has a html part with select and option. I am not using ajax or mysql. Just using a JSON file and xampp for apache.
if you select one of the options,`
if(isset($_POST["choice"]))
this php code will work in the html, and show a series of input boxes where you can type in what ever you want. Each option has an array within a JSON file.
So, I have put it in
$file[$_POST["choice"]]
`
and iterated it with a key => value. and shoved it in the input box. The value of the input box would be initially the value of the JSON file I called. I wanted the user to erase that text and type in their own. There could be several input boxes depending on the choice the user makes.
The name of the input box would be the KEY.
Then if you hit the edit button which is a input type submit, the series of input boxes will disappear.
I wanted to get the return with a
$_POST[KEY]
But, whatever I choose, the $_POST[KEY] will just return me the very first option of the select option html.
IS there a way I can solve this?
I need to get the corresponding array of the selected choice.
My goal is to get the values of the input box and update a JSON file.
<select name = "muscle">
<option value = "chest">Chest</option>
<option value = "back">Back</option>
<option value = "leg">Leg</option>
</select>
<br>
<input type="submit" name="choice" value="choose">
<br><br>
<?php if(isset($_POST["choice"])) : ?>
<h3> Current Workout Program </h6>
<?php
foreach ($program[$_POST["muscle"]] as $key => $val):
?>
<p><?= $key. ":" . $val;?></p>
<input type="text" name="<?=$key?>" value="<?=$val?>">
<?php endforeach;?>
<br><br>
<input type="submit" name="edit" value="edit">
<br>
</form>
<?php endif;?>
The iteration of the Key value above works fine.
But if I do a
if (isset($_POST["edit"])){
print_r($program[$_POST["muscle"]]);
}
After submission, It will give me the array for "chest" only.
As I understood your code, if you submit the mentioned form the $_POST would be as the following box:
//$_POST is like the following
[
"muscle" => "chest",
"choice" => "choose"
]
So in the result page, if(isset($_POST["choice"])) condition check would be always true, And the progress will go right.
I think on the destination page (eg. edit page) you have to add a hidden input as the following to make sure you tell the system which muscle you are editing:
<input type="hidden" name="muscle" value="<?= $_POST['muscle'] ?>">
Please note that the input type is hidden and it's not shown to the person who is working with the form.
Check the solution out and let me know if there are any other issues with the response.

Appending and substracting divs filled with data from one simple form to a form that contains all the divs grouped and ready to POST

So this is the deal:
I have an order page and I use two forms.
The first form contains input data for ONE order item and when I press OK I will pass the form's input data to javascript through onsubmit=send_item_data(this) and at the end i will return false; in send_item_data function so it doesn't get posted.
In the second one I want to append/substract the former data in div groups (so I can add more or delete them easily) but I can't think (or find) of a solution that puts in group the data from the first form in one div and appends that child div to the second form.
In the end, by the push of a button, the second form will post all the divs' data and I will handle it with PHP backend code.
Code body:
<form action="" id="first_form" method="post" onsubmit="return send_item_data(this)">
<div id="Select_list">
<select blah blah>
---bunch of options---
</select>
</div>
<div id="extras">
---Some extras that you can choose by a checkbox input (I generate it via PHP)---
example:
<input name="checkbox[<?php echo $row['column_data_from_query']?>]" type="checkbox" value="checked">
</div>
--->Possibly two more input fields here<---
<input type="button" value="clear" onclick="clear_form()">
<input type="submit" value="OK">
</form>
<form action="" id="finalize_order_form" method="post">
--->This is the second form<---
--->In here I want to put the data from the first form so i can handle them by group<---
if I click OK three times in the first form there should be three groups here that contain each form's data
<input type="submit" class="button" value="Finallize order"/>
</form>
I mainly use PHP Mysql and Javascript (including AJAX, not jQuery).
So you want to have the order items listed in the second form like a pre-checkout shopping cart. If you use divs for that, they will not be submitted with the POST data to the server - they will be display-only. So you need to follow Robert's advice and save the 1st form's data to the DB each time an item is added/removed (in addition to his other reasons like not losing a customer's session info). That way the DB will already be up-to-date when they click Confirm Order. Or else you need to hook the Confirm Order button to a JS function that converts the divs back to JSON and posts that to the server to be stored in the DB.
As far as creating the display-only div from the 1st form's data, your send_item_data function needs to loop over all the form's inputs, get their values, and add them to the div however you want them to be displayed. Then you can just insert the div into the second form. Since you are passing "this" to the function, which is the form object itself, you can get the inputs via something like:
var inputs = this.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type == 'submit') continue; //ignore the Submit button
var name = inputs[i].name, value = inputs[i].value;
---use the name and value of this input to construct a span or something to insert inside your div---
}
---now you can insert the div into the 2nd form---

why php script not catching the value from xhtml form input where the form is under div tag?

i am working with multiple form in one page. Basically i have a form in which there are some element and when a user select one of the category from drop-down list in the form, it adds addition elements on the form depending on the category chosen. my problem occurs when i try to get the value from user input and save it into database. After fiddling with the code i realised that the code is unable to catch the value of the form after one div tag has occurred. let me explain through code.
<form>
<input type="text" name="" >
<select name="category" id="category" onchange="show()">
<option name="Phone" value="Phone" >Smartphone</option>
<option name="Laptop" value="Laptop" >laptop </option>
</select>
//(show() function will make one of the div tag visible(depending on category selected) and so it will display on screen.)
<div id="phone" style="visibility:hidden">
<form>
<some input elements....>
</form>
</div>
<div id="laptops" style="visibility:hidden">
<form>
<some input elements....>
</form>
</div>
</form>
now in php when i try to get the input value using $_POST i can only access the value of first div tag which is with id="phone" but when i chose category laptop, even though it display on screen correctly i am not getting the value of user input. i have checked the forms and there is no error since when i swap the forms around, it have same problem.
You have forms contained by other forms. This is not allowed in HTML and usually produces inconsistent and undesired results.
You also have at least one other error (which you repeat). Test your HTML with a validator.
i can only access the value of first div tag which is with id="phone"
Where you have vaguely said <some input elements....>, you might have inputs which share a name. Invisible elements still exist, so the elements in the first (illegal) nested child form will always be submitted.
When PHP populates $_POST and friends, only the first set of data associated with a given name will be stored, the rest will be discarded. (Unless you name the inputs with [] characters).

How can I submit a <textarea> which is outside my <form>?

My website is separated into two parts - a large (CodeMirror) <textarea> and a drop-down menu which has a <form> (that contains "From", "To", "Subject" inputs) in it linked to a send.php file.
The text area and the form itself are located inside different <div>s so I'm not able to link it to the rest of the inputs I'm transferring to the send.php file.
How can I link / connect the submit button to the <textarea> input along with the other inputs it's associated with ("From", "To", "Subject") when transferring the data to the send.php file?
<div id="container">
<div id="sidebar" class="twenty"> //the form div
<li class="menu">
<li class="dropdown">
<form method="post" action="send.php">
<input type=... />
<input type=... />
<input type=... />
<input type="image" src=... alt="Submit Form" />
</form>
</li>
</li>
</div>
<div class="seventy"> //the textarea div
<textarea id="code" name="code">
</textarea>
</div>
</div>
Technically, you could use the form attribute to associate a textarea with a form:
<form ... id="foobar">
...
</form>
...
<textarea form="foobar" ...>
This is an HTML5 feature supported by Chrome and Firefox, for example, but not IE 9.
So check out other options, primarily reorganizing the page as suggested by #niomaster or using JavaScript as suggested by #Fluffeh. However, it’s not a good idea to rely on JavaScript being enabled, in matters of basic functionality. In reorganizing the page, care should be taken to avoid messing up any styling (or scripting) that might rely on the current markup. Also note that the current markup is invalid, since li elements are allowed only as children of ol or ul, so restructuring (if feasible) would be recommendable anyway.
At the simplest, it might suffice to move the <form ...> start tag to the very start of the body element and the </form> end tag right before the end of the body element. Then all form field elements on the page would be fields of this form.
You can make form the outermost tag. It changes nothing to the flow of the document.
You can't directly - if it's not in a form, it can't be submitted.
The best you can do is to write a custom javascript function that replaces your 'submit (image type)' action and copies the data from the textform into a hidden field within the form then submits the form as the last action. This will get the results you want without the user really knowing what you are doing behind the scenes.
Edit: As niomaster correctly points out, forms can span more than just a single <div> or <li> attribute. You can extend it easily without changing your code structure.
use jquery
//Get the text from text area
var textareadata = $("#code").val();
//dynamically append a hidden feild to your form
$('form').append('<input type="hidden" name="myfieldname" value="myvalue" id='myvalue'/>');
// dynamically write your text adtea data to the hidden field append to the form
$('#myvalue').val(textareadata);
amiregelz,
You can just add one id to form tag. using jquery get the value of the text area [$("#text-areaID").val();]and add hidden field using jquery/ javascript, pass the value to hidden field which you created dynamically then after validation of the form submit the form. You can get the value of the textarea as well in your php page.
Hope It will help you. Please mark it answer if it helps.

How to Get the inner text of a span in PHP

Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span's text does change. I am 15, so give me a break! I need some help! This is were the values are picket.
<div>
<select id="test_select" style="display:none;">
<option value="1">Graphic Designer</option>
<option value="2">Animator</option>
<option value="7">Announcer</option>
<option value="11">Web Developer</option>
<option value="12">Judge</option>
</select>
</div>
That is what needs to be gotten:
<div class="custom-select">
<span>Select Option</span>
<ul>
</ul>
</div>
Ok, you're going to need more than just PHP to get this done; you'll need JavaScript as well.
Let's start with your HTML. I'm assuming your rendered output looks like the following and I won't question why you're doing it this way.
<div id="dropdown">
<span>Option One</span>
<span>Option Two</span>
<span>Option Three</span>
</div>
So there's my guess at your HTML.
To post a value back to PHP, you're also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.
<input type="hidden" name="dropdown-selection" id="dropdown-selection" />
So that's our markup done. Next you'll need to grab the selected option from your div and stick it into the hidden field. I'm assuming you've coded something to render your div to look and behave exactly like a dropdown (ok, I'll bite. Why ARE you doing it this way?).
This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that's not true. Well, maybe it's a little bit true)
<script type="text/javascript">
$(function () {
$('#dropdown-selection').val($('#dropdown span:visible').text());
});
</script>
Now, as long as you've ensured that the hidden field is contained within a form that posts back to your target PHP page, you'll have the value of the span tag available to you.
I've made quite a few assumptions about how you've gone about setting up your page here. Correct me on any parts that don't tie into reality.

Categories