0

I have 2 Button's.

  • 1 = Create field's
  • 2 = Create (write into database)

If i press on the button "Create field's" i get this structure on more time:

<div class="step">
  <div class="header_step">Schritt 1 des Tutorial's</div>
  <div class="body_step">
    <a class="create_tutorial_a">Titel des Schrittes</a>
    <input id="input_title_name0" class="create_tutorial_input">
    <br>
    <a class="create_tutorial_a">Bild</a>
    <input type="file">
    <br><br>
    <a class="create_tutorial_a_full_width">Beschreibung des Schrittes</a>
    <br>
    <textarea class="create_tutorial_textarea" id="input_description_name0">
    </textarea>
  </div>
</div>

When i press the button again, i will get the same again. Just the name's of the input field will change. I did this with Jquery.

After this i press the button "create". Now i want to write the value's of the field's into my database. My Form look like this:

<form method="post" action="index.php?content=create_tutorial" id="form1">
  
  <div class="step">
    <div class="header_step">Schritt 1 des Tutorial's</div>
    <div class="body_step">
      <a class="create_tutorial_a">Titel des Schrittes</a>
      <input id="input_title_name0" class="create_tutorial_input">
      <br>
      <a class="create_tutorial_a">Bild</a>
      <input type="file">
      <br><br>
      <a class="create_tutorial_a_full_width">Beschreibung des Schrittes</a>
      <br>
      <textarea class="create_tutorial_textarea" id="input_description_name0">
      </textarea>
    </div>
  </div>
  
  <div class="step">
    <div class="header_step">Schritt 1 des Tutorial's</div>
    <div class="body_step">
      <a class="create_tutorial_a">Titel des Schrittes</a>
      <input id="input_title_name1" class="create_tutorial_input">
      <br>
      <a class="create_tutorial_a">Bild</a>
      <input type="file">
      <br><br>
      <a class="create_tutorial_a_full_width">Beschreibung des Schrittes</a>
      <br>
      <textarea class="create_tutorial_textarea" id="input_description_name1">
      </textarea>
    </div>
  </div>
  
  
  
  <div class="step">
    <div class="body_step">
      <input class="create_button" id="submit" value="Erstellen" type="button">
      <input class="create_button" id="add_step" value="Schritt hinzufügen" type="button">
      <input class="create_button" id="remove_step" value="Schritt entfernen" type="button">
    </div>
  </div>
  
</form>

How can I send this form now to my .php page, where I will put the whole data into my database?

$.ajax({
    type: "post",
    url: "url",
    data: $("form").serialize(),
    success: function(data) {
       alert("success")
    }
})

I have this code, but I dont know how to use it. What is success & data? Maybe someone of you can help me.

2
  • Sounds like you need some basic instruction in AJAX. You may want to consider having a look at http:learn.jquery.com Commented May 7, 2015 at 16:22
  • Thanks for the link! It's good explained. Commented May 7, 2015 at 17:06

2 Answers 2

1

How can I send this form now to my .php page, where I will put the whole data into my database?

You have configured your form method as POST and you the attribute action = "index.php?content=create_tutorial"which is the endpoint or script that you are calling to do something with the data sent.

So, on submit, index.php should received the POST data using php global variable $_POST.

To simplify the example, the following form has one text input in a form using POST and action set as your endpoint.

HTML

<form method="post" action="index.php?content=create_tutorial">
  <input type="text" name="name"/><br>
  <input type="submit"/>
</form>

PHP

// index.php
<?php 
  $name = $_POST['name'];
  echo $name;
?>

PHP-MYSQL

// assuming connection to database is configured and setup done
$query = "INSERT INTO `user` (`name`) VALUES ($name)";
mysql_query($query);

Notes: - read more about security and validating user input here - take note of the attribute name value is the key to $_POST. - read about inserting database (assuming MySQL) here and deprecated mysql_query()

I have this code, but I dont know how to use it. What is success & data? Maybe someone of you can help me.

$.ajax() is a method to make HTTP requests asynchronously. Read more here on Wikipedia

  • async = true, is to simply say "let me know when you are done, i am going to do other stuffs"
  • async = false is to simply say "i'll wait for you before i do anything else"

By default, $.ajax() async flag is set to true. Because it implements the Promise interface,it provides you functions like

As of jQuery 1.5.1

  • .done() when there is a successful response, this function will be called.
  • .fail() when there is an error response, this function will be called.
  • .. more in $.ajax() documentation

.php

// index.php
<?php
  return "ok";
?>

.js

var jqxhr = $.ajax( "example.php" )
     .done(function(results) {
       alert(results); // "ok"
     })
     .fail(function(error) {
       alert( error ); // "failed"
     })

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! I think it will help! ^^
0

jQuery has already extensively documented the meaning and the various use cases of the $.ajax method here.

Basically, success is a function to be called if the request succeeds. It accepts a data argumentwhich is the data returned from the server (from your PHP backend). This is where you perform the actions you want to perform when the form data has been sent to the server and the server has responded. Note that the data from the server doesn't necessarily have to be used within the success function.

2 Comments

I want to send the whole form over POST to my other PHP page. And at the linked page I want to use PHP to write the data into my database.
Yeah the code you have above should do just that. You would just need to take care of the server side, capture the POSTed data and save it to your database. Sorry I can't give you insight on the server end but you can var_dump the contents of the $_POST variable in your PHP file if you don't have any idea what to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.