Solved

a form submit action and handle in php

Posted on 2011-09-22
11
246 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Nura111
  • 7
  • 4
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36583882
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

0
 

Author Comment

by:Nura111
ID: 36583927
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??
0
 

Author Comment

by:Nura111
ID: 36584079
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?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36587518
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.
0
 

Author Comment

by:Nura111
ID: 36588398
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

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Nura111
ID: 36588460
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!
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36588573
All I can do is recommend that you look for some learning resources for jQuery.  

You have a lot of "moving parts" in this question - the client browser, the form, the CSS, the action script, the javascript, the jquery framework, the php code that talks to the mysql data base, etc.  All of these pieces need to be correct for your application to work right.  It's a lot to learn if you try to do it all at once, but mastery of all of these fields is what it takes to develop web applications.  That's a couple of years of college-level study.

You might want to step back from the technical details and describe the task in non-technical terms -- something like , "I want to enter an email address into a form and send the email automatically."  Then ask us "What are the best practices and design patterns for something like this?"
0
 

Author Comment

by:Nura111
ID: 36588633
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36590408
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.
0
 

Author Comment

by:Nura111
ID: 36590629
Hi ray i already have this database working I have been trying to get the client interface this all time
0
 

Author Closing Comment

by:Nura111
ID: 36911963
Its wasn't really a solution  but a nice suggestion .
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now