title: How do I make ajax data work on my php file
body: <p>I have typed the following and want it to transfer the data to a PHP variable in my PHP file. I have been looking at other posts for around 3 hours but I doesn't really alleviate anything considering my code is different to the original posters and I'm having trouble understanding it.</p>
  
  <p>Here's the code for the java-script file I am working with...</p>
  
  <pre><code>    $(function(){
        $.ajax({
          type: "POST",
          url: 'http://localhost/HelpMe/html/a.php',
          data: ({name_info:nameVal}),
          success: function(data) {
           // alert(data)
          }
        });
      });
  </code></pre>
  
  <p><em>Note: the alert(data) is returning the text I want it to.</em></p>
  
  <hr>
  
  <p>And here is the PHP in my php file...</p>
  
  <pre><code>&lt;?php
    $img1 = $_POST['name_info'];
    echo $img1;
  ?&gt;
  </code></pre>
  
  <p>So again, I want the Ajax value sent over to the php file to be saved as a variable, but at the moment its just returning the error:</p>
  
  <pre><code>Notice: Undefined index: name_info in ****\mysite\html\a.php on line 2
  </code></pre>
  
  <p>Please note that I am extremely new to PHP and AJAX and don't have much back-end knowledge...</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT: Button code...</p>
  
  <pre><code>  function generateURLfront_end() {
  if($('#confirm-title').is(':checked') &amp;&amp; $('#confirm-name').is(':checked') &amp;&amp; $('#confirm-question').is(':checked')) {
  
  
    $(function(){
          $.ajax({
            type: "POST",
            url: 'http://localhost/HelpMe/html/a.php',
            data: {name_info: nameVal},
            success: function(data) {
             alert(data)
            }
          });
        });
  </code></pre>
  
  <p>}</p>
  
  <p>Button HTML...</p>
  
  <pre><code>          &lt;button onclick="generateURLfront_end();" class="btn blue waves-effect waves-light z-depth-3" id="submit-but"&gt;Submit
              &lt;i class="material-icons right"&gt;cloud&lt;/i&gt;
            &lt;/button&gt;
  </code></pre>
  
  <p>Returns the value I want...</p>
  
  <p><a href="https://i.stack.imgur.com/co3cy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/co3cy.png" alt=""></a></p>
  
  <p>Doesn't echo the variable that I thought was being set with the Ajax code...</p>
  
  <p><a href="https://i.stack.imgur.com/v3HZZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v3HZZ.png" alt="enter image description here"></a></p>
  
  <p>EDIT: Tried the following on the instructions of the person who told me to use the click(function(e).</p>
  
  <pre><code>          $('submit-but').click(function(e){e.preventDefault();
            $.ajax({
              type: "POST",
              url: 'http://localhost/HelpMe/html/a.php',
              data: {name_info:nameVal},
              success: function(data) {
               alert(data)
              }
          });
        })
  </code></pre>
  
  <p>Still doesn't echo my created variable on a.php. Just greeted with the the same error message...</p>
  
  <p>EDIT: Someone just posted that it was my PHP and told me I should include the following to my php file..</p>
  
  <pre><code>    $inputJSON = file_get_contents('php://input');
  $img1 = $inputJSON['name_info'];
  </code></pre>
  
  <p>Thats given me the errors...</p>
  
  <pre><code>Notice: Uninitialized string offset: 0 in C:\Users\Trents\Desktop\XXAMP\htdocs\HelpMe\html\a.php on line 3
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Warning: Illegal string offset 'name_info' in C:\Users\Trents\Desktop\XXAMP\htdocs\HelpMe\html\a.php on line 3
  </code></pre>
  
  <p>is there anyway you could help me resolve these or give me some information on how to fix them? Thanks!</p>
  
  <p>EDIT: Adding print_r($_POST); adds the line Array ()</p>