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(){
Related
ASP.NET WebForms is outdated, but in one aspect it's far better than PHP: The ViewState. When you submit the form, controls stay. In PHP, you have to do everything all by yourself, rendering HTML.
Quickly, I come up with code that looks like this:
print('<input type="text" name="txtDate" value="' . (isset($_POST['btnSave']) ? $_POST['txtDate'] : Common::FormatDate($row['Date'])) . '" class="datepicker ' . ($dateValid ? '' : 'invalid') . '" />');
So I decided to at least create basic functions for rendering controls:
class Controls
{
public static function TextBox($name, $id, $value, $class)
{
return '<input type="text" name="' . $name . '" id="' . $id . '" value="' . htmlentities($value) . '" class="' . $class . '" />';
}
}
This makes things more comfortable, but it's just not the same as having a ViewState.
Not to mention that you have to duplicate code for both edit and create with different settings:
Create: value = whatever is in $_POST
Edit: value = If form is submitted, take whatever is in $_POST, otherwise take the value from the database/model. (ternary operator here)
This get's even harder when you show/hide popups!
All in all this is a very hard and uncomfortable way and I have absolutely no idea where to start improving it. What are your suggestions?
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 :)
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')
});
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
$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_#