将jQuery获得的表单元格的值传递给php页面

时间:2018-11-22 14:55:59

标签: php jquery json ajax

这是我在这里的第一个问题,我有点紧张!我可以使用jQuery获得一个简单的html表单元格的值,但不能将其传递给php页面用作php变量。我已经尝试了2天,使用通过Google找到的每个选项-从Ajax,JSON到_POST / _GET并再次返回。我只是无法管理它,而现在总处于“头脑空白”的状态。

我的测试表:

<apex:commandLink target="_blank" 
styleClass="btn" 
style="text-decoration:none;padding:4px;" 
action="{!URLFOR($Action.Contact.NewContact)}"
value="Create New Contact" />

jQuery

<h3>Test Table</h3>
<table id="sourcetable">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Url</th>
            <th>Country</th>
            <th>Item</th>                       
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>1</td>
            <td>Name 1</td>
            <td>url 1</td>
            <td>Country 1</td>
            <td>Item 1</td>
        </tr>
    </tbody>
</table>

我可以单击表格单元格并获取其值,并将其放入同一页面上的P元素中,例如:

<script type="text/javascript">
$(document).ready(function() {
    $( "#sourcetable tbody tr" ).on( "click", function( event ) {
        $("#fillname").val($(this).find("td").eq(1).html());
        var j=($(this).find("td").eq(1).html());
        document.getElementById("hidfo").innerHTML = JSON.stringify(j); 
    });
}); 
</script>

或html输入字段

<p id="hidfo"></p> 

但是我尝试过的所有其他与传递给php变量有关的尝试都失败了。想法?

<input type="text" id="fillname" value="" />

错误消息

jQuery.Deferred例外:j未定义@ https://xxxxxx.com/portal/trans.php:127:19 l @ https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js:2:29373 a / https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js:2:29677 未定义

我在clicktest.php上使用的简单PHP是:

AJAX

<script type="text/javascript">
$(function(){
$.ajax({
type: "POST",
url: 'clicktest.php',
data: ({prod: j}),
cache: false,
success: function()
{
alert("Order Submitted");
}
});
});
</script>

页面加载时会显示PHP错误:

注意:未定义的索引:第32行上/home/gameon/public_html/portal/clicktest.php中的产品

1 个答案:

答案 0 :(得分:0)

获取private string _phonePrefix; public string PhonePrefix { get; set; } public string PhoneNumber { get; set; } public Country Country { get; set; } = new Country(); public List<Country> Countries { get; set; } = new List<Country>(); public List<string> Prefixes { get; set; } = new List<string>(); public override async Task Initialize() { await base.Initialize(); IsUserLogedIn = await _authService.IsUserLoggedIn(); if (IsUserLogedIn) { //get user data from server, show user data await GetUserData(); } //get countries, prefixes if (Countries.Count <= 0 || Prefixes.Count <= 0) { _registrationFormData = await _registrationService.GetRegistrationFormData(); ProcessFormData(); } AddValidationRules(); } private async Task GetUserData() { try { var userDataResult = await _registrationService.GetLoggedInUserData(); if (userDataResult != null) { if (!userDataResult.HTTPStatusCode.Equals(HttpStatusCode.OK)) { Mvx.IoCProvider.Resolve<IUserDialogs>().Alert(userDataResult.Error?.Message); } else { //PhonePrefix = userDataResult.user.country_code_phone; //can't bind it here to the public binded property because the SelectedItem binding fails. //I first assign it to a private field and then use it in the ProcessFormData method _phonePrefix = userDataResult.user.country_code_phone; PhoneNumber = userDataResult.user.phone; Country.id = userDataResult.user.person.addresses[0].country_id; Country.name = userDataResult.user.person.addresses[0].country_name; } } } catch (Exception e) { Log.Error<RegistrationViewModel>("GetUserData", e); } } private void ProcessFormData() { if (_registrationFormData != null) { Countries = _registrationFormData.Countries?.ToList(); var userCountry = Countries?.Where(c => c.id == Country?.id).FirstOrDefault(); Country = IsUserLogedIn && Country != null ? userCountry : Countries?[0]; var prefixes = new List<string>(); if (Countries != null) { foreach (var country in Countries) { //spinner binding doesn't allow null values if (country.phone_prefix != null) { prefixes.Add(country.phone_prefix); } } } //we need to assign the binded Prefixes at once otherwise the binding for the SelectedItem fails Prefixes = prefixes; if (Prefixes.Count > 0 && !IsUserLogedIn && string.IsNullOrWhiteSpace(_phonePrefix)) { PhonePrefix = Prefixes?[0]; } else { PhonePrefix = _phonePrefix; } } } 的值后,您需要在j处理程序范围之外定义.on( "click", function(或从其中调用AJAX调用:

j