Jump to content

makamo66

Members
  • Posts

    64
  • Joined

  • Last visited

Everything posted by makamo66

  1. Why would submitting the form change anything? There is nothing in my $_POST variables upon submission. In CakePHP $_POST would be $this->request->getData('draggables') but this is empty as well.
  2. I've tried ending the names with square brackets to make them into arrays but this doesn't work in CakePHP. My form simply does not submit if I include the square brackets.
  3. I don't need to change the value of the hidden input after all. I just need to submit an array with the ids like follows to the database. var draggables = []; draggables.push("draggable3"); draggables.push("draggable4"); and the following alert responds with draggable3, draggable4 as it should alert(draggables); But draggables is a javascript array and I need the PHP array in order to submit it to the database. How can I get the clientside variable on the server side?
  4. The name of the hidden input is the name of the field in the database and so in cakephp it automatically saves the field name/input name to the database when the form is submitted so maybe this is a question for a cakephp forum and not a plain php forum.
  5. This only works for the hidden input with the id of draggable4 because that is the last input with the name draggables.
  6. The array push isn't working but my last two posts are incorrect and there is no option to delete them. The database is being updated with the last value that gets set for the hidden input after all so it's not true that the value was hard coded in the input array. Also, using the same name for multiple hidden inputs works in cakePHP after all so that was incorrect as well.
  7. None of this works for more than one hidden input because the name is what gets saved to the database and the hidden input can't have several names with the same value. I can't do <input type="hidden" name="draggables" id="draggable3" multiple="multiple" value=""/> <input type="hidden" name="draggables" id="draggable4" multiple="multiple" value=""/> <input type="hidden" name="draggables" id="draggable5" multiple="multiple" value=""/> because the name is repeated. CakePHP saves the input name to the database with the same field name so I guess this belongs in a CakePHP forum after all.
  8. Correction: Nothing is getting added to the database. The value was being set in the hidden input tags before and I didn't realize that so I thought it was being pushed to the array but the value was actually hard-coded in the input tag.
  9. var z = $("#draggable4").val("draggable4"); is an object. If I do alert(z), it shows [object Object].
  10. Now I'm trying to push the z element to an array but the following code doesn't work. The code adds the last z that was selected to the database instead of adding an array of zs. var draggables = []; if(data == "draggable3"){ alert("data " + data); var z = $("#draggable3").val("draggable3"); draggables.push(z); } //end if if(data == "draggable4"){ alert("data " + data); var z = $("#draggable4").val("draggable4"); draggables.push(z); } //end if
  11. I found code that works: var z = $("#drag12").val("draggable3"); updates the field in the table with draggable3
  12. I am trying to set the hidden input in the javascript but it doesn't set it in the php. This is the form with the hidden input. It corresponds to a field in the database table with the name of draggables. <form method="post" accept-charset="utf-8" id="myForm2" action="/mealplans/add"> <input type="hidden" name="draggables" id="drag12"/> <button id="myId" type="submit">Submit Form</button> </form> This is where I change the value in the javascript: if(data == "draggable3"){ alert("data " + data); var x = document.getElementById("drag12").value; x = "draggable3"; alert(x); } //end if The alert pops up with draggable3 as it should but the value of null is what gets saved in the database. The javascript doesn't change the PHP value; it only changes the client side value. I've also tried getElementsByName but that didn't work either. var y = document.getElementsByName("draggables").value; //only changes it on the client This is actually a cakephp site but this problem is with the PHP so I am publishing it here.
  13. Thank you for your helpful input. I implemented the code that you provided and used $_SESSION["cart_items"] instead of $cart_items and now it works!
  14. The var_dump of the $cart_items array is supposed to show an array of $cart_items with the quantities added together for the same id but it doesn't even show an array, it just shows one value. session_start(); $cart_items = $_SESSION["cart_items"]; if ( $cart_items == null ) { $cart_items = array(); } if ( isset($_REQUEST["element_id"]) ) { $id = $_REQUEST["element_id"]; } if ( isset($_REQUEST["quantity"]) ) { $quantity = $_REQUEST["quantity"]; } if ( isset($id) && isset($quantity) ) { if (isset($cart_items[$id])) { $cart_items[$id] += $quantity; } else { $cart_items[$id] = $quantity; } } var_dump($cart_items);
  15. ThanX ginerjm. Your code worked perfectly (except that the dollar signs needed to be escaped)
  16. <table border='1'><tr><th>Remove&nbsp;&nbsp;&nbsp;</th><th width='100'>Your Product&nbsp;&nbsp;</th><th>Quantity</th><th>Description</th><th>Price per item</th><th>Sub Total</th></tr> <?php echo "<form method='post' name='yourForm'>"; echo "<tr><td></td>"; echo "<td><img src=" . $thumbnail . "/></td>"; echo "<td>" . $cart_product_quantity . "</td>"; echo "<td> " . $product . "(s)&nbsp;&nbsp;</td>"; echo "<td> \$" . $price . "</td><td> \$" . $number . "</td></tr>"; echo "</form>"; echo "</table>";
  17. When I echo out my variable in the php file it works fine but when I put the variable in a table cell it doesn't echo out.
  18. How do I get my session to be unset? My form looks like this: foreach ($joined as $i => $qty){ echo "<form action='' method='get' name='yourForm'>"; echo "<button type='submit' name='remove_" . $i . "' value='remove_" . $i . "' class='deletebtn' >X</button>"; echo "</form>"; } My SESSION is defined like this: for($i=0; $i<=5; $i++){ if (isset($_REQUEST["element_id_$i"]) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; } and the form submits to: if (isset($_REQUEST["remove_$i"]) ){ unset($_SESSION["quantity[$i]"]); unset($_SESSION["element_id[$i]"]); var_dump($_SESSION["element_id"]); var_dump($_SESSION["quantity"]); echo "Received variable " . $_REQUEST["remove_$i"]; echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id[$i]"] . '<br><br>'; } The output is: array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } Received variable remove_1 Notice: Undefined index: element_id[1] in C:\xampp\htdocs\TopView\cart5.php on line 147 TARGET INDEX TO BE REMOVED:
  19. When I do the following, it still doesn't work: <?php for($i=0; $i<=5; $i++){ echo "<form action='' method='get' name='yourForm'>"; echo "<button type='submit' value='remove_" . $i . "' name='delete' class='deletebtn'>X</button>"; echo "</form>"; } for($i=0; $i<=5; $i++){ if (isset($_REQUEST["remove_$i"])){ echo "delete"; echo "Received Value: " . $_REQUEST["remove_$i"]; } } ?>
  20. When I remove exit it still doesn't work.
  21. The file test2.php consists of these simple forms: <?php for($i=0; $i<=5; $i++){ echo "<form action='reset4.php' method='get' name='yourForm'>"; echo "<button type='submit' value='delete' name='remove_" . $i . "' class='deletebtn'>X</button>"; echo "</form>"; } ?> It submits to reset4.php which is this simple code: <?php header("Location: test2.php"); exit; for($i=0; $i<=5; $i++){ if (isset($_REQUEST["remove_$i"])){ echo "Deleted"; }} ?> But it doesn't work. The $_REQUEST doesn't populate the address field and it apparently never gets submitted to reset4.php like it should. This is such a simple program, I can't imagine why it doesn't work.
  22. $id is defined like this: for($i=0; $i<=5; $i++){ if (isset($_REQUEST["element_id_$i"]) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; } and $quantity is similar.
  23. When I use this: $cart_items[$id] = $quantity; or this: $cart_items = array($id => $quantity); I get the error: Illegal offset type
  24. This is the cart array: $cart = array( "1" => "2", "3" => "4", "5" => "6"); var_dump($cart); array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" } This is the cart items array: $cart_items = array(); $cart_items["id"] = $id; $cart_items["quantity"] = $quantity; var_dump($cart_items); array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } } I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.