Jump to content

On form submit, check if field values exist


louisstephens
 Share

Recommended Posts

Hopefully this is pretty easy/quick. I have a signup form with name and email and on submit, a new page is created under "Entries". Is it possible to check if the name (which has two fields for first and last) and or the email is already registered and display an error?

Link to comment
Share on other sites

You can simply do this :

  • Form submit => POST request => verify($input->name, $input->email);

Then in the verify function, you parse the name and check if it already exist in your system. If it exist, your return an error, else you create an new entry.

 

Which difficulty are you encountering ?

  • Like 2
Link to comment
Share on other sites

I guess my difficulty is I have never actually done verifying before using the Processwire API. I have always started with: 

if(isset( $_POST['submit'])){
	$u = new Page();
	$u->template = "entries";
	$u->parent = $pages->get("/entries/");

	$u->name = $sanitizer->text($post_name); 
	$u->title = $sanitizer->text($upper_name);

	$u->save();
	$u->setOutputFormatting(false);
	$session->redirect("?alert=form-success"); 
}

else {
	$session->redirect("?alert=form-failure"); 
}

However, I was not quite sure how to get the post input, check if pages already had the email or name, and act accordingly.

Link to comment
Share on other sites

if(isset( $_POST['submit'])){
	$submitted = $pages->find("parent=/entries/, (submitter_name={$input->post->name}),(mail={$input->post->mail})");
    if($submitted){
        $u = new Page();
        $u->template = "entries";
        $u->parent = $pages->get("/entries/");

        $u->name = $sanitizer->text($post_name); 
        $u->title = $sanitizer->text($upper_name);

        $u->save();
        $u->setOutputFormatting(false);
        $session->redirect("?alert=form-success"); 
      }else {
     	$session->redirect("?alert=existing-submission"); 
      }
}else {
	$session->redirect("?alert=form-failure"); 
}

This check is first_name OR mail exists already (and checks for exact value! maybe for name you want: name%=$input->post->name).

  • Like 3
Link to comment
Share on other sites

Could you double check the selector is ok?? I threw in some invented field names in the selector. (sorry to omit that)

It seems to me that the $submitted is not throwing a truthsy value (maybe try $submitted->count() so it returns a number which evals to true except for 0 pages found) and it's trying to create a page with the same page name. (John Smith, will crate page john-smith), also for setting the page rather use the $sanitizer->pageName()

Also, reviewing the code, maybe it's more correct to do this:

if(isset( $_POST['submit'])){
    //Rather only get the first page
	$submitted = $pages->get("parent=/entries/, (submitter_name={$input->post->name}),(mail={$input->post->mail})");
    //Checks for page id, if not it's NullPage object
    if($submitted->id){
       //Code from before
      }else {
     	$session->redirect("?alert=existing-submission"); 
      }
}else {
	$session->redirect("?alert=form-failure"); 
}

Hope i didn't make this more confusing haha

  • Like 2
Link to comment
Share on other sites

Not at all! I appreciate all the help with this. It appears that the form just now shows that everything is already existing.

$sent = false;
	
	if(isset( $_POST['submit'])){

		$name = Trim(stripslashes($_POST['firstname'])) . Trim(stripslashes($_POST['lastname']));
		$upper_name = Trim(stripslashes($_POST['firstname'])) . " " . Trim(stripslashes($_POST['lastname']));
		$post_name = strtolower($name);
		$todaysdate = date("F j, Y H:i a");


    	//Rather only get the first page
		$submitted = $pages->get("parent=/submissions/, (submissions={$post_name}),(submissions={$input->post->email})");
    	
    	//Checks for page id, if not it's NullPage object
    	if($submitted->id){

    					$u = new Page();
			$u->template = "submissions-entry";
			$u->parent = $pages->get("/submissions/entries/");

			$u->name = $sanitizer->text($post_name);
			$u->title = $sanitizer->text($upper_name);
			$u->submissions->_date = $sanitizer->text($todaysdate);
			$u->submissions->full_name = $sanitizer->text($upper_name);
			$u->submissions->email = $sanitizer->text($input->post->email);
	


        	$u->save();
        	$u->setOutputFormatting(false);
        	$session->redirect("?alert=form-success"); 



      	
      	} else {
     		$session->redirect("?alert=existing-submission"); 
      	}
	} else {
		//$session->redirect("?alert=form-failure"); 
	}

 

Link to comment
Share on other sites

What is submissions? e.g. submissions=$post_name. What sort of field is that?

if($submitted->id) means you found an existing page, so you should redirect, and not create a new page in that condition block.

Here's some pseudo-ish code based on @elabx's code since I don't know what submissions is. It assumes there is a parent with the path /submissions/ . That parent page has children which have an email field. In this code, we are looking for a child page whose name matches a submitted name and whose email matches a submitted email. I am assuming name here is ProcessWire's in-built name field and not some custom field. I have also used $input rather than $_POST. I have left your stripslashes in there since I don't know why exactly you are using them.

 

if($post->submit){

    $name = trim(stripslashes($post->firstname)) . trim(stripslashes($post->lastname));
    $upper_name = trim(stripslashes($post->firstname)) . " " . trim(stripslashes($post->lastname));
    $post_name = $sanitizer->pageName(strtolower($name));// I am assuming ProcessWire 'name' which is used to build the page URL
    $email = $sanitizer->email($post->email);
    $todaysdate = date("F j, Y H:i a");


    // rather only get the first page
   // this is an OR:group selector
    $submitted = $pages->get("parent=/submissions/entries/, (name={$post_name}),(email={$email})");
    
    // page with email and/or $post_name already exists, redirect
    if($submitted->id){
        $session->redirect("?alert=existing-submission"); 
    } 
    // good to go, create new page
    else {
        $u = new Page();
        $u->template = "submissions-entry";
        $u->parent = '/submissions/entries/';

        // some of these already sanitized above
        $u->name = $post_name;
        $u->title = $sanitizer->text($upper_name);
        $u->submissions->_date = $todaysdate;
        $u->submissions->full_name = $sanitizer->text($upper_name);
        $u->submissions->email = $email;
        $u->save();
        //$u->setOutputFormatting(false);// @question: why do you need this?
        $session->redirect("?alert=form-success"); 
    }
}

// show new form
else {
    //$session->redirect("?alert=form-failure"); 
}

 

  • Like 3
Link to comment
Share on other sites

1 hour ago, kongondo said:

What is submissions? e.g. submissions=$post_name. What sort of field is that?

if($submitted->id) means you found an existing page, so you should redirect, and not create a new page in that condition block.

Here's some pseudo-ish code based on @elabx's code since I don't know what submissions is. It assumes there is a parent with the path /submissions/ . That parent page has children which have an email field. In this code, we are looking for a child page whose name matches a submitted name and whose email matches a submitted email. I am assuming name here is ProcessWire's in-built name field and not some custom field. I have also used $input rather than $_POST. I have left your stripslashes in there since I don't know why exactly you are using them.

 

 

@kongondo, the field submissions is currently a Profields: Textarea field. However, after reading into it more, it seems that the field (and all fields created by it) are bundled into one searchable field. I am wondering if this is the cause of the issue.

Link to comment
Share on other sites

2 hours ago, louisstephens said:

@kongondo, the field submissions is currently a Profields: Textarea field. However, after reading into it more, it seems that the field (and all fields created by it) are bundled into one searchable field. I am wondering if this is the cause of the issue.

I suspect that's the issue. According to Profields: Textarea docs, you have to search the entire field. Since you want to search for both $somePage->submissions->email and $somePage->submissions->name (assuming name is the name of one of the properties in your Profields: Textarea), based on the example in the Profields: Textarea docs, your get() query could be:

$submitted = $pages->get("parent=/submissions/entries/, (submissions%={$post_name}),(submissions%={$email})");

 

  • Like 1
Link to comment
Share on other sites

Thanks kongondo! I will give that shot this morning. Lat night, I did get it working using:

$existing_name_check = $pages->find("name=$post_name");
$existing_email_check= $pages->find("submission_email=$email");

if (count($existing_name_check)) {
   	$session->redirect("?alert=existing-submission"); 
} else if (count($existing_email_check)) {
   	$session->redirect("?alert=existing-submission"); 
} else {
// create page
$session->redirect("?alert=form-success"); 
}

I really appreciate everyones' help as well as patience with me!

  • Like 1
Link to comment
Share on other sites

 

35 minutes ago, louisstephens said:

Lat night, I did get it working using:

Cool. Just optimise it with get() instead of find(). You could also use OR:groups selector.

<?php

$existing_name_check = $pages->get("name=$post_name");
$existing_email_check= $pages->get("submission_email=$email");

if (count($existing_name_check)) {
   	$session->redirect("?alert=existing-submission"); 
} else if (count($existing_email_check)) {
   	$session->redirect("?alert=existing-submission"); 
} else {
    // create page
    $session->redirect("?alert=form-success"); 
}

// OR...make it shorter

$exists = false;
if($pages->get("name=$post_name")->id) $exists = true;
elseif($pages->get("submission_email=$email")->id) $exists = true;

if ($exists) {
    $session->redirect("?alert=existing-submission");
}
else {
    // create page
    $session->redirect("?alert=form-success"); 
}

// OR..make it even shorter (OR:groups)

$exists = false;
// @todo: you might want to check if page is in the trash here, i.e. parent.id!=7, etc
if($pages->get("(name=$post_name),(email=$email)")->id) $exists = true;

if ($exists) {
    $session->redirect("?alert=existing-submission");
}
else {
    // create page
    $session->redirect("?alert=form-success"); 
}

 

Edited by kongondo
typos
  • Like 3
Link to comment
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
 Share

×
×
  • Create New...