Solved

how to go back to the same page "status" when using forms in php in just part of the page?

Posted on 2011-09-22
37
261 Views
Last Modified: 2012-05-12

HI all an issue im having:

I have a pop up form that is reside in generate.php
the form is "called from the main application page main.php
(generate.php is actully eching the result inot the page (between others also the pop up form)

the handel of the sumbit action is in the main.php
if (isset($_POST['spamIt'])){
update db
other
}

my problem: after sumbitiing the form the the web page that will show is main.php part I want it to sho the same page that was before sumbitiing the form.
any ideas?


I think in general I have a problem to understand the server loading of the diffrent page and what is happening when sumbitiing form using javascript/ajax and etc
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">

			<input type="button" value="Close" onclick="closePopup()"/>

Open in new window

0
Comment
Question by:Nura111
  • 18
  • 14
  • 3
  • +1
37 Comments
 
LVL 9

Expert Comment

by:user_n
Comment Utility
When a form is submitted it is sent an GET or POST HTTP request to the server. In form "action" parameter shows what script will processed the request.

 http://www.tizag.com/phpT/postget.php
http://www.w3schools.com/php/php_post.asp
0
 

Author Comment

by:Nura111
Comment Utility
yes you can see in the form code its using method post and main.php hande that but how to I "save" the same screenshot  of the page (that was showing info also from generate.php)  and after handeling the form to show the same page as before
0
 
LVL 3

Expert Comment

by:dkellner
Comment Utility
http://dkellner.hu/example018.php

The trick is to submit into a hidden IFRAME - your server side script will get all the things submitted but no visible changes will happen on the user side.

 
<?php

if(!$_POST) {
	?>
	<iframe name="scon" style="display:none"></iframe>
	<form target="scon" method="post">

		Whatever 1: <input type="text" name="whatever1" value="Hello"><br>
		Whatever 2: <input type="text" name="whatever2" value="World"><br>
		<br>

		<input type="submit">


	</form>
	<?
	exit;
}


?>
<script type='text/javascript'>

	var G=<?=json_encode($_POST)?>;

	alert(
		"1: "+G.whatever1+"\n"+
		"2: "+G.whatever2+"\n"+
		"We can save them now, without disturbing the form itself!"
	);


</script>

Open in new window

0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
you can add <input type="hidden" element and to place in it the url from which the request was made
http://www.w3schools.com/tags/att_input_type.asp
0
 

Author Comment

by:Nura111
Comment Utility
user_n:   is there any attribute for that I can add?
for exmp: like this but for the page im in right now?
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
You can add something like

<INPUT TYPE=hidden URL="temp.php">
temp.com to be the page that you want to go back
than in the page where you should process the form you can use something like
include 'temp.php';
0
 

Author Comment

by:Nura111
Comment Utility
dkellner:: im sorry I dont get you exmples and how to implement it in my code.

0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
You have something like this in your code already

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

Author Comment

by:Nura111
Comment Utility
user_n: I understand what I need to add to the form I dont understand how to move to this url form the next part

(isset($_POST['spamIt'])){
update db
other
}
0
 

Author Comment

by:Nura111
Comment Utility
doesnt I need to use javascript or ajax for that to change just part of the page?
0
 
LVL 9

Accepted Solution

by:
user_n earned 250 total points
Comment Utility
You can submit the form to the server. There you can change the page and return the result of the changes to the user. Part of the page can be changed with javascrip. It is up to you to decide what exactly is your goal
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
0
 

Author Comment

by:Nura111
Comment Utility
" There you can change the page and return the result of the changes to the user"

how can I return a diffrent page thatn the page the form is process in in my code the form is processes in main.php ans I want to return generate.php
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
http://www.w3schools.com/TAGS/tag_form.asp
action       URL       Specifies where to send the form-data when a form is submitted       STF

in this url will be your php page that will process the changes

http://www.tizag.com/phpT/examples/formex.php
0
 

Author Comment

by:Nura111
Comment Utility
I will try to explain it again:
 I want the action to be main.php  I dont need to write that the code I attched to the question worked because its techincally caliing from main.php that was loading genreate.php that echo the from on part of the page)

but I want after sumbitiing the form to do something in the (isset($_POST['spamIt'])){
update db
other
}  
that will make the changes in the page but show the part of the page that wasnt change the same.
and not to reload whoole of main.php again

is it it possible? or I need to do it in a diffrent way?
0
 

Author Comment

by:Nura111
Comment Utility
how can I use your former suggestion:
You can add something like

<INPUT TYPE=hidden URL="temp.php">
temp.com to be the page that you want to go back
than in the page where you should process the form you can use something like
include 'temp.php';

How can I redirect it to the temp.php in the code?
0
 
LVL 3

Expert Comment

by:dkellner
Comment Utility
Nura111: my example is about submitting a form without going to another page.
What I understood from your original question is you need a way to do a POST but stay on the page while you do.

Is this the question, or have I completely misunderstood it?
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
I don't understand exactly what you want.

there are two strategies to change a site
1. To do it on the serverside - like php, asp, java server pages
this  is done with forms. When a form is submited the data in input fields through post or get method are sent to the server to the page described in action parameter of the form. In this page through get or post you can get data and change the site you want to send to the user. http://www.tizag.com/phpT/postget.php The site that is sent to the user is that described in action parameter. You can use include redirect php functions to include another php site.
2. To do it on the local machine - i only know javascript for this. W3school is a good place to learn javascript. In internet there are many example

Ajax - I don't know nothing about it
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Nura111
Comment Utility
user_n:
1. if no action is mentioned ? which script will get back the data from the server?
"You can use include redirect php functions to include another php site."- how do I do it in my exmple ? I dont understand it"


0
 

Author Comment

by:Nura111
Comment Utility
dkellner: I dont know that what im trying to understand I need to sumbit  a form and show the same state that the page was before sumbitting the form.
thats what im asking how to do it
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
I don't know what is happening when there is no action
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
0
 

Author Comment

by:Nura111
Comment Utility
"You can use include redirect php functions to include another php site."- how do I do it in my exmple ? I dont understand it"
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
0
 

Author Comment

by:Nura111
Comment Utility
No how do I psychically redirect it in  my exmple by using <INPUT TYPE=hidden URL="temp.php">??
what will cause it to go in temp.php if it not in the action og the form?
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
Try this
You will have in your let say current.html
<form action="process.php">

in process.php you can use

<?php include("current.php"); ?>
0
 
LVL 9

Expert Comment

by:user_n
Comment Utility
I want to said current.php not current.html
0
 

Author Comment

by:Nura111
Comment Utility
I think we are taking on diffrent things.
if ill write inside of the code that is in main.php

if isset ($_POST['spamIt'])
{
include ("current.php");
}

so when sumbitiing the form it will process it in main.php and when it will get the include it will open current.php in the browzer??

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
This illustrates the relationship between script initialization, the action script and the form script when all three parts of the code are contained in the same script file.

From this question and some of your other questions, I get the sense that you might really benefit from a book that can show you how PHP and MySQL and HTML work together.  The SitePoint book is pretty good.  If you invest a month in the process of reading and working through the examples, you will be way ahead of the game, I promise!  It teaches many of the things you're wrestling with now, and it has great explanations and examples.
http://www.sitepoint.com/books/phpmysql4/

Good luck with your project, ~Ray
<?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
Comment Utility
I wrote it as a new question because it hink there was a misunderstood here I think the question is diffretn:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27323168.html
0
 

Author Comment

by:Nura111
Comment Utility
Oh Hi RAY didnt see your response with the book is it also include the jquery and ajax issues I think my main problem now is that I dont fully understand the way a website/webapplication is commincate with the server and the diffrent between server side and client side scripting and  would this book help?

And I still kind of need to solve my current problem but I think I simplize it in the new question..
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You have a lot of moving parts when you have a simple client-server application, like a web browser that talks to a server script by submitting a form.  When you add jQuery to the mix, it gets more complicated.

Here is how I usually try to build a web application.  First get the PHP script right.  Then add the HTML forms.  Then add the CSS to style the appearance of the HTML.  Then add the JavaScript or jQuery for the client-side presentation layer.  Others may see it differently, but that order of development works for me.

The problem that you and everyone else has when you start trying to build a site comes down to this.  There are complex interactions between PHP, MySQL, HTML, CSS, HTTP and JS.  You have to know all the technologies, and there are not any books that teach them all.  There are libraries and colleges, but they will teach you from several different books and courses.
0
 

Author Comment

by:Nura111
Comment Utility
Yes you are right and believe it or not with my stupis question.. I have a computer science degree :)
but I wasn't dealing with those kind of technologies there ..

so Im trying to understand what are the foundation I Should learn first if you have recomandation for a specific certification or college class silibous about the area we have been recently discussed I will be happy to hear that.
and I will check the book you offered as well
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
If you have some background in computer science, the SitePoint book will serve you well.  It shows good examples and has good explanations of why we do certain things certain ways.

Almost any college level course in these topics will be helpful to you: PHP, MySQL, HTML, CSS, HTTP and JS.
0
 

Author Comment

by:Nura111
Comment Utility
ok thank you
0
 

Author Closing Comment

by:Nura111
Comment Utility
all the comments help me but it wasnt really the soloution which is using jquery ti sumbit the form which I understoof later from diffrent questions I posted in EE
Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php mail headers 2 8
Mobile menu navigation , its html and css 6 35
Paging Using PHP 7 31
Printing a Google Form 2 0
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Read about why website design really matters in today's demanding market.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to dynamically set the form action using jQuery.

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

15 Experts available now in Live!

Get 1:1 Help Now