Jump to content

Form Handling...


cmazur

Recommended Posts

Well let's start out simple. I'm new to php.
My problem is that I have a form that has multiple buttons. The form is used to add, delete, edit, and open selected items in a list. Here's where the problem is, I don't want to leave the current page to execute my php scripts.

For example, if I want to add a link to my list, I should just type in the link and click the add button. Thus, I've written a simple php script to handle the task. But I don't know how to use this php script from the button click.

Well, I hope i elaborated enough, here is the form's code:

[code]

print("<form id='lists' name='lists' method='post' action=' '>");
print("<p><h2>Links</h2>
<select name='links' size='5'>");
fill_links($id); //Fill the links list with the existing links in the database
print("</select><br />
<input name='open_link', type='button' id='open_link' value='Open' />
<input name='edit_link', type='button' id='edit_link' value='Edit' />
<input name='delete_link', type='button' id='delete_link' value='Delete' />
<br />
<input name='add_link', type='button' id='add_link' value='Add' />
<input name='add_link_text', type='textbox' id='add_link_text' size='35' />
</p></form>");
[/code]

And here is the php script's code to add the link to my database
[code]

// Function: add_link()
// Parameters: user id, link
// Returns:            none
// Description: adds a new link to the user's list
function add_link($id, $link)
{
//Connect to the database
connect_db();

//Get the pics from the database
$query = "INSERT INTO tbl_links (user_id, link) VALUES ($id, '$link')";

//Reload the links
fill_links($id);
}
[/code]



Should I be using the onclick="somefunction()" property to access this php function?

Any help or insight would be greatly appreciated...
Thanks!
Link to comment
Share on other sites

You are going to have to have the page reload to use this script.  PHP is a server side scripting language and as such the script does not run from on the users machine without reloading the script from the server.

This can all be done on the same page and you just need to have a script that checks to see if the page has been reloaded and if so with what request, then process the request before displaying the page back on the users machine again.

If you need some more help doing this, let me know.
Link to comment
Share on other sites

OK, with a basic single page form script you have your submit button which has been given the name="submit".  Then when you submit the form, it sends a the variable with the name submit in addition to the other form variables.

Doing this, when you load a php script, if you check for the presence of a $_POST or $_GET variabled (depending on method used) with the name submit, you know id the form has been submitted.  You can then decide whether to display the form or process the information submitted.

For example:

[code]
<?php

// check to see if form submitted

if (isset($_POST['submit'])) {

// the variable exists so we would have code here to process submitted details

} else {

// the variable does not exist so the form has not been submitted so we put code here to display form

}

?>
[/code]

Are you with me this far?
Link to comment
Share on other sites

Kind of,

Can action property be set to itself (my initial page is a php script with imbedded XHTML)?
Then each time the page loads, I can see if data has been submitted and do what I need to do before i display the page?

but to answer your question, I'm with you thus far.
Thanks again for the help...
Link to comment
Share on other sites

Yeah sorry, forgot to mention that you would set the form action to the same page, you can do this by echoing $_SERVER['PHP_SELF'] which will give you the url of the current page.

Building on the basic example I have given you.  Amend your form so that you have submit buttons for each of the options open, edit, delete and add.  Give each of these submit buttons their own name equal to their role, ie the add button will have name="add", the delete button will have name="delete" etc.

You can then on loading the page, check for the existance of each of these variables in turn to see if the user had submitted the form already using any of the buttons.  Dependent on which submit button has been used, if any, you can then process the values submitted in the form accordingly.
Link to comment
Share on other sites

ok, i think i got ya for the most part...

just one more quick question while this thread is still goin':

since my users have to login using a username and password, how could i save a variable that says that a user is logged in?
would a cookie be goood enough? or is there an alternative method that would work better/be easier?

thanks again for all of the help...
p.s. i just love assignments when profs leave ya hangin' and don't respond to any questions sent via email...
Link to comment
Share on other sites

No problem, glad to be able to help. I am only relatively new to a lot of this myself and have gained a lot of help here so it is gret to be able to help others ;D

The best way to store information like logged in user details is in session variables.  You will need to put session_start(); at the beginning of any pages and it will then resume any session you have open.  You can then save information using session variables, ie $_SESSION['variable_name'] = value;

To get a full understanding of how sessions work just google for a php session tutorial.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.