-1

So here is my scripts:

my Ajax:

$('#ether-wallet').submit(function (e) {
    e.preventDefault();
    var name = $('#etherwallet').val();
    var dataString = 'name=' + name;
    $.ajax({
        type: 'POST',
        data: dataString,
        url: 'Database/wallet.inc.php',
        success: function (data) {
            alert(data);
        }
    });
});

and I am trying to submit the form without leaving the page, Here is my PHP:

    <form id="ether-wallet" class="wallet-form" method="POST">
        <span class="span">Ether Wallet:  </span>
        <input type="text" name="etherwallet" placeholder="Press to lock" id="etherwallet" class="inputapi" value=""/>
        <button id="etherlock" class="lockbutton" type="submit" name="etherlock">Lock</button>
    </form>

and here's where I want to send the data

all of this is inside the php

session_start();

if(isset($_POST['ether-wallet'])) {

include_once 'dhb.inc.php';

$etherwallet = mysqli_real_escape_string($conn, $_POST['name']);
$idbridge1 = mysqli_real_escape_string($conn, $_SESSION['u_id']);
if (empty($etherwallet)) {
    header("Location: ../signup.php?signup=success1");
    exit();
} else {
    if (strlen($etherwallet) != 42 ) {
        header("Location: ../signup.php?signup=success2");
        exit();
    } else {
        $sql = "UPDATE users SET eth_wallet = '$etherwallet' WHERE user_id = '$idbridge1'";
        mysqli_query($conn, $sql);
        header("Location: ../signup.php?signup=success3");
        exit();
    }
}

}

Thanks for the help boys!!

SpectorHacked
  • 19
  • 1
  • 6
  • What's your question? Which part of the code is not working as expected? – Rajdeep Paul Sep 09 '17 at 14:19
  • the SQL isnt getting any input data, I was reading this script 10 times And didnt find any errors, Did I Insert corectlly to the Database? Did I AJAX corectlly? Why the data doesnt go inside the specific colmun with the user id.. – SpectorHacked Sep 09 '17 at 14:55

4 Answers4

0

Use input type="button" onClick="function Name"

Or button type="button"

Dont Use input type="submit"...

May help...

Regards, Vishal Sharma

0

If you are using the button type "Submit", you may also bind the "submit" event in the form (instead of bind "click" event in the button). You also have to return "false" value in your function after AJAX call statement.

0

Just change the ajax code as below.

$('#eether-wallet').submit(function (e) {
    e.preventDefault();
    var name = $('#etherwallet').val();
   //var dataString = 'name=' + name;
    $.ajax({
        type: 'POST',
        data: new FormData(this),
        url: 'Database/wallet.inc.php',
        success: function (data) {
            alert(data);
        },
      error: function(jqXHR, textStatus, errorThrown)
    {
      alert(textStatus);
    }
    });
});

Now we are calling the ajax inside form submit event. so that e.preventDefault() will prevent the postback of form.

Jino Shaji
  • 1,097
  • 14
  • 27
  • I got error coming, And I cant see where you insert the 'name' value inside the data here is the errors: Uncaught TypeError: Illegal invocation at e (jquery.min.js:4) at Ab (jquery.min.js:4) at Function.r.param (jquery.min.js:4) at Function.ajax (jquery.min.js:4) at HTMLFormElement. (Wallets.php:196) at HTMLFormElement.dispatch (jquery.min.js:3) at HTMLFormElement.q.handle (jquery.min.js:3) e @ jquery.min.js:4 Ab @ jquery.min.js:4 r.param @ jquery.min.js:4 ajax @ jquery.min.js:4 (anonymous) @ Wallets.php:196 – SpectorHacked Sep 09 '17 at 15:44
  • if the error is from the ajax call now the alert will be displayed. Go through the edited answer. – Jino Shaji Sep 09 '17 at 15:49
  • This is coming from ajax call and there are a lot of errors when I click the button, anyway the sql remain null, lol what have I dont wrong? – SpectorHacked Sep 09 '17 at 17:08
0

There are couple of issues in your code, such as:

  • You're accessing user submitted data using $_POST['etherlock'], which is wrong. See this statement in your AJAX request, var dataString = 'name=' + name;, you should be accessing user submitted data using $_POST['name'].
  • Your UPDATE query is wrong, it should be like this:

    $sql = "UPDATE users SET eth_wallet = '$etherwallet' WHERE user_id = '$idbridge1'";
    

Sidenote: Learn about prepared statement because right now your query is susceptible to SQL injection attack. And also see how you can prevent SQL injection in PHP.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I have never used AJAX before, So I am sorry If I am having trouble to understand, I am POST the value inside the data String? I tried to change the post to name but It just shows me my index.php – SpectorHacked Sep 09 '17 at 15:19
  • @SpectorHacked That's because you're handling the AJAX request i.e. this code snippet ` – Rajdeep Paul Sep 09 '17 at 16:21
  • I went back to the basic where I have no Errors on code, Now my code is sumbit the form, but I doesnt think my wallet.inc.php is getting the data Beacause I put some errors handlers in that page and it doesnt reach it.. I will edit the page and show you my currect code.. Thanks for the help!!! – SpectorHacked Sep 09 '17 at 17:14