page refreshes but doesn't update the new content - php

the code below works fine it updates the rank in db when submit button is clicked, but then it refreshes the page and loads the old content itself instead of updating with new content. how do i get it to load the new content on refresh.
<?php
function update_ranks($newimgurl,$newrank){
switch ($newrank)
{
case "1":
$rank="1st";
break;
case "2":
$rank="2nd";
break;
case "3":
$rank="3rd";
break;
}
$field_key = "field_582fdg46";
$value=array();
$count=0;
global $totalimgs;
global $allimgurl;
global $allimgranks;
while ($count < $totalimgs){
if ($allimgurl[$count]==$newimgurl){
$value[] = array("imglink" => $newimgurl,"rank" => $rank );
}
else{
$value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] );
}
$count=$count+1;
update_field( $field_key, $value, $post->ID);
}
unset($newrank,$newimgurl,$currentnewrank,$currentimgurl,$rank,$count);
unset($allimgurl,
,$allimgranks,$totalimgs,$value);
}
?>
<?php
if(isset($_POST['rank1btn'])){
$currentnewrank=$_POST['rank1'];
$currentimgurl=$rank1img;
update_ranks($currentimgurl,$currentnewrank);
}
if(isset($_POST['rank2btn'])){
$currentnewrank=$_POST['rank2'];
$currentimgurl=$rank2img;
update_ranks($currentimgurl,$currentnewrank);
}
if(isset($_POST['rank3btn'])){
$currentnewrank=$_POST['rank3'];
$currentimgurl=$rank3img;
update_ranks($currentimgurl,$currentnewrank);
}
?>
<form id="myform" name="myform" action="" method="POST" >
<article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">
<?php
global $post;
$bookid=get_the_title($post->ID);;
$args = get_posts(array('post_type' => 'bookgallery' ,'post_title' =>$bookid, 'posts_per_page' => -1
));
?>
<?php
foreach ( $args as $post ) : setup_postdata($post);
if (!empty($post))
{
$allimg=$allrank=array();
while( have_rows('imgs') ): the_row();
$temprank=get_sub_field('rank',$post->ID);
$tempimg=get_sub_field('imglink',$post->ID);
$allimg[]=$tempimg;
$allrank[]=$temprank;
if ($temprank=='1st') {
$rank1img= $tempimg;
$rank1='1st';
}
if ($temprank=='2nd') {
$rank2img= $tempimg;
$rank2='2nd';
}
if ($temprank=='3rd') {
$rank3img=$tempimg;
$rank3='3rd'
}
endwhile;
if (!empty($rank1img)){
?>
<div >
<img src="<?php echo $rank1img; ?>" alt="" >
<div >1st
<select name="rank1">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
</select>
</div>
<div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" ></div>
</div>
<?php }
if (!empty($rank2img)){
?>
<div >2nd
<img src="<?php echo $rank2img; ?>" alt="" >
<div >
<select name="rank2">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
</select>
</div>
<div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" ></div>
</div>
<?php }
if (!empty($rank3img)){
?>
<div>3rd
<img src="<?php echo $rank3img; ?>" alt="" >
<div>
<select name="rank3">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
</select>
</div>
<div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" ></div>
</div>
<?php }
}
endforeach; ?>
</article>
</form>
i put the update_ranks first below that the isset code and below tht the form...now it doesn't even update the database whereas when i had the isset code below the form it used to update the db but the page refresh tht happens automatically didnt show the new content.u had to manually refresh the page to show the new content. how do i get it to load the new content automatically along with updating the db.

You are updating the database on the bottom of the page, so the content is loaded before your update query has been executed. This can be resolved by placing the update query above your html content.
This way the update will execute and after that you will load the new data from the database.
Remember that all code will always execute in the order it is placed in the file, even the rendering of html content.

I made a total newbie mistake. I have two copies of my files. One which my editor changes. The other I need to copy under nginx web directory /var/www/html/. I failed to copy my changes files, so nothing was updated.
I know this is a different root cause than the original question, but I'm afraid, probably a common reason why some people will not see their changes reflected (and hence, end up on stackoverflow).

Related

HTML form POST data not transferring to next page

I have a form with a drop-down box. The user selects an option from the dropdown box and hits submit. On submit, a new page opens up with a new form. The option that was selected is not being transferred to the next page. My form code is:
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname" type="input">
<option></option>
<?php foreach ($data as $row): ?>
<option>
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="heroname">Submit</button>
</div>
</form>
The next page loads and displays some test variables at the top, but the variable from the POST is empty. My code on the page is
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include('header.php');
$username = $_SESSION['username'];
echo $_POST['heroname'];
echo $username;
$test1 = "test 1, before the if statement";
echo $test1;
I get no errors on the page. The username variable and the test1 variable echo normally. The heroname variable doesn't. I need help figuring out why the selection from the form is not transferring to the next page. Thanks in advance.
You have forgotten the value attribute from the <option> elements. Whatever is in the value attribute is what is returned to the server script.
You have a type="input" attribute on the <select> element, that does not belong there.
You have called the button <button type="submit" name="heroname"> which is also what you called the dropdown <select name="heroname"> you cannot use the same name more than once. So change the button name to something else like <button type="submit" name="submithero"> for example
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="submithero">Submit</button>
</div>
</form>
the button does not need a name attribute
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
First, I removed the value attribute from select. Second, I added a value attribute to option. Third, I changed the name for the button to match the IF statement on the next page. The heroname is now being passed to the next page. I have a whole new set of errors now about undefied index, which I will research first, and if needed, post a new question. Thank you all for the help!!

Form disappears on submission Shopping cart

So at the moment I am trying to create a shopping cart using PHP
currently I'm having trouble submitting a form which adds a product to cart. So essentially I have linked up my database to the my shop.php page, so it loops through each item in the DB and displays the product. Each product has a form which uses POST which when submitted, adds the item to the cart SESSION.
So at the moment, when I click the 'add to cart' button the item is added and works fine. But when i click 'add to cart' for the same item again, the entire product gallery stops working. The issue only occurs when I try to add an item that's already been added. I feel like the issue revolves around submitting the same form twice but I am not quite sure.
<div class="container">'
<div class="box">'
<?php
$query = "SELECT * FROM items";
$result_set = mysqli_query($link, $query);
if (mysqli_num_rows($result_set) > 0){
while($row = mysqli_fetch_array($result_set)){
?>
<div class="displayCase"><br><br><br>
<form method="POST" action="shop.php?action=add&id=<?php echo $row['id']; ?>">
<img class="phone1" src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>" />
<h1> <?php echo $row['name'] ?> </h1>
<h2> <?php echo "$" . $row['price'] ?> </h2>
<label id="QTY">QTY </label><input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number"><br />
<select class="phoneType">
<option value="X">Select Device</option>
<option value="X">iPhone X/XS</option>
<option value="X">iPhone X/XS Max</option>
<option value="X">iPhone 11</option>
<option value="X">iPhone 11 Pro</option>
<option value="X">iPhone 11 Pro Max</option>
</select><br />
<input type="hidden" name="name" value="<?php echo $row['name'] ?> ">
<button type="submit" name="add" value="addToCart">Add To Cart</button>
</form>
</div>
<?php
}
}
?>
</div>
</div>
Cheers
EDIT:
Okay so for some reason it seems to take a full reload after the form is submitted for it to work. So I tried this and it seemed to fix it. Still unsure why this is the case, and whether this is a clean way or if there is a better way of doing it
if (isset($_POST['name'])){
header("Loction: redirect.php");
}
then I created a redirect.php page redirects back to shop.php

php if dropdown option value 0 then hide div

as I am not experienced with php, I need some help solving my issue.
I pasted the snippet of php I believe needs editing.
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?></option>
</select>
</div>
<!-- <input type="submit" name="btn-submit" class="button alt" id="leasingAddItem" value="<?php _e('Add Item', 'wp-woo-leasing'); ?>"
data-value="<?php _e('Add Item', 'wp-woo-leasing'); ?>" />-->
</div>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description"></div>
<div id="product-price-details" class="product-price-details"></div>
</div>
My issue is that I want my code to be like:
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?>
If option value = "0" then hide the following div, upon loading site, since default is value is 0.
<div id="product-description" class="product-description"></div>
I hope you understand my question, and I hope that you can guide me to approaching this issue, thanks.
You can use jQuery to show and hide div on basis of select value.
Wordpress have already included jquery file, so you no need to add it.
(https://developer.wordpress.org/reference/functions/wp_enqueue_script/).
Here is code you can use:
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation" onchange="getval();">
<option value="0">Select Option</option>
<option value="1">Select Option1</option>
</select>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description">dgsdgsdgsdgsdgsd</div>
<div id="product-price-details" class="product-price-details"></div>
</div>
</div>
<script>
jQuery(document).ready(function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
jQuery('#sel-product-variation').on('change', function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
else {
jQuery('#product-description').attr('style', 'display:block');
}
});
});
</script>
Let me know incase of any concern for the same.

Magento custom search form

I am trying to create custom search functionality in Magento 1.9. Instead of writing it from scratch, I want to piggy back the default search functionality and add in custom filters. First I copied the form.min template and replaced with drop downs that I want to use.
form.custom.phtml:
<?php
$catalogSearchHelper = $this->helper('catalogsearch');
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
?>
<form id="search_custom_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">
<div class="select-box item">
<label for="finder">GIFT FINDER</label>
<select id="customS" name="finder">
<option value="">Please select</option>
<?php
foreach ($_categories as $key => $value) {
echo("<option value='" . $value["name"] . "'>" . $value["name"] . "</option>");
}
?>
</select>
</div>
<div class="item">
<label for="for">FOR</label>
<select id="for">
<option value="">Please select</option>
<option value="him">HIM</option>
<option value="her">HER</option>
</select>
</div>
<div classs="item">
<label for="delivery">DELIVERY OPTION</label>
<select id="delivery">
<option value="express">EXPRESS DELIVERY</option>
<option value="standard">STANDARD POST</option>
</select>
</div>
<div classs="item">
<label for="price">DELIVERY OPTION</label>
<select id="price">
<option value="<250">LESS THAN £250</option>
<option value=">250">MORE THAN £250</option>
</select>
</div>
<button type="submit" title="<?php echo $this->__('Find') ?>" class="button search-button"><span><span><?php echo $this->__('Find') ?></span></span></button>
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_custom_form', '', '');
//]]>
</script>
</form>
I have included the above template on my home page using {{block type="core/template" name="custom-search" as="custom-search" template="catalogsearch/form.custom.phtml"}}, I have copied the core files into my own template folder and now I find myself stuck.
Can anyone offer a better alternative or solution as to what I should do?
Thanks
Found a much easier option here. I copied the catalogsearch folder to my theme, copied the advanced search form and placed it on my home page. Now I can customise the form and the query as I see fit.

$_POST not working with <select>

I can get the $_POST working perfectly with the inputs but not with the select statements for some reason. Here is my code:
registration.php
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="<?php if (isset($user_type)) { echo 'admin'; } ?>">Faculty</option>
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="submit" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
results.php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Setting the form inputs to variables
$first_name = trim($_POST["first_name"]);
$last_name = trim($_POST["last_name"]);
$email_address = trim($_POST["email_address"]);
if (isset($_POST["submit"])) {
$user_type = $_POST['user_type'];
echo $user_type . '<br><br>';
}
echo $first_name . '<br><br>';
echo $last_name . '<br><br>';
echo $email_address . '<br><br>';
}
ouput:
Alex
Cory
alex#mydomain.com
Any Ideas as to why this $_POST['user_type']; isn't working?
Assuming that what you have posted is the complete contents of registration.php then the issue is that your value for each of the user_name options contains an empty string.
The actual HTML that your script generates is:
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="">Faculty</option>
<option value="">Student</option>
</select>
This is happening because your registration.php script is checking if the PHP variable $user_data is set before adding the values of admin or registeredUser to the <option> tags.
If we expand your code you can see that the if condition will never fire:
<option selected="" value="<?php
if (isset($user_type)) {
// This will never be fired because the PHP variable $user_data is not set.
echo 'admin';
}
// The result is that nothing is echoed so the HTML reads value=""
?>">Faculty</option>
To fix, then either remove the conditional check:
<select id="user_type" name='user_type' class="form-control">
<option selected="selected" value="admin">Faculty</option>
<option value="registeredUser">Student</option>
</select>
Or set $user_data at the start of the script:
<?php $user_data = true; ?>
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
...
But what I think you are really trying to achieve is to determine which of these options to populate as selected in your form. If this is the case, you should check that $user_type is set and if it's value matches the option value, mark that option as selected.
<select id="user_type" name='user_type' class="form-control">
<option <?php
if (isset($user_type) && $user_type === 'admin') {
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser') {
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>
I cant see the submit button in your code, does it have name='submit' on it ?
Additionally if you are using chrome inspect the post in network(developer tools) to see if 'submit' is posted
This happens because both of your options under select id="user_type" have no values when registration.php is executed.
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
If $user_type is not defined the value for both options will be "" (empty string) - and I assume this is what happens.
Here's the Answer
The problem had a little to do with PHP and also a little to do with HTML. PassKit did a great job explaining the PHP which I ended up using in my code to make it work properly. Look to his answer about the PHP and mine about the HTML.
The opening < form > tag needed an "id" attribute that matched the < select > tag's "form" attribute which neither of them had.
Solution:
<form id="user-form" class="form-horizontal" action="results.php" method="post">
right here -^ needed: id="someFormID"
<!-- Faculty or Student? DROP-DOWN -->
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select form="user-form" id="user_type" name='user_type' class="form-control">
right here ---------------------^ needed: form="someFormID"
<option <?php
if (isset($user_type) && $user_type === 'admin')
{
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser')
{
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="singlebutton" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
use submit button in your form or check post with if($_POST)

Categories