Jump to content

Posting JS data to use in php

jam1234

Hey guys,

 

I am making a sign-up system thingy.

 

I want to post the data so that I can use it on another page. The thing that I am stuck on is converting it from JS into PHP.

 

Heres what I have so far:

$( "#signup-form a" ).click(function() {        if ($('#signup-form a').text() != "Sign Up") {            return;           } else {            $email = $('#input-signup1').val();            $password = $('#input-signup2').val();                    }    });

So i want to post the data to signup.php.

Is this possible? If so how do I do it?

Sorry if its easy im new to JavaScript.

 

Thanks in advance!

My PC: http://uk.pcpartpicker.com/p/MN3YwP


Wish I could afford to do giveaways for you guys, Sorry school doesn't pay me! <3


Link to comment
Share on other sites

Link to post
Share on other sites

Why use Javascript at all? If the page is a PHP script, submitting it will put the values of all the input fields in the $_POST superglobal array, assuming that you have set the method of the form as such.

Link to comment
Share on other sites

Link to post
Share on other sites

You can use jQuery like this:

$("#myForm").submit(function() {    e.preventDefault();    var url = "path/to/your/script.php"; // the script where you handle the form input.    $.ajax({           type: "POST",           url: url,           data: $("#myForm").serialize(), // serializes the form's elements.           success: function(data)           {               alert(data); // show response from the php script.           }         });});

But for a sign-up form you don't want to keep the user on the page anyway so it would be better to just submit it normally.

Link to comment
Share on other sites

Link to post
Share on other sites

Why use Javascript at all? If the page is a PHP script, submitting it will put the values of all the input fields in the $_POST superglobal array, assuming that you have set the method of the form as such.

Maybe because using ajax to post the data is a more streamlined experience for the user?

 

@op

 

function post(){var example = document.getElementById("example").value;var dataString = 'example=' + example;if(example == ''){return false; //do something here like tell them to fill out fields}else  {    $.ajax({      type: "POST",      url: "your.php", //your php file to post to      data: dataString, //variable above, its not necessary to use a var but makes it more organized for a long string. + possible var re assignment later on for ez pz? idk      cache: false,      success: function(html)      {        //if it posts good it returns the html (good for if you want to echo "err" or something then fade in an error)      }    });  }}

Then for your button just do

<button onclick="post()">Sign Up!</button>

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

 

Maybe because using ajax to post the data is a more streamlined experience for the user?

 

@op

 

function post(){var example = document.getElementById("example").value;var dataString = 'example=' + example;if(example == ''){return false; //do something here like tell them to fill out fields}else  {    $.ajax({      type: "POST",      url: "your.php", //your php file to post to      data: dataString, //variable above, its not necessary to use a var but makes it more organized for a long string. + possible var re assignment later on for ez pz? idk      cache: false,      success: function(html)      {        //if it posts good it returns the html (good for if you want to echo "err" or something then fade in an error)      }    });  }}

Then for your button just do

<button onclick="post()">Sign Up!</button>

 

Thanks :) Only one to understand my reasoning!

My PC: http://uk.pcpartpicker.com/p/MN3YwP


Wish I could afford to do giveaways for you guys, Sorry school doesn't pay me! <3


Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×