submit in html on sql query - php

I need to run this sql query, which give me a list of Id and Dates
I want to click each result and take with me the Id value to the next form
I wrote this query above but i see in the debager that the hidden ID get his value but not pass to the next form
I think i have a problem with the submit() .
where should I put him ?
function ShowAllCarts($user_email) {
$connB = new ProductDAO();
$connB->Connect();
$pro_query = "SELECT * FROM Cart WHERE `Email`='$user_email';";
$db_result = $connB->ExecSQL($pro_query);
$html_result = '<div data-role="content"> <ul data-role="listview" data-theme="b"> ';
$html_result .= '<form action="PreviouscartProduct.php" method="POST"/>';
while($row_array = $db_result->fetch_array(MYSQLI_ASSOC))
{
$Id= $row_array['Id'];
$Date= $row_array['Date'];
//$html_result //
$html_result .="<li><a href='PreviouscartProduct.php'>Cart number: $Id from Date: $Date><input type='hidden' name='Id' value'<?=$Id?>'</input></a></li>'";
$html_result .= '<a onclick="this.form.submit();" </a>;
}
$html_result .= '</form>';
$html_result .= ' </ul> </div>';
$connB->Disconnect();
return $html_result;
}
//display all carts
$func_result = ShowAllCarts($Email);

You need to use a checkbox element:
$html_result .="<li>"
."<checkbox name='cartItem[$Id]' value='$Date'>"
. "Cart number: $Id from Date: $Date"
. "</li>'"
;
Then, in PreviouscartProduct.php, you'd itera over cartItem:
$cartItems = $_POST[ 'cartItem' ];
foreach( $cartItems as $id => $date ) {
... do something ...
}
In case you'd like to take exactly one item, why not use this:
$html_result .="<li>"
. "<a href='PreviouscartProduct.php?cartID=$Id&date=$Date'>"
. "Cart number: $Id from Date: $Date"
. "</a>"
. "</li>'"
;

There's a bunch of HTML syntax errors in this, check the output in a validator
For a start, your opening a tag doesn't close after this.form.submit();, it should read
$html_result .= '<a onclick="this.form.submit();">Anchor Text Here</a>';
Edit :
the anchor element needs to reference the form. Give the form element a name attribute and use something like
onclick="document.nameattributehere.submit();return false"
on the link.
End edit.
Also, in the line above, you're already using the PHP parser when you get to the value attribute of your input, so there is no need for the
<?= and ?>
Finally, in the same tag, you don't need a closing input tag
</input>
Just close the opening tag with
/>
That's just glancing, run the validator for other errors and I'm sure the problem will be clearer.

Related

$_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 :)

PHP: using an html Form to call up a PHP action

I am trying to create a 'submit' input type button that when clicked will call up a switch case action that I've defined in PHP. When the 'submit' button is clicked, I want the form action to essentially create a link and display in the URL form action so that it is called properly by the PHP file.
However when I click on the submit button, the URL does not properly display the desired link and action.
PHP:
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
echo "<form action='enter_time.php?uid='" . $userid . "?action=timesubmit method='get'>";
echo "<td><input name=submit_time type=submit id=submit_time" . $time_cell_row . $time_cell_column . "></input></td>";
echo "</form></tr>";
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
The problem now is when I click on the 'submit' button, the URL displayed enter_time.php?submit_time=Submit" instead of "enter_time.php?uid=3?action=timesubmit
You might want to add in the final apostrophe after timesubmit
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='post'>";
You have a quote after uid that should not be there:
"<form action='enter_time.php?uid=" . $userid . "?action=timesubmit method='get'>";
If you are using a form to submit GET variables into a url you could do something like
<a id="submit_time<?= $time_cell_row . $time_cell_column ?>" href="enter_time.php?uid=<?= $userid ?>">Submit Time</a>
If you prefer to use a form, writing it this way looks clearer to me
PHP
<?php
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
?>
<form action='enter_time.php' method='get'>
<input type="hidden" name="action" value="<?= $timesubmit ?>">
<input type="hidden" name="uid" value="<?= userid ?>">
<input name="submit_time" type="submit" id="submit_time<?= $time_cell_row . $time_cell_column ?>" />
</form>
<?php
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
?>
Just off top of my head it should be:
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='get'>";
Single quote after "uid=" was in the wrong place. Shouldn't be until after "timesubmit".

Multiple PHP MySQL Insertions

I can't think of an easy way to explain what I'm trying to accomplish..
Inserting data into MySQL using php is simple, yet I need to be able to give users the option to add more text inputs in one form...
Just for example purpose...
Users can create a shopping list, the page loads with 15 inputs for 15 items they wish to insert into their shopping list...
At the bottom, they can have the option to add another item, and when clicked, it will show an additional text input..
I've looked for examples but off the top of my head I can't think of any...
if(isset($_POST['createList']){
$item=addslashes(strip_tags($_POST['item']));
}
mysqli_query("INSERT INTO shoppingLists (id,itemName) VALUES (``,`$item`)");
How do insert multiple items with a simple POST?
I was hoping it's possible to use JQuery to add additional input fields.. but how is something like this accomplished on the PHP side?
I do hope I've explained this well enough haha.
You can use an array for your input name attribute
<input type="text" name="item[]" />
And you can browse it by looping through your variable $_POST['item'], that now contains an array with an entry for each field in your form.
I use jQuery .clone() for this.
html:
<div id=="ShoppingList">
<input class="item" name="item[]" />
<input type="button" onclick="addAnotherItem()" />
</div>
js:
function addAnotherItem(){
$("#ShoppingList input.item:first").clone().val("").appendTo("#ShoppingList");
}
I use .val("") so that whatever value the first input has isn't copied to the new one.
Sample Code For This inserting multiple images
if(isset($_POST['addSpace'])){
$spaceTitle = mysql_real_escape_string($_POST['title']);
$spaceBody = mysql_real_escape_string($_POST['text']);
if($_FILES['SliderImage']['tmp_name'] != "" ){
if (($_FILES["SliderImage"]["type"] == "image/jpeg")
&& ($_FILES["SliderImage"]["size"] < 2000000))
{
if ($_FILES["SliderImage"]["error"] > 0)
{
echo "<div class='error_box'><p>Error :: " . $_FILES["SliderImage"]["error"] . ".</p></div>'";
}else{
$path = "../images/prisma-img/demo/services/";
$path2 = "images/prisma-img/demo/services/";
$num = mt_rand();
if (file_exists($path . $num.".jpg" ))
{
echo "<div class='error_box'>"."(".$num .")".
" already exists. "."</div>";
}else{
if(move_uploaded_file($_FILES["SliderImage"]["tmp_name"],$path . $num.".jpg" )){
$mysqlPath = $path2. $num.".jpg" ;
$result = $db->insert("pages","pageTitle, pageImage, pageBody, pageSlug ", "'$spaceTitle','$mysqlPath','$spaceBody','services'");
if($db->affected_rows()){
$id=mysql_insert_id();
echo '<div class="valid_box"><p>Success :: Services successfully Added.</p></div>';
echo "<meta http-equiv='refresh' content='1; url= add-services-slide.php?id=".$id."' />";
}
}
}
}
}else{
echo '<div class="error_box"><p>Error :: Only JPEG file allowed.</p></div>';
}
}
}
?>
Hope that this will help u.

How to update radio group status in a metabox?

I have implemented radio-group option in WordPress metabox. I got the desire radio group in meta box with respective label, but I failed to update my checked status on selecting radio or saving the post where I am using it. I think there is something I need to put.
<div class="my radio group">
<h2>my radio group </h2>
<?php
$cars = array('BMW', 'FERRARI', 'PORSCHE', 'BENTALI', 'MRX', 'CHEVROLET');
foreach ($cars as $car) {
echo '<input name="my-best-car" type="radio" onchange="javascript:document.post.submit()"';
$option = 'id=" ' .$car . '"';
$option = '<value="' . $car . '"';
if ($car == $my_favorite_car) $option .= "checked";
$option .= '>';
$option .= '<label for=" '.$car .' ">' . $car .' ';
$option .= '</label>';
echo $option;
}
?>
</div>
WordPress meta box save function is also added. My other option type like text, select & checkbox updating properly.
While I trying to update my RADIO-GROUP meta values using:
update_post_meta($post_id, 'my-best-car', $_POST['my-best-car'], true);
Ok I found your mistake in foreach loop you have one < extra before the value so the line would look like this:
$option = 'value="' . $car . '"';
Also your js is not submitting the form so just add js function in head like this for example:
<script>
function submitOnClick(formName){
document.forms[formName].submit();
}
</script>
And in the form instead of:
onchange="javascript:document.post.submit()"
put
onclick="submitOnClick(\'myForm\')"
I tried it and it works you just need to rename your form name accordingly

get id of checkbox created from loop php

How would i get the value of the checkboxes that have been selected from these checkboxes created with a loop in php:
<?php
while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count[]" id="count[]" size="3" value=""/>' .
$rowequipment['description']."<br />";
}
?>
The above commenters are wrong: The name attributes for HTML form elements are of type CDATA. Thus, your code is correct. HTML definition found here.
The id attributes are useless, unless you need to operate using JavaScript over the DOM tree. In case you just want to process the control's values using PHP, just drop the ids.
In case you POSTed data, this iterates over all elements named equipment[]:
foreach( $_POST[ 'equipment' ] as checkBoxIndex => checkBoxValue ) {
echo '<br />Checkbox ' . checkBoxIndex . ' has value [' . checkBoxValue . ']';
}
Assuming you're generating these checkboxes in your PHP code and want to match those with checked = "checked" attribute later (maybe in other file).
As others mentioned, the HTML made by the code is not valid, fix it and then you can get what you want using DOM and XPath:
$html = new DOMDocument();
$html->loadHTML($yourMarkup);
$xpath = new DOMXpath($html);
$checkboxes = $xpath->query("*/input[#type='checkbox' and #checked='checked']");
if (!is_null($checkboxes)) {
foreach ($checkboxes as $checkbox) {
echo $checkbox->nodeValue;
}
}
If you want to get the checkboxes values on form post, then use $_POST['key'] which key is checkbox name attribute.

Categories