events
events copied to clipboard
Edit event in frontend form
How would you go about creating a frontend form for editing an exiting event. I'm using the below method for adding an event. I can pull in the exiting data using the event id but the form still creates a new entry rather than editing the chosen event.
I've tried adding {{ hiddenInput('entryId', entry.id) }}
<body>
{% set eventType = craft.events.getEventTypeById(1) %}
<form id="form-submission">
<input type="hidden" name="action" value="events/events/save">
<input type="hidden" name="typeId" value="1">
{{ csrfInput() }}
<input type="text" name="title" value="New Event">
{% for field in eventType.getFieldLayout().getFields() %}
{{ field.name }}: <input type="text" name="{{ field.handle }}">
{% endfor %}
<input type="date" name="startDate">
<input type="date" name="endDate">
<input type="submit" name="" value="send">
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#form-submission").on("submit", function(e){
e.preventDefault();
var formEl = new FormData($(this)[0]);
$.ajax({
url: $("input[name='action']",this).val(),
type: 'post',
method: "post",
processData: false,
contentType: false,
data: formEl,
complete: function(o){
console.log("Complete");
console.log(o);
}
});
})
})
</script>
</body>
Originally posted by @JoshC96 in https://github.com/verbb/events/issues/69#issuecomment-703211043
So the tricky part will be replicating the ticketing table, as per the control panel, where you can add multiple ticket types and their capacities, their own fields. You'll need some JS to handle adding new ticket definitions.
The best resource would be to view-source on the event edit page in the control panel and look at the name
attribute for inputs. So long as you replicate that, you'll be fine! Consult the (CP edit template)[https://github.com/verbb/events/blob/craft-3/src/templates/events/_edit.html].
But, it should like you're just missing sending the ID of the event in your form.
<input type="hidden" name="id" value="{{ event.id }}">
Thanks. That put me on the right track. The field was actually:
<input type="hidden" name="eventId" value="{{ event.id }}">
Ah, my mistake sorry!