English 中文(简体)
如何将数据从html传送到mongodb?
原标题:How do i send data from html to mongodb?

我是用明示和Mongo的 no子。 我有一个简单的方案,通过无线电纽州在用户环境中进行。 我有一个纽芬兰语,但我不知道如何从那里开展工作。 难道我需要一份新的联合材料来把它寄给蒙戈布? 我想要的是,我的html可以节省这些优惠,将其储存到数据库中。 我开始采取什么步骤?

Example here: if I clicked male and hit submit, I would want the word male to save to the database.

<html>
  <body>
      <form action="">
        <input type="radio" name="sex" value="male">Male<br>
        <input type="radio" name="sex" value="female">Female
      </form>
  </body>
</html>
最佳回答

首先,你想向 no子派出一个网站。

This will show you how to do that. How to retrieve POST query parameters?

Then you need to take that info and parse it and then send it to Mongo.

This will show you how to do an insert using Nodejs Mongo Driver. http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html

问题回答
  1. create server.
  2. send http req.
  3. connect to mongodb.
  4. insert data in mongodb
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Submission</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <h1>Email Submission Form</h1>

    <form id="emailForm">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <button type="button" onclick="submitEmail()">Submit</button>
    </form>

    <script>
  function submitEmail() {
    // Get the email input value
    var emailInput = document.getElementById("email");
    var email = emailInput.value;

    // Validate the email (you may add more sophisticated validation)
    if (!isValidEmail(email)) {
        alert("Invalid email address. Please enter a valid email.");
        return;
    }

    // Construct the MongoDB Atlas Data API endpoint
    var apiKey =  YOUR_API_KEY ; // Replace with your MongoDB Atlas API key
    var clusterName =  YOUR_CLUSTER_NAME ; // Replace with your cluster name
    var databaseName =  YOUR_DATABASE_NAME ; // Replace with your database name
    var collectionName =  emails ;

    var endpointUrl = `https://${clusterName}.mongodb.net/api/atlas/v1.0/groups/${apiKey}/database/${databaseName}/collections/${collectionName}`;

    // Create a new XMLHttpRequest object
    var xhr = new XMLHttpRequest();

    // Configure it to make a POST request
    xhr.open("POST", endpointUrl, true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.setRequestHeader("Api-Key", apiKey);

    // Prepare the data to be sent
    var emailData = { "email": email };

    // Convert the data to JSON and send the request
    xhr.send(JSON.stringify(emailData));

    // Handle the response (you may add more error handling)
    xhr.onload = function () {
        if (xhr.status === 201) {
            alert("Email submitted successfully!");
            // You can add more logic here after a successful submission
        } else {
            alert("Error submitting email. Please try again.");
        }
    };
}

function isValidEmail(email) {
    // Basic email validation, you can enhance it as needed
    var emailRegex = /^[^s@]+@[^s@]+.[^s@]+$/;
    return emailRegex.test(email);
}
</script>

</body>
</html>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签