title: can I get text from html and put it into variable in php without submitting a form?
body: <p>I have a form in html: </p>
  
  <pre><code>&lt;form&gt;
  &lt;label&gt;&lt;input type="hidden" name="pNameChange" value=""&gt;&lt;/label&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and I want to get the value of this input in php without submitting it in a form.</p>
  
  <p>this is my javascript:</p>
  
  <pre><code>var pName= null;
  $(document).ready(function(){
      $('img').click(function(){
          pName= $(this).attr("name");
          console.log(pName);
      });
  });
  </code></pre>
  
  <p>My php:</p>
  
  <pre><code>$pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
  </code></pre>
  
  <p>what I want is. you click on the picture,</p>
  
  <p>1.the value of the name attribute of the picture is going to be saved into the variable <code>pName</code> (javascript), </p>
  
  <p>2.it then goes into the form and changes the value of the form to the variable <code>pName</code> (javascript), </p>
  
  <p>3.php picks up the value of the form (which should now be equal to <code>pName</code>), </p>
  
  <p>4.then stores it into a variable <code>$pName</code> (php). </p>
  
  <p>5.I also want <code>$pName</code> (php) to be globally used throughout all the pages of the website.</p>
  
  <p><em>edit</em></p>
  
  <p>this is my index page:</p>
  
  <pre><code>&lt;?php
  $pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
  $db_connection = mysqli_connect('localhost','root','',"project_online_planner");
  if (!$db_connection){
      die('Failed to connect to MySql:'.mysql_error());
  }
  $query="SELECT * FROM project limit 5 ";
  $results = mysqli_query($db_connection,$query);
  $intro=mysqli_fetch_assoc($results);
  ?&gt;
  &lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Project planner online&lt;/title&gt;
      &lt;script src="http://code.jquery.com/jquery-2.1.0.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="ppo.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="ppo.css"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="bgNav"&gt;
      &lt;div id="login"&gt;
          &lt;a href="register.php"&gt;Register&lt;/a&gt;
          &lt;a href="login.php"&gt;Log in&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;nav id="nav"&gt;
          &lt;a href="index.php"&gt;Home&lt;/a&gt;
      &lt;/nav&gt;
  &lt;/div&gt;
  &lt;h2 class="titlePage"&gt;Home&lt;/h2&gt;
  &lt;div id="bgTile"&gt;
      &lt;?php
  
      while($row = mysqli_fetch_array($results))
      {
          $project = $row["name"];
          echo nl2br("&lt;a href='project.php'&gt;" ."&lt;img name=\"$project\" width='100px' alt='Procject name' height='100px' class='tile' src=". $row['image'] ."/&gt;". "&lt;/a&gt;");
      }
      ?&gt;
  
      &lt;a href="allprojects.php"&gt;&lt;div class="tile" id="tileM"&gt;&lt;h2&gt;Meer&lt;/h2&gt;&lt;/div&gt;&lt;/a&gt;
  
  &lt;/div&gt;
      &lt;form&gt;
          &lt;label&gt;&lt;input type="hidden" name="pNameChange" value=""&gt;&lt;/label&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>what I want: click on the image then you get sent to the project page where (php) <code>$pName</code> is equal to the value of (javascript) <code>pName</code></p>
  
  <p>project page:</p>
  
  <pre><code>&lt;?php
  $newRecord = null;
  $pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
  $db_connection = mysqli_connect('localhost','root','',"project_online_planner");
  if (!$db_connection){
      die('Failed to connect to MySql:'.mysql_error());
  }
  
  //insert into database
  if(isset($_POST['insertComments'])){
      include('connect-mysql.php');
      $username = $_POST['username'];
      $comment = $_POST['comment'];
      $sqlinsert = "INSERT INTO user_comments (username, comment, project) VALUES ('$username', '$comment', '$pName')";
          if (!mysqli_query($db_connection, $sqlinsert)){
              die('error inserting new record');
          }
          else{
              $newRecord = "1 record added";
          }//end nested statement
  
  }
  
  //text from database
  $query="SELECT * FROM user_comments where project = '$pName' ";
  $results = mysqli_query($db_connection,$query);
  $intro=mysqli_fetch_assoc($results);
  
  $query2="SELECT * FROM project where name = '$pName' ";
  $results2 = mysqli_query($db_connection,$query2);
  $intro2=mysqli_fetch_assoc($results2);
  
  
  ?&gt;
  &lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Project planner online&lt;/title&gt;
      &lt;script src="http://code.jquery.com/jquery-2.1.0.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="ppo.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="ppo.css"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="intro"&gt;
  
  &lt;/div&gt;
  &lt;div id="bgNav"&gt;
      &lt;nav id="nav"&gt;
          &lt;a href="index.php"&gt;Home&lt;/a&gt;
          &lt;a class="rightNav" href="register.php"&gt;Register&lt;/a&gt;
          &lt;a class="rightNav" href="login.php"&gt;Log in&lt;/a&gt;
      &lt;/nav&gt;
  &lt;/div&gt;
  
  &lt;div id="projectTile"&gt;
      &lt;span id="statusCheck"&gt;&lt;?php print_r($intro2["status"]); ?&gt;&lt;/span&gt;
      &lt;h2 id="prTitle"&gt;&lt;?php print_r($intro2["name"]); ?&gt;&lt;/h2&gt;
  &lt;div id="prPic"&gt;&lt;img width="300" height="200" src="&lt;?php print_r($intro2["image"]); ?&gt;"&gt;&lt;/div&gt;
  &lt;div id="prDescription"&gt;&lt;?php print_r($intro2["description"]); ?&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="comments"&gt;
      &lt;?php
      while($row = mysqli_fetch_array($results))
      {
          echo nl2br("&lt;div class='profile_comments'&gt;" . $row['username'] . "&lt;/div&gt;");
          echo nl2br("&lt;div class='comment_comments'&gt;" . $row['comment'] . "&lt;/div&gt;");
      }
      ?&gt;
  
  &lt;/div&gt;
  
  &lt;div id="uploadComments"&gt;
      &lt;form method="post" action="project.php"&gt;
          &lt;label for="name"&gt;&lt;input type="hidden" name="insertComments" value="true"&gt;&lt;/label&gt;
          &lt;fieldset&gt;
              &lt;legend&gt;comment&lt;/legend&gt;
          &lt;label&gt;Name:&lt;input type="text" id="name" name="username" value=""&gt;&lt;/label&gt;&lt;br/&gt;
          &lt;label&gt;Comments: &lt;textarea name="comment" id="comment"&gt;&lt;/textarea&gt;&lt;/label&gt;
          &lt;input type="submit" value="Submit" id="submitComment"&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;?php
  echo $newRecord;
  ?&gt;
  &lt;form&gt;
      &lt;label&gt;&lt;input type="hidden" name="pNameChange" value=""&gt;&lt;/label&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>