I have PHP page and i am trying user can update phone number when click on EDIT button.
For that i created 1 button and when user click on EDIT the button changed to SAVE and now user enter phone number and press save button. Now user click save button i want to save that phone number in database so i want to execute query. I tried to add PHP code in javascript but its not working.

This is the button code. I am showing phone number from database. That works fine.

<input name='wophno' type='text' id='wophno' maxlength='10' disabled='disabled' value=".$res['PHONE_NUMBER'].">
<input type='button' value='Edit' id='editMno' name='editMno' onclick='update_mono()'>

onClick() i am calling javascript function update_mono()

 function update_mono()
    {
       var btnval = document.getElementById('editMno').value;
       if(document.getElementById('editMno').value == 'Edit')
       {
         document.getElementById('mophno').disabled = false;
         document.getElementById('mophno').focus();
         document.getElementById('editMno').value = 'Save';         
       }
       else
       {
         var mno = document.getElementById('mophno').value;    
         <?php 
            $sql_mono = $_GET[$person_id];
            $sql_mono="SELECT phone_number FROM PHONE P WHERE  P.Person_ID =".$person_id." AND   P.phone_type_code like 'M'";
            $s = @OCIRowCount($sql_mono);
            echo "document.write('" . $s . "');";
            if($s == 1)
            {
               $sql_upmono="UPDATE PHONE SET created_date = sysdate, phone_number = " ?>mno <?php " WHERE  Person_ID =".$person_id." AND phone_type_code = 'M'";
            }
            else
            {
               $sql_upmono="INSERT INTO PHONE VALUES(".$person_id.",'M',"?> mno <?php ",sysdate)";
            }
            $s = oci_parse($conn, $sql_upmono);
            oci_execute($s);
          ?>
          document.getElementById('mophno').disabled = true;
          document.getElementById('editMno').value = 'Edit';
         }
       }          

Please help me what is wrong with this code. How to call PHP code in javascript.
Thanks for help.

Recommended Answers

All 8 Replies

You can't call PHP code in JS, because JS will never see it. PHP code is executed server side meaning the only output from it shall be HTML.

What you are trying to do can be achieved by using AJAX which is Asynchronous JavaScript and lets you dynamically communicate with a database.

commented: u r right dude +2

Thank you for your help. But can you please give me idea how do i implement AJAX in my code.

Unfortunately I don't know any AJAX, however there are plenty of tutorials on the world wide web. Just search for something along the lines of:

'AJAX Communication with SQL database'

Good luck!

Is any other way i can implement this functionality. This is PHP page. And i want to insert phone number as user input. So is ther any other way i can achive this.
Apperciate your help.

You could always use it as a form and then just POST it?

I tried to call php page from javascript like this.

function update_mono(str1)
            {
                var btnval = document.getElementById('editMno').value;
                if(document.getElementById('editMno').value == 'Edit')
                {
                    document.getElementById('mophno').disabled = false;
                    document.getElementById('mophno').focus();
                    document.getElementById('editMno').value = 'Save';          
                }
                else
                {
                     xmlhttp=new XMLHttpRequest(); 
                     xmlhttp.onreadystatechange=function()
                     {
                       if(xmlhttp.readyState==4 && xmlhttp.status==200)
                        { document.getElementById("mophno").innerHTML=xmlhttp.responseText; }
                     }
                    xmlhttp.open("GET","updtin_mno.php?q="+str1,true);
                    xmlhttp.send();                 
                    document.getElementById('mophno').disabled = true;
                    document.getElementById('editMno').value = 'Edit';
                }
            }

and updtin_mno.php like

<?php
include_once('oracledb.php');

$person_id = $_REQUEST['id'];
$q=$_GET["q"];
$sql_mono="SELECT phone_number FROM PHONE P WHERE  P.Person_ID =".$person_id." AND P.phone_type_code like 'M'";
$s = @OCIRowCount($sql_mono);
echo "document.write('" . $s . "');";
if($s == 1)
{
    $sql_upmono="UPDATE PHONE SET created_date = sysdate, phone_number = '".$q."' WHERE  Person_ID ='".$person_id."' AND phone_type_code = 'M'";
}
else
{
    $sql_upmono="INSERT INTO PHONE(person_id,phone_type_code,phone_number,'created_date) VALUES(".$person_id.",'M','".$q."',sysdate)";
}
$s = oci_parse($conn, $sql_upmono);
oci_execute($s);
?>

But its showing unknown runtime error.
I am not getting idea why its showing error. Please help me to solve error.

I would use jQuery post() and post to another PHP file where the new number is then saved to the database . After that the PHP file can echo a callback back to the jQuery post function to verify that the number has been saved.
http://api.jquery.com/jQuery.post/

sorry dude. but I found the culprit.

4.$person_id = $_REQUEST['id'];

use $_GET
and kindly add echo on oci_execute($s) to see if it runs. if not try to add die fuction in this line

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.