Can't $_GET contents of text field from element printed in PHP - php

I've got a .phtml form which allows a user to select from a box a quantity of boxes ranging from 1 to 5.
Depending on the value selected, I use JS to call a PHP method using Ajax to print dimension fields for these boxes which the user is then required to fill in.
for ($counter = 0; $counter < $boxes; $counter++){
if ($counter%2==0){
echo "<tr class=\"even pointer\">";
}
else{
echo "<tr class=\"pointer\">";
}
echo "<td>" . ($counter+1) . "</td>
<td class=\"value\"><select name=\"dispatch_parcel_" . $counter . "_type\" id=\"dispatch_parcel_" . $counter . "_type\" style=\"width:138px\">" . $this->getBoxTypes() . "</select></td>
<td class=\"value\"><input name=\"dispatch_parcel_" . $counter . "_length\" id=\"dispatch_parcel_" . $counter . "_length\" value=\"n/a\" disabled/></td>
<td class=\"value\"><input name=\"dispatch_parcel_" . $counter . "_width\" id=\"dispatch_parcel_" . $counter . "_width\" value=\"n/a\" disabled/></td>
<td class=\"value\"><input name=\"dispatch_parcel_" . $counter . "_height\" id=\"dispatch_parcel_" . $counter . "_height\" value=\"n/a\" disabled/></td>
<td class=\"value\"><input name=\"dispatch_parcel_" . $counter . "_weight\" id=\"dispatch_parcel_" . $counter . "_weight\" value=\"n/a\" disabled/></td></tr>";
}
Up until this point everything works 100%.
This is all printed into a div within the existing form. After the div field, there is a submit button which calls a method to do calculations based on these dimensions.
However, when trying to $_GET the value of the input elements printed above, I am getting an error as follows
Notice: Undefined index: dispatch_parcel_0_length in /chroot/home/[mysite][/[mysite]html/app/code/local/[mynamespace]/[mymodule]/controllers/Adminhtml/[mycontroler].php
(I get similar errors trying to fetch any other of the printed elements' content)
I have quadruple checked that:
-The exact element does exist with firebug & chrome-dev-tools
-The form's method is get and,
-The div in which the elements are printed is within the form's fieldset.
Would really appreciate some input - thanks very much in advance.

disabled elements are not send, try using readonly

Related

jQuery Modification of Ajax Populated Form Fields

I have a form, and when a certain select box changes, it uses Ajax to populate some checkboxes. Works great.
However, when I check these checkboxes, I'd like to use jQuery to modify another part of the form - this does not seem to be working... the jQuery does not fire at all.
Is this something to do with when the jQuery loads vs the ajax request and its just impossible? Or is there likely an error somewhere in my code?
The Ajax.php page (which works just as expected)
... some other code ...
$fields = "";
$i = 0;
foreach($cols as $c){
$fields .= '<tr><td width="50%"><input class="fieldCheckBox" data-num="' . $i . '" type="checkbox" name="data[' . $_GET['t'] . '][fields][' . $i . '][field]" value="' . $c . '"> ' . $c . '</td><td width="50%"><input type="text" name="data[' . $_GET['t'] . '][fields][' . $i . '][fieldAs]" class="form-control" style="width: 150px;"></td></tr>';
$i++;
}
echo $fields;
And then my jQuery request (which isn't firing)
... document.ready function...
$(".fieldCheckBox").change(function(){
alert("were in!");
});
This is your problem:
$(".fieldCheckBox").change(function(){
The value of the checkbox is not actually changing so this will never execute.
Instead you can check for the click event for example:
$(".fieldCheckBox").click(function(){
Edit: If the checkboxes don't exist on page-load, when your javascript is executed, you need event delegation to make sure the events get registered.
A simple example:
$('body').on('click', '.fieldCheckBox', function(){

$_POST doesn't work for PHP post. ISSET reports that no such value was forwared via post method?

I have a form for a comment section. Here every comment has unique IDs.
But however, the comments aren't forwarded to the action PHP form.
Code for comments:
echo '<form action="interact.php" method="post">';
$new_refreshed_ID = 'uni_story_ID_' . $row['ID'] . '_comment_ID_' . $cache_ready_new_comment_ID;
echo '<textarea name="' . $new_refreshed_ID . 'rows="12" cols="70"></textarea>';
echo '<button type="submit">Submit</button>';
echo '</form>';
$_SESSION['assoc'] = $row['ID'];
$_SESSION['cache_comment_details'] = $new_refreshed_ID;
My code for receiving the request:
interact.php:
<?php
session_start();
$assoc = $_SESSION['assoc'];
$get_comment = $_SESSION['cache_comment_details'];
if(isset($_POST[$get_comment])) {
echo "yea!";
} else {
echo "no!";
die();
}
I get no in the interact.php which means that no data was forwarded.
How can this be?
btw comment id's are in this manner (for example):
uni_story_ID_4_comment_ID_17
I did check $new_refreshed_ID. It is showing all the values properly as desired. I did start the sessions in the both PHP files.
echo '<textarea name="' . $new_refreshed_ID . 'rows="12" cols="70"></textarea>';
You need to close the name of textarea like this:
echo '<textarea name="' . $new_refreshed_ID . '" rows="12" cols="70"></textarea>';
Edit:
Even Better to not use php when not needed (to avoid those) you can maybe do something like this:
<textarea name="<?php print $new_refreshed_ID;?>" rows="12" cols="70"></textarea>
Please note that this is an Example, i'm only printing what is really needed with php, otherwise i'll stay with html :)

Dynamic ID between PHP, SQL and jQuery

I have a SQL database that PHP is getting the contents of. It gets the info from each row of a table and displays it on a web page. There is also an anchor and a hidden div containing more details from that row of the SQL table. When the use clicks the anchor the jQuery needs to detect it and show the hidden div. The problem I have is that each anchor and hidden div have a dynamic id of:
'ev_a_' . $row['id']
'ev_' . $row['id']
the jQuery I assume needs to look something like this:
$('#ev_a_')[].click(function(){
$('#ev_')[].show();
}
I'm not entirely sure how this works and I've never used dynamic ids before in jQuery. Any suggestions?
I should point out the PHP/SQL code is:
while($row = mysql_fetch_array($q_result)){
echo "<br/><b>" . $row['a'] . "</b><br/>" . $row['b'] . ", " . $row['c'] . "<br/>" . $row['d'] . "<br/><a class='ev_event_a'>More Details</a><br/><div class='ev_event'>" . $row['e'] . "</div>";
}
I would use a class instead for these links instead and set the ID in a data attribute. Depending on your needs you don't even need the ID as you can show the next element if it is already part of the DOM.
A simple example (ID not used...):
Toggle details
<div class="initially_hidden">
...
</div>
....
Toggle details
<div class="initially_hidden">
...
</div>
....
and the javascript:
$('.show_more').on('click', function(e) {
e.preventDefault();
$(this).next().slideToggle();
// if you need the ID, you can get it via $(this).data('id')
});

PHP Refresh button onclick

I have this while loop that echoes entries from a SQL database. I have an "Add to Cart" button which is supposed to remove the entry from the list and add it somewhere else. This part works, but once I click on the "Add to Cart" button, the entry is not removed right away, I have to manually refresh the page or visit another page and come back in order to see that the item is removed. I tried using the following (and few similar) lines on the onclick propery of the button, but it didn't work.
echo "<br /><center><input type='submit' name='submitAdd' value='Add to Cart' onclick='window.location.reload();'></center>";
Any suggestions are appreciated.
Thank you.
EDIT: This is the while loop that I use to generate the table. It is within a tag which is within the .
while ($row = mysql_fetch_array($song_query)) {
foreach ($_SESSION['selected_items'] as $key => $value) {
if ($value == $row['Item_ID']) {
$rowID = $row['Item_ID'];
echo "<tr style='background-color: #66FFFF;'>";
echo "<td><input type='checkbox' name='removeFromCart[]' value='$rowID'></td>";
echo "<td>" . $row['Item_ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Artist'] . "</td>";
echo "<td>" . $row['Album'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Year'] . "</td>";
echo "<td>" . $row['Bit_Rate'] . "</td>";
echo "<td>" . $row['Sample_Rate'] . "</td>";
echo "</tr>";
}
}
}
PHP is a server-side language, so any "actions" will be executed on the server, before the results are displayed to the user. In order to modify the page after it has been loaded on the user's browser, you have to use a client-side language, such as JavaScript. If you want to show an update to your cart without having to reload the page, then using an AJAX request is your best option. Check out the tutorial at http://www.w3schools.com/ajax/ and let me know if you have any questions about how to implement one :)
Update: Force Reload
If you want to just stick with reloading the page as is, try onclick='window.location.reload(true);' instead of onclick='window.location.reload();'. The true parameter forces a get request to the server. http://www.w3schools.com/jsref/met_loc_reload.asp
I use a link that references to the same page
Recargar
If the only feature you are looking for is to reload the page on the button's click, the way I typically do that is onclick="history.go(0)"
But if the form button is what's submitting a form and already reloads the page then I don't see why you couldn't put the while loop after your other code like this,
CHECK FOR FORM() {
UPDATE SQL DB
}
WHILE () {
OUTPUT DB DATA
}
not a button but a simple link, but maybe still useful for some:
<?php
$page = $_SERVER['PHP_SELF'];
print "Reload this page";
?>
Use header() function to refresh a web page in PHP. The PHP header() function sends an HTTP header to a client or browser in raw form.
Before HTML, XML, JSON or other output has been sent to a browser or client, a raw data is sent with the request (especially HTTP Request) made by the server as header information.
Syntax:
header ("header:url of the page")
I hope that this can place you on the right way.

how can I recieve this form in cart.php

$index = 1;
foreach($product['varieties'] as $variety){
echo '<input style="width:10px; margin-left:9px; " name="price_' . $index . '" type="checkbox" value="' . $variety['price']. '" />';
echo '<input name="size_' . $index . '" type="text" value="' . $variety['size']. '" />'; $index++;
}
if you can see This will have an index=1 and it will be incrementing where each iteration will be price_1, price_2, etc.and size_1,size_2. Now with a dynamic name="" input how can I receive in the cart.php when each name will be different?
it would be something like $price= "'..',$_POST['price_']"? well I have not idea how can I receive this name index from the cart.php url.
Thnank you.
Instead of string building the name like size_1, why not make the name like this size[].
Then you can instantly access it like an array via PHP.
Look at alex's answer. It is better suited to the circumstances..
You could, at the end, add a hidden text box with the number of items (the $index value). and read it back
so after the loop add
echo '<input name="counter" type="hidden" value="' . $index. '" />'
and when you try to read it, the first thing would be to read the $POST['counter'] and then loop again from 1 to that value reading the $POST['price_'.$loop_counter]
You could find the values with something like this:
$prices = preg_grep('/^price_\d+$/', $_POST);
foreach($prices as $P) {
$idx = substr($p, 5); // extract the digits, which we know will be at position 5->end
$size = $_POST['size_' . $idx];
etc....
}
This assumes for that for every price_#, there's a corresponding size_#

Categories