Jump to content

Recommended Posts

Posted

Hi,

I am studying forms and form processing with processwire.

I found this post in a thread with very good information about it:

https://processwire.com/talk/topic/3105-create-pages-with-file-upload-field-via-api/page-4#entry39669

Thing is I can't get the code to work that is posted there because as far as I can see the last part of the code is not closed correctly. Also my editor shows that the last part of the code is not close correctly.

Looks like a curly bracket or a ?> is not on the right place. Tried everything but can't find it.

Maybe I am missing something ?

This is the last part of the code where I think a mistake is causing it not to work:

// Save page again
$np->save();
?>

<p>Thank you for your contact information.</p>

<?php
return true;
}

else
{ ?>
<p> Sorry, your photo upload was not successful...</P>
<?php}
?>

And here is the complete code:

<?php// First, confirm that a submission has been made
if($input->post->contactname) 
{

// Set a temporary upload location where the submitted files are stored during form processing
$upload_path = $config->paths->assets . "files/contact_files/";

// New wire upload
$contact_photo = new WireUpload('contact_photo'); // Reference field name in HTML form that uploads photos
$contact_photo->setMaxFiles(5);
$contact_photo->setOverwrite(false);
$contact_photo->setDestinationPath($upload_path);
$contact_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

// Second wire upload (other_photos) [NEW CODE]
$other_photos = new WireUpload('other_photos'); // Reference field name in HTML form that uploads photos
$other_photos->setMaxFiles(10);  // Allow 10 other photos
$other_photos->setOverwrite(false);  // Use the temporary location set above
$other_photos->setDestinationPath($upload_path);
$other_photos->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

// execute upload and check for errors
$files = $contact_photo->execute();
$other_files = $other_photos->execute(); // [NEW CODE]

// Run a count($files) test to make sure there are actually files; if so, proceed; if not, generate getErrors()
if(!count($files))
{
$contact_photo->error("Sorry, but you need to add a photo!");
return false;
}

// Set up submissions in the ProcessWire page tree
$np = new Page(); // create new page object
$np->template = $templates->get("data"); // Set template for pages created from form submissions
$np->parent = $pages->get("/test/"); // Set parent for pages created from form submissions

// Send form submissions through ProcessWire sanitization and apply each to a template field for the new page
$np->of(false);
$np->title = $sanitizer->text($input->post->contactname);
$np->name = $np->title;
$np->contactname = $sanitizer->text($input->post->contactname);
$np->email = $sanitizer->email($input->post->email);
$np->comments = $sanitizer->textarea($input->post->comments);
$np->save();

// Run photo upload for "contact_photo"
foreach($files as $filename)
{
$pathname = $upload_path . $filename;
$np->contact_photo->add($pathname);
$np->message("Added file: $filename");
unlink($pathname);
}

// Run photo upload for "other_photos" [NEW CODE]
foreach($other_files as $other_file)
{
$pathname = $upload_path . $other_file;
$np->other_photos->add($pathname);
$np->message("Added file: $other_file");
unlink($pathname);
}

// Save page again
$np->save();
?>

<p>Thank you for your contact information.</p>

<?php                    
return true;
}

else
{ ?>
<p> Sorry, your photo upload was not successful...</P>
<?php}
?>

This is the error message I get:

Parse Error: syntax error, unexpected '}' (line 73 of \site\templates\form.php)

Line 73 is the curly bracket in this part at the end of the code:

<?php                    
return true;
}

Hopefully somebody can see better than me what is the problem here.

This is the code of the form that is being processed by the code above:

<form action="/customer-service/contact/contact-success/" method="post" enctype="multipart/form-data">
<p><label for="contactname">Name:</label></p>
<p><input type="text" name="contactname"></p>
<p><label for="email">E-Mail:</label></p>
<p><input type="email" name="email"></p>
<p><label for="comments">Comments:</label></p>
<p><textarea name="comments" cols="25" rows="6"></textarea></p>
<p>Click the "Select Files" button below to upload your photo.</p>
<input type="file" name="contact_photo" />
<input type="file" name="other_photos[]" multiple />
<button type="submit">Submit</button>
</form>
Posted

Edit:

When I remove the comment: // First, confirm that a submission has been made

in the first line of the code I get this error message:

Parse Error: syntax error, unexpected end of file (line 98 of \site\templates\form.php)

Line 98 is the very last empty line in my editor
 

Posted
<?php
// First, confirm that a submission has been made
if($input->post->contactname) {

	// Set a temporary upload location where the submitted files are stored during form processing
	$upload_path = $config->paths->assets . "files/contact_files/";

	// New wire upload
	$contact_photo = new WireUpload('contact_photo'); // Reference field name in HTML form that uploads photos
	$contact_photo->setMaxFiles(5);
	$contact_photo->setOverwrite(false);
	$contact_photo->setDestinationPath($upload_path);
	$contact_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

	// Second wire upload (other_photos) [NEW CODE]
	$other_photos = new WireUpload('other_photos'); // Reference field name in HTML form that uploads photos
	$other_photos->setMaxFiles(10);  // Allow 10 other photos
	$other_photos->setOverwrite(false);  // Use the temporary location set above
	$other_photos->setDestinationPath($upload_path);
	$other_photos->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

	// execute upload and check for errors
	$files = $contact_photo->execute();
	$other_files = $other_photos->execute(); // [NEW CODE]

	// Run a count($files) test to make sure there are actually files; if so, proceed; if not, generate getErrors()
	if(!count($files)) {
		$contact_photo->error("Sorry, but you need to add a photo!");
		return false;
	}

	// Set up submissions in the ProcessWire page tree
	$np = new Page(); // create new page object
	$np->template = $templates->get("data"); // Set template for pages created from form submissions
	$np->parent = $pages->get("/test/"); // Set parent for pages created from form submissions

	// Send form submissions through ProcessWire sanitization and apply each to a template field for the new page
	$np->of(false);
	$np->title = $sanitizer->text($input->post->contactname);
	$np->name = $np->title;
	$np->contactname = $sanitizer->text($input->post->contactname);
	$np->email = $sanitizer->email($input->post->email);
	$np->comments = $sanitizer->textarea($input->post->comments);
	$np->save();

	// Run photo upload for "contact_photo"
	foreach($files as $filename) {
		$pathname = $upload_path . $filename;
		$np->contact_photo->add($pathname);
		$np->message("Added file: $filename");
		unlink($pathname);
	}

	// Run photo upload for "other_photos" [NEW CODE]
	foreach($other_files as $other_file) {
		$pathname = $upload_path . $other_file;
		$np->other_photos->add($pathname);
		$np->message("Added file: $other_file");
		unlink($pathname);
	}

	// Save page again
	$np->save();

	echo "<p>Thank you for your contact information.</p>";
	                 
	return true;

}else { 
	echo "<p>Sorry, your photo upload was not successful...</P>";
}

I've just gone though it and prettified the code. Also for line one: Do never put things right after "<?php". Not even comments. There has to be at least a space.

  • Like 1
Posted

Mucho thank you LostKobrakai. Your changes made me end up with an error message that at least made me know what to do.

Parse Error: syntax error, unexpected '<' (line 75 of site\templates\form.php)

Adding ?> after the last } in:

}else {
    echo "<p>Sorry, your photo upload was not successful...</P>";
}

so it looks like this:

}else {
    echo "<p>Sorry, your photo upload was not successful...</P>";
}
?>

Made the error go away and now the form shows up. A major step forward.

I guess normally it should not be needed to put there ?> but in my case without it,

that last error message comes back !

Also the message: Sorry, your photo upload was not successful... shows up in all cases, all the time.

Anyway it has come alive, so from here I should be able to go on with it.

Thanks.

Posted

Of course, the part:

else {
    echo "<p>Sorry, your photo upload was not successful...</P>";
}

will always be true in the beginning as long as the form has not been submitted

and so the message: Sorry, your photo upload was not successful...

will always show up in the beginning.

Posted

I see that this line of code is used: $contact_photo->error("Sorry, but you need to add a photo!");

If I go to http://cheatsheet.processwire.com and look in the api cheatsheet, I see nowhere the part: ->error

Where can I find more about ->error and how to use it with api ?

Edit:

I found something here:

https://processwire.com/talk/topic/4659-customizing-error-messages-for-inputfields/

https://processwire.com/talk/topic/1126-how-to-debug-use-the-error-log/

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...