Jump to content
antpre

Changing the way save() function reloads the page ?

Recommended Posts

Hy there,

I am fairly new to PW and php so please bare with me.

I am having a form to do some front end editing (editing some fields of a selected user). I pass  the user's id with a url segment to the page where the form is. it is working fine. But when  the submit button is clicked the fields are processed and the page is saved  ( $mypage->save() ) at this point the page reloads with its normal url (ie without the url segment passing the user's id). As a result when the page is reloaded there is an error because it cannot find the url segment.

from what I understand it is the save() function that reloads the page.

Is there a way to alter the way the save function reloads the page. Or simply a way to prevent the save function to reload the page or just an other way to do what I am doing...

Thanks in advance

Share this post


Link to post
Share on other sites

The save won't reload your page, it is the POST who 'reloads'.

Big side note:

When I read your post well it 'sounds' very dangerous what you are doing right now.  (not that is has to be)

Would you mind to post your code so we can get a better understanding...

Share this post


Link to post
Share on other sites
Thanks for answering my post.
My use case is a very simple elearning site where users with role 'teacher' manage a group of users with role 'student' and give them access to diferent documents (learning material).
On the teachers home page. I list all the student belonging to the loged in teacher.
For each student I output a link to go to a page where there is a form to edit the user profile of that student. in this link i pass as a url argument the id of the given student :
 
    <a href= "<?php echo $pages->get(1049)->url.$stid;?>" > modifier les documents </a>  
 
where $stid is the student user id.
 
On the page with the form I get the id from the url segment with the $input variable. the rest of the code is a snipet I got from an other post on the forum. On this form Only a few fields from user template are displayed not all of them. 
With the different tests I did I got the idea that it was the save function that was reloading the page.
As far as security is concerned, I thaught the input->urlsegment(1) was doing some sanitasation of the variable.
I didn't do it now but my plan was also to include an if statment so that this page is only accessible by user with role 'teacher' and other if statment to make sure the urlsegment is a proper user id and deny acces to anything else.
Here is the code.
 
<?php
$asm = $modules->get("InputfieldAsmSelect");
// Get the user you need to edit from the url argument
$userId = $input->urlSegment(1);
$mypage = $pages->get($userId);

// Populate with the names of the fields you want to exclude OR include (see instructions below)
// Leave empty to output all the fields
$myfields = array('ref_package','information');
$form = $modules->get('InputfieldForm');
$fields = $mypage->getInputfields();

// If array is not empty use it to filter the fields
if ($myfields){
	foreach($fields as $f){
		// Output all the fields minus the ones listed in the $myfields array
		// Instead, to output only the fields that are in the array, remove the (!) from the condition
		if (in_array($f->name, $myfields)){
			$form->append($f);
		}
	}
}

// Else, include all the fields
else {
	$form->append($fields);
}

// Add save button
$field = $modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_save');
$field->attr('value', 'Save');
$field->label = "submit herei";
$form->append($field);

// Process the form
if($input->post->submit_save) {
	$form->processInput($input->post);
	if(!$form->getErrors()) {
		$mypage->of(false); 
	// turn off output formatting before setting values
	foreach($mypage->fields as $f) {
			if (in_array($f->name, $myfields)){
			$mypage->set($f->name,$form->get($f->name)->value);
		}
		}
			$mypage->save();			
		}
}

// Render the form
echo $form->render();
?>

As stated earlier I am fairly new to php and PW (coming from drupal but never dared to code in this complex environement :-)

Thanks

Share this post


Link to post
Share on other sites

Without commenting the actual implementation decisions, the reason for this behavior is that you haven't defined an action for the form. In such case, InputfieldForm will default the action to './', which then causes the described behavior.

As Martijn already commented, this has nothing to do with save(), but only with the actual form that is rendered (and eventually posted). In a way, it doesn't have anything to do with PHP either. You would have probably noticed the problem, if you had viewed the HTML of the page.

Anyway, to fix the issue, you can do this for an example

$form->attr('action', $_SERVER['REQUEST_URI']);

Share this post


Link to post
Share on other sites

Hy,

Thanks a lot to take time to answer my question.

Didn't have time to test it. But took time to try to understand better (had a look at the inputfieldForm module code and read about the $_server variable).

Lots to learn for me as you can see.
Thanks again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...