« MediaWiki:Common.js » : différence entre les versions

De wiki Tim
Aucun résumé des modifications
Aucun résumé des modifications
 
Ligne 29 : Ligne 29 :
     };
     };


     $.post(apiUrl, params, function(data) {
     mw.Api.post(apiUrl, params, function(data) {
         if (data && data.edit && data.edit.result == 'Success') {
         if (data && data.edit && data.edit.result == 'Success') {
             window.location.reload(); // Reload the page to show the new question
             window.location.reload(); // Reload the page to show the new question

Dernière version du 12 avril 2024 à 16:50

<form id="newQuestionForm" onsubmit="return postNewQuestion();">
    <label for="questionTitle">Titre de votre question :</label>
    <input type="text" id="questionTitle" name="questionTitle" required><br>
    <label for="questionBody">Détaillez votre question :</label>
    <textarea id="questionBody" name="questionBody" required></textarea><br>
    <input type="submit" value="Poser la question">
</form>

<script>
function postNewQuestion() {
    var title = document.getElementById('questionTitle').value;
    var body = document.getElementById('questionBody').value;
    var fullContent = `== ${title} ==\n{{Discussion|titre=${title}|question=${body}|réponse=}}\n`;
    postContent(fullContent);
    return false;
}

function postContent(content) {
    var apiUrl = mw.config.get('wgScriptPath') + '/api.php';
    var editToken = mw.user.tokens.get('csrfToken');
    var pageTitle = mw.config.get('wgPageName');

    var params = {
        action: 'edit',
        title: pageTitle,
        appendtext: content,
        token: editToken,
        format: 'json'
    };

    mw.Api.post(apiUrl, params, function(data) {
        if (data && data.edit && data.edit.result == 'Success') {
            window.location.reload(); // Reload the page to show the new question
        } else {
            alert('Error: Could not post your question.');
        }
    });
}
</script>