Form submitting in php - php

--EDIT---
i have created a text box area where users can input some data,
Basically when i press the submit button, it should save the inputted data. here is the full code, i can't fix it. :/
The user entry does not get updated.
<?php
if ( $act == "htmlprofile" ) {
?>
<div class="contentcontainer">
<div class="contentmboardheaderarea"><img src="images/header.png" />
<div style=”word-wrap: break-word”><div class="contentheadertext"><?php echo "$hlang2"; ?></div></div></div>
<div class="contentheading">
<form id="htmlform" name="htmlform" method="post" action="options.php?act=htmlsubmit">
<div class="contentheading4">
<?php echo "$olang15"; ?></div>
</div>
<div class="contentmboardlistarea2"><textarea id="htmlprofile" name="htmlprofile" cols="33" rows="10"><?php echo $qry2[htmlprofile];?>
</textarea></div></form>
<div class="contentbuttonarea">
<div class="contentbutton1" onClick="document.location.href = 'profile.php?act=index'";><?php echo "$glang3"; ?></div>
<div class="contentbutton2" onClick="document.forms['htmlform'].submit();"><?php echo "$glang21"; ?></div>
<div class="contentbutton3"></div>
<div class="contentbutton4"></div>
<div class="contentbutton5" onClick="document.location.href = 'help.php#htmlprofile'";><?php echo "$glang5"; ?></div>
</div>
</div>
<?php
}
?>
<?php
if ( $act == "htmlsubmit" ) {
$save ='Profile updated successfully';
$query4 = "UPDATE members SET htmlprofile = '$htmlprofile' WHERE username = '$myuser'";
mysql_query($query4);
?>

There is no value attribute in textarea you have to enter the content between the <textarea>text</textarea>
See for reference tag_textarea
It should be like this
<textarea id="htmlprofile" name="htmlprofile" cols="33" rows="16" >
<?php echo $qry2[htmlprofile]; ?>
</textarea>
Refer to this answer also which-value-will-be-sent-by-textarea-and-why

Your div contentheadertext is not contained within the <form></form> so if you have inputs there, they won't be included in the form submission.
To resolve, move the opening <form> tag higher, so that it encloses all inputs.

the submit works fine. But you have no variables with the post data definied. And textareas haven't "value". And when you don't need fixed strings in your echo, don't use the "
try the following:
<textarea id="htmlprofile" name="htmlprofile" cols="33" rows="16"><?php echo htmlspecialchars($_POST['htmlprofile']); ?></textarea>

if you are trying to use a php variable inside HTML do that simply by echo $myvariable ; Quotes not required there. But if u are trying to echo a string use quotes just like echo 'mystring' .
Moreover when you are trying to pass some values as part of form submission, the required values should be between the tags
if you want to show any value inside text area, remember it doesn't have a 'value' attribute. So write the code like this: value to be displayed

Related

PHP CodeIgniter: Updating a dynamic array bound to an input field

What I'm about to show might be the most horrific code in existence, so be prepared. I'm new to PHP and received a CodeIgniter project. Here we go:
In my edit_article view, I dynamically generate <input> fields and make them accessible to the controller by posting them as an array, notice name="pricelevel_checked_array[]":
<form id="form-work" class="form-horizontal" role="form" autocomplete="off" method="post">
<!-- excluded code to display form content -->
<?php
$pricelevel_array = array();
$count = 0; ?>
<?php foreach($array_used_for_loop as $item_used_for_loop): ?>
<?php $article_group_price = ""; ?>
<!-- excluded code to fill $article_group_price -->
<div class="col-sm-2">
<div class="checkbox">
<span class="bg-transparent left">
<input type="checkbox" data-init-plugin="switchery" data-size="small" data-color="primary" id="<?=$count?>"
<?php if($article_group_price !== ""): ?>
<?php array_push($pricelevel_array, 1); ?>
checked="checked"
<?php else: array_push($pricelevel_array, 0); ?>
<?php endif; ?>
onchange="groupprice_active_changed(this)"/>
</span>
</div>
</div>
<input hidden type="number" id="pricelevel_checked_array" name="pricelevel_checked_array[]" value="<?=$pricelevel_array[$count];?>">
<?php $count++; ?>
<?php endforeach; ?>
</form>
As you can see, I fill that array with 1's or 0's depending on the value of $article_group_price (I get these values from the controller and originally the database).
It all works fine upon first loading the view and the array is filled correctly, but I can't seem to update the array when I check- or uncheck a checkbox.
I've tried to do this quick and dirty using javascript onchange="groupprice_active_changed(this)" where I would use the $count variable to change the index of the array, but unfortunatly that didn't work out since I only get one value and not the entire array:
<script>
function groupprice_active_changed(obj) {
if($(obj).is(":checked")){
alert("Yes checked");
var input_value_array = document.getElementById('pricelevel_checked_array').value;
console.log(input_value_array);
for (index = 0; index < input_value_array.length; index++) {
console.log(input_value_array[index]);
}
}else{
alert("not checked")
}
}
</script>
How can I best update this array or change my code so I can post the dynamic generated checkboxes to the controller? Another problem is that I need the checkbox-id in the controller, even if it's false. And that the browser doesn't post an unchecked checkbox value. So, just passing the checkboxes isn't an option.
I'm of course prepared to post more code.
Thank you
Disclaimer: Please no steal
View: Complete code
Controller: Complete code
Edit 1: Changed 'php' to 'the browser' in the last section
Edit 2: Added as good as the whole code because filtering out will only make it more difficult.
One technique I have see used to make checkboxes behave better is to use
a hidden input with the same name as the checkbox. Put the hidden input before
the checkbox. If the checkbox is not checked, the value of the hidden input
gets sent. If you check the checkbox then the checkbox value overrides the hidden input.
This works because only one of the values is sent. The browser sends the later one.
Take a look at the following example.
<html>
<head>
</head>
<body>
<div>
<form method="post">
<input type="hidden" name="foo" value="off">
<input type="checkbox" name="foo" /> Foo
<input type="submit" />
</form>
</div>
</body>
</html>
<?php if ( ! empty($_POST) ) : ?>
<div> Hello </div>
<div>
Checkbox is <?= $_POST['foo']?>
</div>
<?php endif ?>
I eventually made the decision to drop the whole array approach and follow the KISS principle by passing my index to the controller. I use these indexes to only save the rows that are checked. Thanks to #ryantxr for showing me that I was overthinking all this.
v_edit_article
<?php $index = 0; ?>
<?php foreach($array_used_for_loop as $item_used_for_loop): ?>
<?php $article_group_price = ""; ?>
<!-- excluded code to fill $article_group_price -->
<div class="col-sm-2">
<div class="checkbox">
<span class="bg-transparent left">
<input type="checkbox" name="group_active[]" data-init-plugin="switchery" data-size="small" data-color="primary" value="<?=$index;?>"
<?php if($article_group_price !== ""): ?>
checked="checked"
<?php endif; ?>/>
</span>
</div>
</div>
</div>
<?php $index++; ?>
<?php endforeach; ?>
admin (controller)
public function edit_article($id) {
// Excluded code
$group_prices_active = array();
if(isset($data['group_active'])):
echo "<script>console.log('GROUP_ACTIVE ".print_r($data['pricelevel_checked'])."');</script>";
foreach($data['group_active'] as $value):
array_push($group_prices_active, $group_prices[$value]);
endforeach;
echo "<script>console.log('GROUP_PRICES_ACTIVE ".print_r($group_price_active)."');</script>";
unset($data['group_active']);
endif;
// Excluded code
$this->m_articles->edit_article($data, $id, $article_types, $group_prices_active);
$this->session->set_flashdata('succes', $this->lang->line('edit_success'));
redirect('/administrator/articles', 'refresh');
}

Populate input values with PHP variables with related ID

I am trying to create a form when the user clicks a button on a selected element. However, when i try it, i click a button and it either loads multiple, or loads 1 but shows only one value, if that makes sense. Here is my code:
<?php
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<div class="service_display">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
</div>
<?php
}
?>
I have a form that will appear with jQuery when the button is pressed, and im trying to fill the values with the respective id's. Here is my code for that:
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
I think the problem is me not knowing where to put that code, if i put it in the while loop, it works, but understandably, i have like 10 input fields pop up..
if i put it outside the while loop and run a separate query to fill it, its empty..
As per you explanation of the problem its clear that you are looping the forms along with the front end display divs, which is not that good idea.
Always its better to use one modal window and then depending upon the click you have to load the data onto it and then pop it up.
Anyway for your scenario there may be more than one solution but this one will also serve the purpose:
Put the "show-onclick" whole div inside the "service_display" div so that now also it gets looped with no issues.
Now when the button gets clicked find the closest "service_display" div, it will give you the dom element & then use that and popup the "show-onclick" div enclosed within that.
Follow the code below:
<?php
$database = new mysqli("localhost", "root", "", "so");
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
?>
<div class="service_display" id="sd_<?php echo $row['id']; ?>">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
</div>
<?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".show-onclick").css("display", "none");
$(document).on("click", ".additional_files", function() {
$(".show-onclick").css("display", "none");
var sd_id=$(this).closest('div.service_display').attr("id");
$("#"+sd_id+" .show-onclick").css("display", "block");
});
});
</script>
The only change i made to your code is i have added a dynamic id field to service_display div.
And i have used jquery to show and hide the forms of respective clicked buttons.
Again here i have used oop based mysqli which is not mandatory yours is also perfectly fine.

PHP: Confirmation Dialog without using JavaScript?

I have a very simple form:
<form action="system/web/push.php" method="post">
<div class="row">
<div class="col card_input_push card_input_push_language">
<select name="<?php echo PARAM_PUSH_LANGUAGE; ?>">
<?php echo get_languages($mysqli); ?>
</select>
</div>
</div>
<div class="row">
<div class="col card_input card_input_push">
<input type="text" name="<?php echo PARAM_PUSH_TITLE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_TITLE]; ?>"<?php echo set_value(PARAM_PUSH_TITLE); ?>required/>
</div>
</div>
<div class="row">
<div class="col card_input_push card_input_push_message">
<textarea name="<?php echo PARAM_PUSH_MESSAGE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_MESSAGE]; ?>"<?php echo set_value(PARAM_PUSH_MESSAGE); ?>required></textarea>
</div>
</div>
<div class="row">
<div class="col text-center card_input_push card_button_push_send">
<button class="btn btn-default" type="submit" name="<?php echo REQUEST_SEND_GCM; ?>" value="<?php echo $language_array[LV_PUSH_INPUT_SEND]; ?>"><?php echo $language_array[LV_PUSH_INPUT_SEND]; ?></button>
</div>
</div>
</form>
When i now press the submit button i dont want the post to perform instantly. I first want a confirmation dialog to show up with yes or no answers and only if the user says yes i want the post action to be performed.
What i NOT want is to use JavaScript. Is there a way to do that?
EDIT
Thank's for the answers. To satisfy all respondents i would like to tell you, why i dont want to use JavaScript. The answer is simple. I have no idea about JavaScript. I never have used it before but the project im working on (as always) has to be finished ASAP. That's the only reason.
Yes there is a way, use GET (not POST) the first time, on the PHP file check if GET was used and if so generate a form that sends confirmation form back to the user. Then make confirmation form to submit using POST and you are done.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// generate confirmation form
}elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// process your data
}
If anyone has access to a book called Head First PHP-MySQL they cover a similar example on page 280.
Maybe you could do something like this, with some css for design your dialog.
change your form with :
<form action="this_page">
If you submit, you call this page, display the dialog with a form, and when you submit with second form, you post to your needed php file.
if (isset($_POST['your_submit'] )) {
echo "<yourdialog>";
echo "<form action="system/web/push.php">";
echo "content";
echo "<submit yes>";
echo "</form>";
echo "</yourdialog>";
}
Your workflow is:
gather form data
send to server
get confirmation that that's what the user wants.
if yes then process data.
If you want to do that all via php then the server's handling the full load, so you'll want two forms, one to collect and one to confirm. And you'll want to store the first form's data so you can process it according to the second form's result. A bit old fashioned but not difficult.

PHP separate id for every div

to be honest this is more of a how to then help with code i already have. So i hope this is okay, else of course i will delete my question again. Anyway here goes i have a site with boxes, with a picture headline and a submit button. All the info in these boxes is being delivered, from my database. And of course in my database i also have a id cell, and if i try to echo out the id cell with the rest of the info in the box it shows up fine. But when i try to assign the id output variable to a header location, i do for some weird reason always get the id 3. Eventhough the id´s shows up perfectly fine, in the boxes. I have included my php code and i am still a beginner to php so sorry for this noob question. :)
session_start();
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_array($sqlSelect))
{
$id = $feed['id'];
if(isset($_POST['readArticle']))
{
$id = $_SESSION['id'];
header("Location:"."redirect.php?".SID.$idArticle);
}
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<?php $idArticle= $feed['id'];?>
<h2><?php echo $feed['headline'];?></h2>
</div>
You are setting $idArticle at the bottom of the loop but trying to use it at the top so it will be pulling it from the previous result. Try:
while ($feed=mysqli_fetch_assoc($sqlSelect)){
$idArticle= $feed['id'];
$sid = $_SESSION['id'];
if(isset($_POST['readArticle']))
{
header("Location:"."redirect.php?".$sid.$idArticle);
}
//rest of code
}
You will have to put div inside the loop.
I also replaced the header redirect with form action attribute (you may want to replace method POST with GET instead).
ID is passed with a hidden field
<?php
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_assoc($sqlSelect))
{
$id = (int)$feed['id'];
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="redirect.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<h2><?php echo $feed['headline']; ?></h2>
debug: <pre><?php print_r($feed); ?></pre>
</div>
<?php } // end of while loop ?>

How to pass image from one page to other using PHP?

I have been trying to execute the code for long time but rest then my image all other variables are working. My images are stored in server folder called "uploaded".
I have two pages called "show.php" and "readmore.php", and I want to pass the image from show.php to readmore.php but other then image all the variables are displaying in readmore.php.
My code for show.php
<a class="btn" href="readmore.php?ad_title=<?php echo $rec['ad_title']; ?> & price= <?php echo $rec['price']; ?> & address= <?php echo $rec['address']; ?> & phone= <?php echo $rec['m_number']; ?> & description= <?php echo $rec['description']; ?> & contact_name= <?php echo $rec['contact_name'] ; ?> & photo_name= <?php echo $rec['photo_name']?>;">
My code for readmore.php (I am displaying here two of the variables along with the image)
<div class="control-group">
<label class="control-label">About the book</label>
<div class="controls">
<label class="checkbox">
<?php echo $_GET['description'];?>
</label>
</div>
</div>
<div class="control-group">
<label class="control-label">About the book</label>
<div class="controls">
<label class="checkbox">
<td id="pic" width="300px"><img src= "../uploaded/<?php echo $_GET['photo_name'];?>" style="border-radius:5px; padding:3px; "/></td>
</label>
</div>
</div>
Learn to debug:
On first lines of readmore.php dump the superglobal variable $_GET and see whether value of $_GET['photo_name'] is coming or not.
Eg.
echo "<pre>";
print_r($_GET);
echo "</pre>";
P.S. <pre> tag is just for formatting the output. print_r dumps the values of $_GET.
If you see value of 'photo_name' index in the GET array,
there should be some errors in your image path.

Categories