0

I have the form that consists of input text and button. I need to get the text from the input field and pass into the php variable. I use ajax, callback function responds to me but php doesn't get the data. Here is occurring such an error - Notice: Undefined index: site_input.

<form>
    <div class="input-group">
      <input type="text" class="form-control" placeholder="URL" value="">
        <span class="input-group-btn">
          <button id="start" class="btn" type="button">Start</button>
        </span>
    </div> 
</form>

//jQuery
 $("#start").click(function(){
        $(document).ready(function(){
            if(status == 0) {
            $("#start").css("background-color", "red");
            $("#start").text("Stop");
             var site = $(".form-control").val();
            $.ajax({
                url: "index.php",
                data: {
                    "site_input" : site
                },
                type: "POST",
                success: function(data) {
                    alert("OK");
                }
            });
            status = 1;   
        }
//index.php
$a =isset($_POST['site_input'])?$_POST['site_input']:'not yet';
echo $a ;
$value = $_POST['site_input'];
echo $value;
  • You can check this [https://stackoverflow.com/questions/45584571/how-to-pass-a-single-form-value-with-ajax-to-php/45585162#45585162](https://stackoverflow.com/questions/45584571/how-to-pass-a-single-form-value-with-ajax-to-php/45585162#45585162), it is very slimier to your case. – Haitham Adnan Mubarak Aug 09 '17 at 15:34
  • Possible duplicate of [Validate Name Field In javascript](https://stackoverflow.com/questions/17899107/validate-name-field-in-javascript) – kinny94 Aug 09 '17 at 15:37
  • Asynchronous 101. Have a look at [this](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – kinny94 Aug 09 '17 at 15:40
  • `if(status == 0) {` what is this supposed to be? an undefined variable will never be equal to 0. – Kevin B Aug 09 '17 at 18:58
  • There is a global variable 'status' before the function. I set it up to toggle my button from Start to Stop and so on. – Anton Huzhov Aug 09 '17 at 19:01
  • open console and look for errors. make sure preserve log is enabled for the console. I'm expecting a syntax error – Kevin B Aug 09 '17 at 19:03
  • @KevinB only this one. `Uncaught Error: Bootstrap's JavaScript requires jQuery at bootstrap.min.js:6` But I don't reckon that it's the problem. PHP can't catch the index of $_POST['//this index//']. My js is working perfectly. – Anton Huzhov Aug 09 '17 at 19:11
  • that sounds like a pretty serious problem. – Kevin B Aug 09 '17 at 19:11
  • @KevinB could you please look through my code? As far as I see, you're an expert in web area. It has to be easy for you. – Anton Huzhov Aug 09 '17 at 19:12
  • First, fix the bootstrap problem. How do you know your js is working perfectly? What about it is working? – Kevin B Aug 09 '17 at 19:13
  • How do you know $.ajax is what is causing php to be reached? If it were, surely `$_POST['site_input']` would be set. – Kevin B Aug 09 '17 at 19:14
  • @KevinB dunno how the hell to fix that. Everything is included, just downloaded fresh bootstrap and replaced my old one. Still the same error. How do I know? I did console.log(site) and it takes data from my text field properly. – Anton Huzhov Aug 09 '17 at 19:19
  • that error indicates that you're including bootstrap before jquery. jQuery must come first, because bootstrap depends on it. – Kevin B Aug 09 '17 at 19:20
  • @KevinB Oh thanks a lot, but the main problem still remains. – Anton Huzhov Aug 09 '17 at 19:22
  • Then then next step would be to -> https://stackoverflow.com/questions/45594944/php-cant-reach-ajax-data?noredirect=1#comment78156966_45594944 Also look for your request in the network tab (don't forget to enable preserve log there too) – Kevin B Aug 09 '17 at 19:22
  • @KevinB http://imgur.com/4BudSWY – Anton Huzhov Aug 09 '17 at 19:30
  • I see a request there, what's in the request body? (scroll down) – Kevin B Aug 09 '17 at 19:31
  • Also note that that is a GET request, not a POST request, which supports my theory that $.ajax isn't what is causing the request. – Kevin B Aug 09 '17 at 19:32
  • @KevinB Probably this one. http://imgur.com/a/1A4N9 – Anton Huzhov Aug 09 '17 at 19:45
  • @KevinB Could you please just download my code and look it through? http://dropmefiles.com/HV576 – Anton Huzhov Aug 09 '17 at 19:47

0 Answers0