简体   繁体   中英

javascript inside php and inside that javascript php code

I have scenario that I want javascript inside php code and in this javascript I want to execute php code for execute mysql query.

On my submit button I have call javascript so I can't call ajax for checking value from database so I have put code in submit button function.

I need functionality if user confirm then mysql update query will execute but if user not confirm then it will goes to accountlist.php page

         if (isset($_POST['createcomapny']) && $_POST['createcomapny'] == "Submit") {
              $company = $_POST['cmpname'];
              $user = $_POST['username'];
              $checkquery = mysql_query("select * from license_info where company_name='$company' and admin_user_name='$user'");

              if($checkrecord>0){
                      echo "<script>var conf=confirm('Are you sure want to create this company'); if(conf){";
                      mysql_query("update license_info set admin_user_name='lalu' where company_name='$company' and admin_user_name='$user'");
                      echo "} else { windows.location='accountlist.php';  }</script>";
        }

On submit button I have check javascript validation.

function create()
            {
                if (document.companyregister.cmpname.value === "")
                {
                    alert("Please Enter Company name");
                    document.companyregister.cmpname.value = "";
                    document.companyregister.cmpname.focus();
                    return false;
                }
                var company = document.companyregister.cmpname.value;
                var user = document.companyregister.user.value;
                var xmlhttp = new XMLHttpRequest();                    
                xmlhttp.onreadystatechange = function() {            
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                    
                var res = xmlhttp.responseText;                    
                var resu = res.split("|");                    
                if(resu[0]=="lokesh"){                        
                      document.getElementById('selectdriver1').style.display = 'block';                                                
                      document.getElementById("drvr").style.display = "none";
                      document.getElementById('findprinter').disabled = false;
                }                                        
                document.getElementById('driver').innerHTML = resu[1];
                return false;
                }
            }
            xmlhttp.open("GET", "getaccount.php?", true);
            xmlhttp.send();                                        
            return true;

            document.getElementById("formsubmitting").style.display = "block";
            document.getElementById("hidesubmit").style.display = "none";
            return true;
            }

Just use form with method="POST". If user confirms, set value of input named "confirm" true in javascript and send form. You can add all data you need as hidden inputs.

Javascript to submit it:

if(conf){ document.getElementById("myForm").submit(); }

PHP:

if($_POST['confirm']==true) { do action }

Btw. Can't you popup this message to confirm before sending a form? Basically you will send 2 times form like that. You can just add confirmation before sending first one...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM