Submit button -> run .php -> popup -> refresh part of page php - php

On my page, I currently have a select form and attached submit button, which is generated dynamically based on the entries in my database. Here is my outputted HTML
<div id="structures">
<h1>Build</h1>
<form name="buildForm" id="buildForm" method="POST" onsubmit="buildForm();return false;">
<select name="buildID" class="buildClass">
<option value="0" selected="selected" data-skip="1">Build a Structure</option>
<option name='Town Center' value='1' data-icon='../img/structures/tc.png' data-html-text='Town Center<i>
500 minutes<br>50000 gold</i>'>Town Center</option>
<option name='Barracks' value='2' data-icon='../img/structures/barracks.png' data-html-text='Barracks<i>
25 minutes<br>1500 gold</i>'>Barracks</option>
<option name='Dragon Roost' value='3' data-icon='../img/structures/droost.png' data-html-text='Dragon Roost<i>
200 minutes<br>5000 gold</i>'>Dragon Roost</option>
<option name='Mage Hall' value='4' data-icon='../img/structures/mage.png' data-html-text='Mage Hall<i>
40 minutes<br>300 gold</i>'>Mage Hall</option>
<option name='Test Lab' value='6' data-icon='../img/structures/testlab.png' data-html-text='Test Lab<i>
1 minutes<br>10 gold</i>'>Test Lab</option>
</select>
<div id="buildSubmit">
<input class="button" type="submit" value="Submit"/>
</div>
</form>
</div>
While I am finally improving with PHP/mySQL and html/css, javascript still throws me for a loop.
When the user clicks "submit," I want a popup menu to appear, and I want to update the database based on the contents of a separate php file. Afterwards, once they close out of the pop up, I want to refresh a specific area of the page. I am using codeigniter, so I load my models functions through that to populate the page.
I would love to put more code, though honestly, its such a mess and completely worthless that instead I thought I'd ask more for a healthy guideline as to what I would have to do to implement something like this.
Examples would be amazing, but I am just trying to wrap my head around what I actually need to do to make this work.

Here's the steps I think you're trying to accomplish alongside info to perform those steps. Please clarify if I'm not understanding correctly...
After clicking the submit button...
Execute a php file which alters the DB
To do this, since you're using jQuery, you could make an ajax call to your PHP file with appropriate params that are needed for the DB update.
For an example of jQuery, PHP and mysql together, see Using JQuery AJAX and php to fetch data from a mysql database. Also see the jQuery documentation on it: jQuery.ajax
Popup window to confirm the change (using JavaScript)
var answer = confirm("Do you want to change this?")
if (answer){
// Possibly call another PHP ajax script to commit database changes...
}
else{
// Rollback database changes and do some other processing...
}
To reload the page (using JavaScript)
window.location.reload();

Related

Make form URL submission look like route instead of query string

Is there a way to change default URL formation on form submission?
Here is what I have:
<form action="<?php echo base_url('some_controller/some_method'); ?>" method="GET">
<select name="foo">
<option value="1">bar</option>
<option value="2">Wubba lubba dub dub</option>
</select>
<button type="submit">Send</button>
</form>
When I click the button, the url becomes: (not wanted)
localhost/some_controller/some_method?foo=2
Is there a way to make it like the following? (expected result)
localhost/some_controller/some_method/2
Is it achievable with PHP / Codeigniter only? Or maybe in HTML?
(Can it be done without javascript and without htaccess?)
I don't think using the form_open method makes any difference.
This happens because your method is GET. If you change your method to POST the info will disappear out of the URL. However, it will have different side-effects which are either good or bad depending on your viewpoint:
Post will put it in scope for CSRF protection, if that is turned on in codeigniter.
You will get a "Do you want to resubmit?"-type of message from your browser if navigating back to a page that was the result of a POST submit.
The only other alternative is to do an ajax request in the background via javascript.

Update MySQL table from Dropdown and Form

I'm posting this question up here because i'm a bit lost. This is my first time working on PHP and MySQL and I'm doing this for my uni assignment. I've managed to do all the CSS, Javascript and part of the PHP/MySQL requirements. The only thing that I have left is:
How do I get the value from my form that is generated through PHP and MYSQLquery and update the respective row of my MySQL table. I've attached an image to make it easier to understand.
The left side allows a user to search or display all orders. The PHP within will then call itself to update the table on the right. That part I managed to do it. But now part of my assignment requirement is:
The Manager should be able to ‘update’ the status of an order from a link
or button next to the order in the table, changing the status from
(pending | fulfilled | paid | archived). This could be done by returning
the record to a form in this webpage, to change and ‘confirm’ the change.
Now the table generated is actually a form. I've put some snippets of the codes here
echo "<form method=\"post\" id=\"regForm\" action=\"https://formtest.php\" name=\"editAll\">\n";
.......
echo "<td>", $row["order_status"],"</td>\n";
echo "<td><select name=\"change_orderstatus[]\" id=\"change_orderstatus[]\">
<option value=\"\" selected=\"selected\" >Please Select</option>
<option value=\"pending\">Pending</option>
<option value=\"Fulfilled\">Fulfilled</option>
<option value=\"Paid\">Paid</option>
<option value=\"Archived\">Archived</option>
<option value=\"Delete\">Delete</option>
</select></td>\n";
....
echo "<input type= \"submit\" value=\"Save changes\" id=\"commit\" name=\"editAllquery\" />";
If I put change_orderstatus[] it supposedly becomes an array but how do I make use of this to update my MySQL Table
I'm attaching another picture to show the bottom where I have the save changes button
Now I know that Stackoverflow is known for bashing people instead of helping because "all the noobs should learn their stuff and all" (this is from my personal experience) but I'm not asking you to write the codes for me or help me do my assignment.
I'm just asking you to point me to a direction where I can then do it on my own. That's all. I've tried to make my post as clear and as detailed as possible so that I don't get immediately down voted to oblivion.
If you could help me out, that would be great.
Thank you
First you'll need to output a form somewhere, I suggest making it like:
<form action="phphandler.php" id="form1" method="POST" style="display: none;">
<input type="hidden" name="id" id="inp1">
<input type="hidden" name="status" id="inp2">
</form>
Make onChange event for select :
<select onchange="event.preventDefault(); myFunction(1)" id="sel1">
myFunction(1) should be generated for each select with its unique id using php
Then function like:
function myFunction(id) {
document.getElementById('inp1').value = id;
document.getElementById('inp2').value = document.getElementById('sel1').value;
document.getElementById('form1').submit()
}
All ID's are generated by php except for inp1 and inp2 and form1

How do I Apply a working Dropdown instead of a Text field in php

I am working on customizing a plugin, however my knowledge is currently lacking on the php side.
currently i have an Ad plugin in which you can sell ads per day, however my predicament is that i want to sell the ads only in packs of 30,60 & 90 days.
In this plugin upon typing a number of days this plugin calculates the amount and gives a result right under without anything pressed, so it is actively reading an input; however the only problem with this plugin is that it allows anyone to type a random number in there.
now the only real solution that i could think of was to make a drop-down with pre-existing values 30, 60, 90
My attempts thus far have been futile as i keep applying HTML <Select> (yes i had changed the wording from <input to <select and then added <option value="value">value wording</option>)techniques and it seems that anytime that i attempt to put a number for the value as one of the choices i get nothing but an empty drop-down, this then made sense to me since this is a PHP form and not an HTML form.
I was able to find where to code the changes, as i was able to find where the field is pulling from; here is the code:
(How should i proceed to make the changes that i want happen?)
<div class="control-group" id="total_days" style="<?php echo $date_dis;?>">
<label class="ad-price-lable control-label" width="40%"><div id="total_days_label"><?php JText::_("TOTAL_DAYS_FOR_RENEWAL");?>:</div> </label>
<div class="controls">
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
<input type="hidden" name="ad_totaldays" id="ad_totaldays" value="<?php echo $ad_totaldays;?>" />
</div>
</div>
now i am also assuming that out of this area, the main thing to be changed would be:
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
of course, i could be wrong, but i really would appreciate help with this!
thank you very much!
Here is how I've always done dropdowns
<select form="examplef">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<form id="examplef">
<input type="submit">
</form>
The php you're looking at is setting a value field for the inputs. This just means that something will be in the box already when you load the page.
Also keep in mind that just because you have predefined options doesn't mean you wont get unexpected input; Changing the options is as easy as right clicking
PHP form vs HTML form
They are the same. The html <form> tag is a way to communicate with php. This tag will (most likely) have an attribute called "action" and "method". The action attribute specifies what to do with the collected information. You can think of it almost as an <a href> that includes data.
The method attribute is a little more complicated. It tells your browser how to send the data entered into the form. The two options are "GET" and "POST". the GET method is data put in the url. I'm sure you've seen youtube links with the ?id=. This is just a GET variable included in the URL. The POST method is for secure data, or data that shouldnt be in the URL. If you would like to read more, W3Schools has a great document here or from Php here and here

How to run php when an option is selected from a select list?

I would like to have a select list that runs a php database query when an option is selected. I have the code:
<select>
<option value="available">Available</option>
<option value="sold">Sold</option>
</select>
<input type="submit" name="change status">
Say, when a user selects 'sold' I would like to run:
<?php
db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'");
?>
I've tried
<option value="sold" <?php db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'"); ?> >Sold</option>
but doesn't seem to work.
I don't know if I should be using
<form method="GET">...</form>
I know I can use ajax somehow but I'm really not familiar with it.
Any ideas how to do this?
Thanks
You have to use javascript! Or else you must update your database after form submit.
A possible way with the javascript library jquery would be:
$("select").change(function() {
$.post("/your/url", {option: $(this).val()}, function(return) {
//echo result if necessary
}
});
On server side you should check for $_POST['option'] and update your database after you
ESCAPED
the contents of the variable "option".
You have to send an AJAX call with jquery or plain JavaScript when the combobox got changed.
Depending on the new value, you can send various parameter and call your PHP scripts.
But the interaction between HTML and PHP like you tried will never work. The time you see the combo box in your browser, your PHP commands were already executed.
My site is build on drupal so I created a custom module and used hook_form_alter so I could just use PHP and didn't need to code in ajax.
Well, you will have to create two files one with the form and a second one with the action itself, the first will have something like this.
<form method="POST" action="ActionFile.php">
<input type="text" name="name" id="name" value="">
<input type="submit" value="find name">
</form>
and create a file called ActionFile.php that will contain the variables and the connection to the DB and the select.

Passing data back to the current page as POST using jquery auto submit on a second page

I have a page which displays data for a given year.
I would like to ask the user a simple question like "what year would you like to see" via a dropdown menu.
The data set (called from a database) is defined by the single variable $year.
My current solution is to as the user the question within a form:
<form id="year" method="post" action="processing.php">
<select name="year" >
<option value="2011">2011</option>
<option value="2012" selected>2012</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="View Year" />
</form>
The page processing.php has a single hidden field populated by the POST data $_POST['year'] and automatically submits this back to the original page, which in turn defines the variable $year via the newly received POST data from processing.php
Code on processing.php page:
$year=$_POST['year'];
echo '
<div class="hide" >
<form id="year" method="post" action="original_page.php">
<input name="year" value="'.$year.'" />
<input type="submit" onload="submitForm()" />
</form>
</div>
<script>
function submitForm()
{
document.year.submit();
}
</script>
';
Question 1:
Is this a sensible way to achieve this, is there something more elegant I could do?
Question 2:
It does not work - the secon page does not forward me back to the first page on load, what have I don wrong in my code?
Thank you!
The elegant method I think you are talking about is called AJAX. It will allow you to retrieve the data from the server without the need to switch pages at all.
See this : Your code implementing AJAX via jQuery $.get
Of course, now your processing.php script will need to retrieve the data you want to display and return it in HTML format so that the $.get callback can drop it into the retrieved_contents div.

Categories