How to pass a variable from php to a modal?

How do I pass a value to a PHP script using AJAX?

  • php file : <?php include 'conn.php'; $temp=$_POST['Imgname']; mysqli_query($con, ("Insert into TempTableForAjax values (2,'$temp')")); ?> //the $_POST['Imgname'] is always empty :/ the jquery : $.ajax({ type: "POST", url: 'phpcodes/AjaxCallTest.php', data: ({Imgname:"13"}), success: function() { alert('form was submitted'); } });

  • Answer:

    use either $value_name = $_GET['value_name'] or $value_name = $POST['value_name'] in your PHP code. In jQuery you would then use an ajax call to that page or controller file like this: $.ajax({url : url_param, data : {'value_name' = 'value'}, dataType : 'json', type : 'GET/POST' });

Torrance Miller at Quora Visit the source

Was this solution helpful to you?

Other answers

if you require the output (if it's html, say) on the same page, the better deal would be to use $("div").load("your_url.php?your_var="+your_var_value); where "div" is the div tag in which you're loading the processed html.

Shival Gupta

I have a feeling that the PHP script is encountering a fatal error in conn.php and not continuing after the error. I would make sure PHP error reporting and display errors feature is set in your PHP configuration file. That being said, I tried your code without including the database connection and the query. So we know the POSTed value is being received by the server. Try it out with the following code: JavaScript <script type="text/javascript"> $(function(){ $.ajax({ type: "POST", url: 'AjaxCallTest.php', data: ({Imgname:"13"}), success: function(data) { alert(data); } }); }); </script> PHP <?php $temp = $_POST['Imgname']; echo $temp; ?> Make sure you are using mysqli* family of functions securely. mysqli* family of functions support parameterization of input, this feature protects your web application against SQL Injection Attack: https://www.owasp.org/index.php/SQL_Injection https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

Gopal Adhikari

If you want to use pure JS to pass the value through AJAX, given below is the code if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { alert('form was submitted'); } }; xmlhttp.open("POST", "phpcodes/AjaxCallTest.php", true); xmlhttp.send("Imgname=13");

Robin Thomas

Toby Thain

You should check these one with a working demo how to http://whats-online.info/science-and-tutorials/30/select-preview-rename-and-upload-image-using-jquery-Ajax/ .These post assisted me create a classic file chooser that allows submit a file to the server without page load. I think these a nice article for you

Dansyo Stylagard

Edit: misread your code. Below is still valid. I imagine you are practicing / learning but please read up on sanitization. Your current script is very insecure.

Andrew Spode Miller

try this $.ajax({ method:"POST", url:"phpcodes/AjaxCallTest.php", data:{Imgname:"13"}, success:function(data) { alert("data sent"); }, error:function(data) { alert("Data sending failed"); } });

Prakash Chokalingam C

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.