Link to home
Start Free TrialLog in
Avatar of Nura111
Nura111

asked on

a form submit action and handle in php

I want to do the following:
when submitting a form :
1. to update something in the db.
2. to execute a script (lets call him action.php) with a GET method

I attached the form code but I think its doesn't really matter (its currently in post method)
Thank you!
<form id = "popup_form_{$row['id']}" method ="post" style="background-color:white; border:2px solid blue;padding:4px; width:250px;  position:absolute">

			<input type="hidden" name="id" value ="{$row['id']}">

			<input type="hidden" name ="email" value ="{$row['email_address']}">

			<input type="submit" value="SpamIt" name="spamIt" style="margin-right:15px"

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

See line 20 of this script.  That is the action script, which is the part that would update something in the db.

I do not understand this part: 2. to execute a script (lets call him action.php) with a GET method.  There are several ways to do that.  You might load the script into your action script with include().  Or you might execute it with file_get-contents().  Or you might just put a link on the screen and let the client click the link to make the GET method request.

<?php // RAY_post_example.php
error_reporting(E_ALL);
echo "<pre>"; // MAKE IT EASY TO READ



// THIS ILLUSTRATES THE RELATIONSHIP BETWEEN THE 'ACTION' AND THE 'FORM' WHEN COMBINED INTO A SINGLE PHP SCRIPT FILE



// SET A DEFAULT VALUES FOR A VARIABLE TO PREPOPULATE THE FORM - THIS COULD COME FROM A DATA BASE, OR MIGHT BE EMPTY
$my_INPUT_Field = 'ORIGINAL DATA';



// HAS ANYTHING BEEN POSTED? IF SO, $_POST IS SET AND CONTAINS THE DATA
if (!empty($_POST))
{
    // THIS IS THE ACTION SCRIPT.  HERE WE SHOW THE POST ARRAY
    var_dump($_POST);

    // IF THE FORM WAS FILLED IN, COPY THE INPUT INTO OUR VARIABLE
    if (!empty($_POST["my_INPUT_Field"]))
    {
        $my_INPUT_Field = $_POST["my_INPUT_Field"];
    }

    // THIS IS THE END OF THE ACTION SCRIPT
    echo 'ACTION COMPLETED';
}



// THIS IS THE FORM SCRIPT CREATED IN HEREDOC NOTATION
$form = <<<ENDFORM
<form method="post">
TYPE SOMETHING HERE:
<input type="text"   name="my_INPUT_Field"   value="$my_INPUT_Field" />
<input type="hidden" name="my_HIDDEN_Field"  value="Not Really Much of a Secret" />
<input type="submit" name="my_SUBMIT_Button" value="go" />
</form>
ENDFORM;

// WRITE THE FORM TO THE BROWSER
echo $form;

Open in new window

Avatar of Nura111
Nura111

ASKER

HOw to solve number one for me this one I know I already check
if isset($_POST['spamIt']{
update the db

}

my problem is number two: I need when the user click the submit form that two things happened 1. to update the db. (I did that like I mentioned above) but than I had a problem.( because what im working on is already a part of different script and logics I didn't design so I have to "fit" my solution into the current logic of the application)
my problem is: I want after the submit button will be pressed by the user to update only part of the page this part can be generate by calling action.php with a $GET parameter..

is that make sense??
Avatar of Nura111

ASKER

If I was to use the onclick in the submit button of the form, when the user will click the submit it also will send http request with the post /get method of the form and perform the on click ?

what exactly will happen?
It is a violation of the HTTP protocol if you allow a GET method request to update anything.  GET is only for retrieving data, not updating it.  POST is for updating.  This restriction has some deep background thought behind it.  Consider what would happen when a search engine spidered your page, found a link, and followed the link.  If the link could cause an update, a search engine could update your data model.  Probably not what you want.

I think I would design the application so that the action script handles both parts of the request.  It is possible for a POST-method request to use FsockOpen() or CURL to re-post the data to another script, but the code for that is complicated and difficult to debug.

To give any further help we would need to have some test data and see what code exists now.  Please post that for us, thanks.
Avatar of Nura111

ASKER

I don't believe that is what I meant: when you say  if you allow a GET method request to update anything.
what do you exctly mean by update?  if I have a text on the page and by clicking the text its going to a javascripot function that send it to a script.php
and the script are going to $GET[parameter} to get the parameters and than echo to part of the page inof thats wrong?


function toggleGraph(id,ajaxUrl) {
                    var fieldName = "d_" + id + "_graph"; // prefix with d_ to avoid errors when domain_id begins with a number.
                    var element = $('#' + fieldName + ' #rank_graph');
                    if (element.is(':hidden')) {
                            element.load(ajaxUrl);
                    }
                    element.toggle();
            }

Open in new window

Avatar of Nura111

ASKER

I already attached the test data: its the form And I need to change it so when the user will submit the form  .
its will update the db and than after  will load into the form element area in the page the action.php output

when action.php call he retritriev info form $GET[para1] and Get[para2]

this varaible exist in the form.

but I dont know how to do that..

I thought of using onsumbit() in the form but I still need help with that.

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nura111

ASKER

I  Have a web application :  currently Im working to make a change in the next page interface :

url:sem_page.php?page=leads&sub=manage

(that was generate from sem_page.php and in sem_page.php there is a table of name rows when clicking  each name rows its showing a list of emails address and and a grafh (by clicking the row its firing toogleGrafh which I attached the code above and explained above)

what I want to do is add the spam form because I want the user to mark email as spam  when submitting the form I want the email will be deleted from the list so in order for that to happen im marking this email record as spam in the db and I need to somehow get again the result from action.php (which will echo the new emails list without the record that marked as spam and show it on the page without reloading seo_page again
OK, I think the design goes something like this...

1. Check $_POST to see if there is a message number marked for deletion.
2. Delete messages by using an UPDATE query to set the "is_spam" column to "Y"
3. Read the data base with a SELECT query that has WHERE is_spam <> "Y"
4. Present each of the messages with a form beside the message.  The form makes a post-method request to the same script.  In the request you would have the form data as shown in the original question.

About this part: "show it on the page without reloading seo_page again" -- I would forget about that for now.  Just get the script and data base working.  You can add the client interface later.
Avatar of Nura111

ASKER

Hi ray i already have this database working I have been trying to get the client interface this all time
Avatar of Nura111

ASKER

Its wasn't really a solution  but a nice suggestion .