0

Is there a way, when clicking on a button, using HTML and PHP, to display an alert message containing the name of the session started, a radio button checked and the date and time choose? The button is included in a form, so I am looking if there is way to get these things done...

Here's my code:

<?php

if (!isset($_SESSION)) {
    session_start();
}

echo $_SESSION["uname"]; ?>

</div>

<br>

<form id="form1" action="welcome.php" method="post" >

    Blood Type Needed :
    <br>
    <br>
    <input id="input1" type="Radio" value="A+" name="bn"/> A

    <input id="input1" type="Radio" value="B+" name="bn"/> B

   <input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />

$('input[@name="bn"]:checked').val();

var radios = document.getElementsByName('bn');

if (radios.checked) {
    function showSession(){


 var x=document.getElementById("sessionId");


var y=new Date();


 alert(x.value+" wants "+radios.value+y);

}
}

            </script>
                 <input id="done" class="button1" type="button" onClick="showSession();"  value="   DONE   ">


            </form>
Charbel
  • 177
  • 1
  • 5
  • 14
  • 2
    javascript has no concept of, and couldn't care less about PHP sessions. If you want JS to be able to display PHP session state, you will have to have some mechanism of TELLING js that a session's been started, e.g. `session_start(); echo 'var started = true';`?> inside a js ` – Marc B Apr 09 '13 at 17:11
  • 1
    You can do this through ajax, make an ajax request to the file containing the session code and depending on the respond populate your check boxes and text boxes using javascript....look for jquery ajax() function. – Jay Bhatt Apr 09 '13 at 17:15

2 Answers2

0

I will use a INPUT hidden and a javascript function, after attack it to onClick event on your button, but suggest you use Jquery to make all cross-browser:

HTML input

<input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />

JAVASCRIPT

function showSession(){
   var x=document.getElementById("sessionId");
   alert(x.value);
}

HTML BUTTON

<input type="button" value="Click me" onClick="showSession();" />


FULL SCRIPT

<?php
    if (!isset($_SESSION)) session_start();
    echo "Session ID: ".$_SESSION["uname"]; 
?>
</div><br/>
<script>
    function showData(){
        var sessionId = document.getElementById("sessionId").value;
        var today = new Date();
        var x = document.getElementsByName('bn')
        var selected = false;
        // Check the selected value

        for(var k=0;k<x.length;k++)
          if(x[k].checked){
            alert(' '+ sessionId + " wants " + x[k].value + " " +today);
            selected = true;
          }
        if (!selected) alert('Select something please...');
    }
</script>
<form id="form1" action="welcome.php" method="post" ><br/>
    Blood Type Needed :<br/><br/>
    <input id="AP" type="Radio" value="A+" name="bn"/><label for="AP"> A</label><br/>
    <input id="BP" type="Radio" value="B+" name="bn"/><label for="BP"> B</label><br/><br/>
    <input id="done" type="button" onClick="showData();" value="   DONE   " class="button1">
    <input type="hidden" id="sessionId" value="<?php echo $_SESSION['uname']; ?>" />
</form>
Alex Abreu Mulet
  • 118
  • 1
  • 1
  • 9
0

You can do this by using simple Javascript

<script>
   function showValue()
   {
       var sessionVal=<?php echo $_SESSION['uname']; ?>;
       var selectedDate=documnet.form1.dt.value;
       var selectedRadio=document.form1.bn.value;
       alert(sessionVal+" "+selectedRadio+" "+selectedDate); //what ever format you want to show.
       document.form1.actiom='welcome.php';
       document.form1.submit();
   }
 </script>

In your html add onsubmit function

  <form id="form1" action="" onsubmit="return showValue();" method="post" >
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86