Enable save to database when checkbox is clicked using PHP & MySQL - php

First of all, I am using PHP and MySQL in the Joomla Framework (but not interested at this point in the Joomla MVC structure).
I have a list of data from a database (Table A) and next to that I have added a checkbox. When I click on a checkbox, I want the data from the enabled checkbox to be sent (insert a copy of) to a new table (Table B) when I click on the 'Save' button and have that data listed on a new web page. The ones that are unchecked should not be sent to the new table and therefore should be ignored. Also, the 'Save' button must initially be disabled until a checkbox is checked.
Now, there must be a relationship between the existing data in a table (Table A) to the new table (Table B), so for example when the main existing data is deleted from the table (Table A), it should also automatically delete the data in the new table (Table B).
I know some PHP and MySQL but not enough to figure out some of the above.
Can some smart PHP and MySQL guru please point me to the right direction as I'm pulling my hair out, especially with the checkbox issue. Thanks!

To send data to another page and to displaying a message you'll need AJAX.
And for that check this page:
Ajax passing data to php script
But without the message ( to send data to a page ) , use this ->
Let's say that you have:
<form action="insertInDatabase.php" method="post">
<input type="checkbox" name="cbs[]" value="firstValue">First value<br>
<input type="checkbox" name="cbs[]" value="secondValue">Second value
</form>
So here you send your data to "insertInDatabase.php".
There you have this:
//connection do mySql database
if(!empty($_POST['cbs'])) {
foreach($_POST['cbs'] as $cb) {
mysql_query("INSERT INTO table2(checkedCheckBoxValue) VALUES('$cb');");
}}

Related

Updating mysql records from Laravel

I'm just diving into Laravel this week. I've done a few tutorials and have been able to figure out how to setup my database, loop through my records and display them in a view. With each record I am displaying a checkbox and each is set to checked="checked" based on a boolean field from the "CheckedIn column in the database. I want to be able to uncheck multiple records then submit and update that column in the database then refresh the page showing the changed records as unchecked. Just looking for a jumping off point or links to resources that explain this process better. If I could figure out how to do it via javascript and AJAX that'd be even better.
You need to post up more information for people to help you.
Your process would be something like this:
Create the table with a form, with all the checkboxes named with the ID of the row in the DB table
->
Submit the form
->
Get all the inputs for the checked boxes
->
loop through and see whether it is checked or not
->
Update that row in the database
->
Redirect back

Way to re-populate checkboxes on page?

I have a bit of an odd/unique situation, where I am currently looking for a way to 're-populate' checkboxes on my page.
First some background on how the page/app works.
1.) state1: You get simple search form. Enter in first or last or state, etc., hit submit.
It queries a DB and posts back to the SAME Page (?mode=results).
Inside of a MAIN container DIV, I loop through the returned records, and create (echo) a TABLE with a certain tabular layout to parse the values from each record/row (name, date, ID#, etc).
If I have 5 records returned from DB, I loop through and create 5 tables right after each other, populating each with the unique data from the record/row.
In each one of these tables I create, along with the data from the DB record/row. I also create/add in several check boxes.
Using some jQuery and on $(document).ready, I listend for the onClick event for all these checkboxes. So when clicked, I grab some data from the checkbox, as well as some parent info, and do the following:
a.) I create/add another element to the stage
b.) Make an Ajax call to an external .php script, so that I can update the SESSION with my array I have build (collecting all the user checks/input)
I build the array in JS/jQuery and use the Ajax call to get this data into my PHP session (anyone knows a way to NOT use this external .php script to update the SESSION var, let me know please!)
So to re-cap so far: There is no "FORM" (tags), just check boxes in multiple tables. There really is no 1-button (submit) event that POSTS all the checkbox data anywhere at one time. Each time a checkbox is clicked (or un-clicked), I update my SESSION array. That if/when a user IS ready to 'check out' (for lack of better term) (leave the page and go to another page), the SESSION data/array is 'current' and the data is ready to be used on any following pages.
so far everything posted above works just fine.
The question I have is: If the user wants to go BACK to this MAIN PAGE with all the checkboxes, what nice, simple/elegant solutions do I have to re-populate all these checkboxes?
I'm thinking the only approach I have is the check and see if this SESSION array exists when main page loads (meaning it's a re-visit, not the first time), and try to loop through this array and somehow target elements on my stage to find the correct table, then the checkbox ID?
Usually use jQuery to walk the DOM - not PHP?
And what about toggling the checkbox with PHP?
Is there a nice way to do this?
I don't really see your problem. I recapitulate:
You have a page with several <table> coming from your database (1 row = 1 <table>)
In these tables you have checkboxes
When you click on a checkbox, you do an ajax call to save the clicked checkbox in session
Your question is: how can I repopulate checkboxes when the use come again on the page
I think you already have the answer: just use what you stored in your session. When you build your <table>, and your checkboxes, test if they are in session:
<input type="checkbox" <?php if (!empty($_SESSION['blabla'][$myCurrentCheckboxID])) echo 'checked'; ?> />
If you want something more elegant, just create a nice PHP class or list of functions to generate your checkbox.
thanks for the suggestions.. this is what worked for me in the end (so far)
function sessionCheck($recordID, $checkBoxID){
for($r = 0; $r< count($_SESSION['userPicks']); $r++){
if($_SESSION['userPicks'][$r]['recordid'] == $recordID){
for($z=0; $z < count($_SESSION['userPicks'][$r]['itemsordered']); $z++){
if($_SESSION['userPicks'][$r]['itemsordered'][$z]['name'] == $checkBoxID){
return 'checked="checked"';
}else{
return "";
}
}
}else{
return "";
}
}
}
usage for MY project is like this:
<?=sessionCheck($row["id"] ."-A","Testimonial History") ?>

PHP on a site with dynamic content

SO FAR I have made a website which is connected to a MYSQL database. On the website I have many buttons, each button corresponds to a table in my database. When I click the button, a javascript function opens my php file in this form
xmlhttp.open("GET", "addPoint.php?q=" + name, true); Where name is the name of the table
Back to the html. I have a div with an ID that will update with whatever the PHP echos.
The div is now populated with table, reflecting the table in mysql. This all works fine.
Here is the problem: I am trying to implement a voting system, where you vote by clicking on one of the elements of the newly created table. AFAIK the MYSQL updating which will need to happen for the vote to work, will need to be done in PHP, which means that I need to tell the PHP file
The name of the table
The name of the element
I am reluctant to use forms because I would like to just be able to click some text, NOT have to fill in a form or click a radio button.
So how do I get these 2 bits of info to a PHP file? Is the structure which I am doing this any good or should I step back and redesign?
Thanks for reading please let me know if I have been ambiguous.
Familiarize yourself with JQuery. It's great for projects like these.
http://www.w3schools.com/jquery/default.asp
As Norse already alluded to, use javascript to submit a "form" request for you. The javascript can populate the table and element values for you based on the button which was clicked. It is up to you whether this is a GET/POST.
JQuery would be a great way to help make this easier, but you don't need it if you don't want it.

JavaScript form into PHP table

I am creating a website. On one of the pages there is a JavaScript form that asks for the usual information: Name, email etc and then I need to save it into a table using MYSQL and then I want to display the table using PHP on another page. I have no idea how to start saving the data into a table (when Submit is hit) and then displaying the table on another page. Any advice?
http://teamtutorials.com/web-development-tutorials/php-tutorials/inserting-data-into-a-mysql-database-using-php#.T5rIZLNWq9Q

dynamically populating textfields in Flashbuilder

I'm trying to wrap my brain around getting data into my project in a way that I can use. I want to use MySQL & PHP my Flashbuilder app and I'm not populating a datagrid so.
For simplicities sake, in my database table I have 3 columns "ID, Title & Content".
I want to use this to populate the different states in my flashbuilder project.
Normally in a web page I could say in the sql statement SELECT * FROM table WHERE ID = 1
to get the first row of info and I could put my Title and Content where I want them on my page
I can change the query to SELECT * FROM table WHERE ID = 2 to populate page 2 to get it's title and content.
In flashbuilder it all on the same page and I'm not understanding how to populate a singular text field for a title or content area with a single field from the database.
It seems all the examples on the web are basically for datagrids.
Can someone please help me out?
I'll try to help.
You have the database table (ID, Title, Content).
Go ahead and create a web service in PHP that will ping the database and retrieve the data. I don't know much about PHP, so can't help with specifics. If possible, I recommend using something like AMFPHP or ZendAMF to transfer data back and forth between Flex and PHP.
In Flex, you can retrieve the data using RemoteObject, WebService, or HTTPService. You'll get the data back somehow. For the purposes of this sample, I'll assume you're using a form of AMF and have an array of value objects.
Each object contains an ID, title, and content property, representing a single row in your database table.
Now, what do you do with that array? It would be odd to try to display a collection of data in a single text input. Let's pretend you want to create a form to edit the first item:
<Form>
<formItem label="title">
<textInput id="titleInput" text="{resultsFromPHP[0].title}" />
</formitem>
<formItem label="content">
<textInput id="contentInput" text="{resultsFromPHP[0].content}" />
</formitem>
<formItem >
<button label="Process Input" click="processInput()" />
</formitem>
</form>
So, you have a form that allows for inputs, and has a button. Just write the click handler:
protected function processInput():void{
resultsFromPHP[0].content = contentInput.text
resultsFromPHP[0].title = titleInput.text
// call other PHP Service to update data
}
Does that help?
This was psuedo code I wrote in the browser and will most likely need tweaking to compile.

Categories