Kukula Mula
Kukula Mula

Reputation: 1869

Span variable not being read in php

I have a website that receives parameters in the URL address (www.xxx.html?name=David)

then I assign the value to the html text like this:

<span name="uname" id="uname"> </span>, I'd like to thank you 

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
     var userName = getParameterByName('uname');
     document.getElementById("uname").textContent = userName;
});
</script>

up to this point everything works well

now when I try to read again the value in uname in the PHP file in order to send it in the email I have two issues

  1. the parameter is not received - emailText = $_POST['uname'];
  2. when trying to debug I see the HTML file and js file being loaded and can be debugged but the PHP file doesn't exist in the file tree of the debug

BUT it is being loaded and read because other variables are being read and sent correctly to the email.

Upvotes: 0

Views: 315

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38609

If I need to get value of span ill do something like this

<span data-name="someTextHere">...</span> # data-name will not visible in browser unless page source mode

and you can access it by using ($(this).data('name'))

Ex

var userName = ($(this).data('name'));

And this article Submit Form Using Ajax, PHP and jQuery will helps you to pass data without Undefined Index Error.

Upvotes: 0

Ivar
Ivar

Reputation: 6848

When submitting a form the user agent (browser) will build a form data set from "Succesful controls".

A Control is one of the following:

  • buttons
  • checkboxes
  • radio buttons
  • menus
  • text input
  • file select
  • hidden controls
  • object controls

Only these fields will be submitted when you submit a form. A <span> is not part of that.

If you want to add a value with JavaScript to the form that is not inside of a text input, I suggest that you use a hidden input to do so.

<input name="uname" type="hidden" value="value">

When you edit the span, you should then also change this hidden field to the same value.

Upvotes: 3

Sayonara
Sayonara

Reputation: 267

You cannot use span with name attribute.

Read this https://www.w3schools.com/tags/tag_span.asp

If you want to post, use form + proper input element.

https://www.w3schools.com/html/html_form_input_types.asp

And still, it's not very clear you are really trying to do, show us the whole page ...

Upvotes: 1

Related Questions